import {ContainerClassMap} from "../../containers/utils/ContainerMap";
export class TypedContainerController {
  constructor(node) {
    this.node = node;
    this._callbacks = [];
    this._callbacks_tmp = [];
    const container_class = ContainerClassMap[node.nodeContext()];
    this._container = new container_class(this.node);
  }
  get container() {
    return this._container;
  }
  async requestContainer() {
    if (this.node.flags?.bypass?.active()) {
      return await this.requestInputContainer(0) || this._container;
    }
    if (this.node.isDirty()) {
      return new Promise((resolve, reject) => {
        this._callbacks.push(resolve);
        this.node.cookController.cook_main();
      });
    }
    return this._container;
  }
  async requestInputContainer(input_index) {
    const input_node = this.node.io.inputs.input(input_index);
    if (input_node) {
      return await input_node.requestContainer();
    } else {
      this.node.states.error.set(`input ${input_index} required`);
      this.notify_requesters();
      return null;
    }
  }
  notify_requesters(container) {
    this._callbacks_tmp = this._callbacks.slice();
    this._callbacks.splice(0, this._callbacks.length);
    if (!container) {
      container = this.node.container_controller.container;
    }
    let callback;
    while (callback = this._callbacks_tmp.pop()) {
      callback(container);
    }
    this.node.scene().cookController.remove_node(this.node);
  }
}
export class BaseContainerController extends TypedContainerController {
}
