export class AnimationMixerStore extends Map {
  pauseAll() {
    for (const mixer of this.values()) mixer.timeScale = 0;
  }

  resumeAll() {
    for (const mixer of this.values()) mixer.timeScale = 1;
  }

  updateAll(delta) {
    for (const mixer of this.values()) mixer.update(delta);
  }

  release(model) {
    const mixer = this.get(model);
    if (!mixer) return false;
    mixer.stopAllAction?.();
    mixer.uncacheRoot?.(model);
    return this.delete(model);
  }

  releaseTree(root) {
    root?.traverse?.(object => this.release(object));
  }

  dispose() {
    for (const model of [...this.keys()]) this.release(model);
  }
}

export function createAnimationMixerStore() {
  return new AnimationMixerStore();
}
