import {TypedSopNode} from "./_Base";
import {NodeParamsConfig, ParamConfig} from "../utils/params/ParamsConfig";
import {NetworkChildNodeType} from "../../poly/NodeContext";
class SubnetInputSopParamsConfig extends NodeParamsConfig {
  constructor() {
    super(...arguments);
    this.input = ParamConfig.INTEGER(0, {
      range: [0, 3],
      rangeLocked: [true, true],
      callback: (node) => {
        SubnetInputSopNode.PARAM_CALLBACK_reset(node);
      }
    });
  }
}
const ParamsConfig2 = new SubnetInputSopParamsConfig();
export class SubnetInputSopNode extends TypedSopNode {
  constructor() {
    super(...arguments);
    this.params_config = ParamsConfig2;
  }
  static type() {
    return NetworkChildNodeType.INPUT;
  }
  initializeNode() {
    this.io.inputs.setCount(0);
    this.lifecycle.add_on_add_hook(() => {
      this.set_parent_input_dependency();
    });
  }
  async cook() {
    const input_index = this.pv.input;
    const parent = this.parent();
    if (parent) {
      if (parent.io.inputs.has_input(input_index)) {
        const container = await parent.container_controller.requestInputContainer(input_index);
        if (container) {
          const core_group = container.coreContent();
          if (core_group) {
            this.setCoreGroup(core_group);
            return;
          }
        }
      } else {
        this.states.error.set(`parent has no input ${input_index}`);
      }
      this.cookController.end_cook();
    } else {
      this.states.error.set(`subnet input has no parent`);
    }
  }
  static PARAM_CALLBACK_reset(node) {
    node.set_parent_input_dependency();
  }
  set_parent_input_dependency() {
    if (this._current_parent_input_graph_node) {
      this.removeGraphInput(this._current_parent_input_graph_node);
    }
    const parent = this.parent();
    if (parent) {
      this._current_parent_input_graph_node = parent.io.inputs.input_graph_node(this.pv.input);
      this.addGraphInput(this._current_parent_input_graph_node);
    }
  }
}
