import { NodeManager } from "../Mangers/NodeManager";
import { Node, NodeType } from "../structs/node";
import { NodeDefinition } from "./Nodes";

export let NodeArray: NodeDefinition = {
  os: 'any',

  isSingle: true,
  name: 'Array',
  typeId: 'array',

  w: 200,

  statics: [
    {
      name: "Key",
      type: NodeType.Label,
      value: null
    }
  ],

  outputs: [
    {
      name: "Flow",
      type: NodeType.Flow,
    },
    {
      name: "Array",
      type: NodeType.Array
    }
  ],

  inputs: [
    {
      name: "Flow",
      type: NodeType.Flow,
    },
    {
      name: "Value",
      type: NodeType.Any
    }
  ],

  onInputsUpdate: async ( node: Node ) => {
    let lastInput = node.inputs[node.inputs.length - 1];

    if(lastInput.connections.length != 0){
      node.inputs.push({
        name: "Value",
        type: NodeType.Any,
        connections: [],
        parent: node,
        index: node.inputs.length
      });
    } else{
      if(node.inputs.length > 2){
        let prevInput = node.inputs[node.inputs.length - 2];
        if(prevInput.connections.length == 0)
          node.inputs.pop();
      }
    }

    node.updateSize();
    NodeManager.Instance.UpdateConfig();
  }
};