import {TypedSopNode} from "./_Base";
import {HierarchyMode, HierarchySopOperation, HIERARCHY_MODES} from "../../../core/operations/sop/Hierarchy";
const modesWithLevel = [HierarchyMode.ADD_PARENT, HierarchyMode.REMOVE_PARENT];
import {NodeParamsConfig, ParamConfig} from "../utils/params/ParamsConfig";
const DEFAULT = HierarchySopOperation.DEFAULT_PARAMS;
class HierarchySopParamsConfig extends NodeParamsConfig {
  constructor() {
    super(...arguments);
    this.mode = ParamConfig.INTEGER(DEFAULT.mode, {
      menu: {
        entries: HIERARCHY_MODES.map((m, i) => {
          return {name: m, value: i};
        })
      }
    });
    this.levels = ParamConfig.INTEGER(DEFAULT.levels, {
      range: [0, 5],
      visibleIf: [
        {mode: HIERARCHY_MODES.indexOf(HierarchyMode.ADD_PARENT)},
        {mode: HIERARCHY_MODES.indexOf(HierarchyMode.REMOVE_PARENT)}
      ]
    });
    this.objectMask = ParamConfig.STRING("", {
      visibleIf: {mode: HIERARCHY_MODES.indexOf(HierarchyMode.ADD_CHILD)}
    });
    this.debugObjectMask = ParamConfig.BOOLEAN(0, {
      visibleIf: {mode: HIERARCHY_MODES.indexOf(HierarchyMode.ADD_CHILD)}
    });
  }
}
const ParamsConfig2 = new HierarchySopParamsConfig();
export class HierarchySopNode extends TypedSopNode {
  constructor() {
    super(...arguments);
    this.params_config = ParamsConfig2;
  }
  static type() {
    return "hierarchy";
  }
  static displayedInputNames() {
    return ["geometry to add or remove parents to/from", "objects to use as parent or children (optional)"];
  }
  initializeNode() {
    this.io.inputs.setCount(1, 2);
    this.io.inputs.initInputsClonedState(HierarchySopOperation.INPUT_CLONED_STATE);
    this.scene().dispatchController.onAddListener(() => {
      this.params.onParamsCreated("params_label", () => {
        this.params.label.init([this.p.mode, this.p.levels, this.p.objectMask], () => {
          const mode = HIERARCHY_MODES[this.pv.mode];
          if (modesWithLevel.includes(mode)) {
            return `${mode} ${this.pv.levels}`;
          } else {
            return `${mode} (with mask: ${this.pv.objectMask})`;
          }
        });
      });
    });
  }
  cook(input_contents) {
    this._operation = this._operation || new HierarchySopOperation(this._scene, this.states);
    const core_group = this._operation.cook(input_contents, this.pv);
    this.setCoreGroup(core_group);
  }
}
