import {TypedSopNode} from "./_Base";
import {InputCloneMode as InputCloneMode2} from "../../poly/InputCloneMode";
import {NodeContext as NodeContext2} from "../../poly/NodeContext";
import {NodeParamsConfig, ParamConfig} from "../utils/params/ParamsConfig";
class TransformCopySopParamConfig extends NodeParamsConfig {
  constructor() {
    super(...arguments);
    this.useSecondInput = ParamConfig.BOOLEAN(1);
    this.reference = ParamConfig.OPERATOR_PATH("", {
      nodeSelection: {
        context: NodeContext2.SOP
      },
      visibleIf: {useSecondInput: 0}
    });
  }
}
const ParamsConfig2 = new TransformCopySopParamConfig();
export class TransformCopySopNode extends TypedSopNode {
  constructor() {
    super(...arguments);
    this.params_config = ParamsConfig2;
  }
  static type() {
    return "transformCopy";
  }
  static displayedInputNames() {
    return ["objects to transform", "objects to copy transform from"];
  }
  initializeNode() {
    this.io.inputs.setCount(1, 2);
    this.io.inputs.initInputsClonedState([InputCloneMode2.FROM_NODE, InputCloneMode2.NEVER]);
  }
  cook(input_contents) {
    if (this.pv.useSecondInput == true && input_contents[1]) {
      this._copy_from_src_objects(input_contents[0].objects(), input_contents[1].objects());
    } else {
      this._copy_from_found_node(input_contents[0].objects());
    }
  }
  _copy_from_src_objects(target_objects, src_objects) {
    let target_object;
    let src_object;
    for (let i = 0; i < target_objects.length; i++) {
      target_object = target_objects[i];
      src_object = src_objects[i];
      src_object.updateMatrix();
      target_object.matrix.copy(src_object.matrix);
      target_object.matrix.decompose(target_object.position, target_object.quaternion, target_object.scale);
    }
    this.setObjects(target_objects);
  }
  async _copy_from_found_node(target_objects) {
    const node = this.p.reference.found_node_with_context(NodeContext2.SOP);
    if (node) {
      const container = await node.requestContainer();
      const core_group = container.coreContent();
      if (core_group) {
        const src_objects = core_group.objects();
        this._copy_from_src_objects(target_objects, src_objects);
        return;
      }
    }
    this.setObjects(target_objects);
  }
}
