import {TypedGlNode} from "./_Base";
import {NodeParamsConfig, ParamConfig} from "../utils/params/ParamsConfig";
import {GlConnectionPointType} from "../utils/io/connections/Gl";
import {ShaderName as ShaderName2} from "../utils/shaders/ShaderName";
import {VaryingGLDefinition} from "./utils/GLDefinition";
const VARYING_NODE_AVAILABLE_GL_TYPES = [
  GlConnectionPointType.FLOAT,
  GlConnectionPointType.VEC2,
  GlConnectionPointType.VEC3,
  GlConnectionPointType.VEC4
];
class VaryingReadGlParamsConfig extends NodeParamsConfig {
  constructor() {
    super(...arguments);
    this.name = ParamConfig.STRING("");
    this.type = ParamConfig.INTEGER(0, {
      menu: {
        entries: VARYING_NODE_AVAILABLE_GL_TYPES.map((name, i) => {
          return {name, value: i};
        })
      }
    });
  }
}
const ParamsConfig2 = new VaryingReadGlParamsConfig();
const VaryingReadGlNode2 = class extends TypedGlNode {
  constructor() {
    super(...arguments);
    this.params_config = ParamsConfig2;
    this._on_create_set_name_if_none_bound = this._on_create_set_name_if_none.bind(this);
  }
  static type() {
    return "varyingRead";
  }
  initializeNode() {
    this.addPostDirtyHook("_set_mat_to_recompile", this._set_mat_to_recompile.bind(this));
    this.lifecycle.add_on_create_hook(this._on_create_set_name_if_none_bound);
    this.io.connection_points.initializeNode();
    this.io.connection_points.set_output_name_function(() => {
      return this.output_name;
    });
    this.io.connection_points.set_expected_input_types_function(() => []);
    this.io.connection_points.set_expected_output_types_function(() => [
      VARYING_NODE_AVAILABLE_GL_TYPES[this.pv.type]
    ]);
    this.scene().dispatchController.onAddListener(() => {
      this.params.onParamsCreated("params_label", () => {
        this.params.label.init([this.p.name]);
      });
    });
  }
  get output_name() {
    return VaryingReadGlNode2.OUTPUT_NAME;
  }
  set_lines(shaders_collection_controller) {
    if (shaders_collection_controller.current_shader_name == ShaderName2.FRAGMENT) {
      const varying_name = this.pv.name;
      const definition = new VaryingGLDefinition(this, this.gl_type(), varying_name);
      const out_value = this.gl_var_name(VaryingReadGlNode2.OUTPUT_NAME);
      const body_line = `${this.gl_type()} ${out_value} = ${varying_name}`;
      shaders_collection_controller.add_definitions(this, [definition]);
      shaders_collection_controller.add_body_lines(this, [body_line]);
    }
  }
  get attribute_name() {
    return this.pv.name.trim();
  }
  gl_type() {
    return this.io.outputs.named_output_connection_points[0].type();
  }
  set_gl_type(type) {
    this.p.type.set(VARYING_NODE_AVAILABLE_GL_TYPES.indexOf(type));
  }
  _on_create_set_name_if_none() {
    if (this.pv.name == "") {
      this.p.name.set(this.name());
    }
  }
};
export let VaryingReadGlNode = VaryingReadGlNode2;
VaryingReadGlNode.OUTPUT_NAME = "fragment";
