import {TypedNode} from "../_Base";
import {CoreGroup} from "../../../core/geometry/Group";
import {ObjectType} from "../../../core/geometry/Constant";
import {NodeContext as NodeContext2} from "../../poly/NodeContext";
import {FlagsControllerDBO} from "../utils/FlagsController";
import {BaseSopOperation} from "../../../core/operations/sop/_Base";
var MESSAGE;
(function(MESSAGE2) {
  MESSAGE2["FROM_SET_CORE_GROUP"] = "from set_core_group";
  MESSAGE2["FROM_SET_GROUP"] = "from set_group";
  MESSAGE2["FROM_SET_OBJECTS"] = "from set_objects";
  MESSAGE2["FROM_SET_OBJECT"] = "from set_object";
  MESSAGE2["FROM_SET_GEOMETRIES"] = "from set_geometries";
  MESSAGE2["FROM_SET_GEOMETRY"] = "from set_geometry";
})(MESSAGE || (MESSAGE = {}));
const INPUT_GEOMETRY_NAME = "input geometry";
const DEFAULT_INPUT_NAMES = [INPUT_GEOMETRY_NAME, INPUT_GEOMETRY_NAME, INPUT_GEOMETRY_NAME, INPUT_GEOMETRY_NAME];
export class TypedSopNode extends TypedNode {
  constructor() {
    super(...arguments);
    this.flags = new FlagsControllerDBO(this);
  }
  static nodeContext() {
    return NodeContext2.SOP;
  }
  static displayedInputNames() {
    return DEFAULT_INPUT_NAMES;
  }
  initializeBaseNode() {
    this.flags.display.set(false);
    this.flags.display.add_hook(() => {
      if (this.flags.display.active()) {
        const parent = this.parent();
        if (parent && parent.display_node_controller) {
          parent.display_node_controller.set_display_node(this);
        }
      }
    });
    this.io.outputs.set_has_one_output();
  }
  setCoreGroup(core_group) {
    this.setContainer(core_group, MESSAGE.FROM_SET_CORE_GROUP);
  }
  setObject(object) {
    this.set_container_objects([object], MESSAGE.FROM_SET_OBJECT);
  }
  setObjects(objects) {
    this.set_container_objects(objects, MESSAGE.FROM_SET_OBJECTS);
  }
  setGeometry(geometry, type = ObjectType.MESH) {
    const object = this.create_object(geometry, type);
    this.set_container_objects([object], MESSAGE.FROM_SET_GEOMETRY);
  }
  setGeometries(geometries, type = ObjectType.MESH) {
    const objects = [];
    let object;
    for (let geometry of geometries) {
      object = this.create_object(geometry, type);
      objects.push(object);
    }
    this.set_container_objects(objects, MESSAGE.FROM_SET_GEOMETRIES);
  }
  set_container_objects(objects, message) {
    const core_group = this.container_controller.container.coreContent() || new CoreGroup();
    core_group.setObjects(objects);
    core_group.touch();
    this.setContainer(core_group);
  }
  static create_object(geometry, type, material) {
    return BaseSopOperation.create_object(geometry, type, material);
  }
  create_object(geometry, type, material) {
    return TypedSopNode.create_object(geometry, type, material);
  }
  static create_index_if_none(geometry) {
    BaseSopOperation.create_index_if_none(geometry);
  }
  _create_index_if_none(geometry) {
    TypedSopNode.create_index_if_none(geometry);
  }
  _add_index(geometry) {
    const position_attrib = geometry.getAttribute("position");
    const position_array = position_attrib.array;
    const points_count = position_array.length / 3;
    const indices = new Array(points_count);
    for (let i = 0; i < points_count; i++) {
      indices[i] = i;
    }
    geometry.setIndex(indices);
  }
}
export class BaseSopNodeClass extends TypedSopNode {
}
