import { SoundboardClipReader } from './SoundboardClipReader';
import { SoundboardClips } from './types';

/** Parallels custom-emoji/PackImagesReader. */
export class SoundboardClipsReader {
  private readonly rawClips: SoundboardClips;

  private shortcodeToClips: Map<string, SoundboardClipReader> | undefined;

  constructor(clips: SoundboardClips) {
    this.rawClips = clips;
  }

  get collection(): Map<string, SoundboardClipReader> {
    if (this.shortcodeToClips) return this.shortcodeToClips;

    const shortcodeToClips: Map<string, SoundboardClipReader> = new Map();
    Object.entries(this.rawClips).forEach(([shortcode, clip]) => {
      const reader = SoundboardClipReader.fromClip(shortcode, clip);
      if (reader) shortcodeToClips.set(shortcode, reader);
    });

    this.shortcodeToClips = shortcodeToClips;
    return this.shortcodeToClips;
  }
}
