import {TypedPathParam} from "./_BasePath";
import {CoreWalker, TypedParamPathParamValue} from "../../core/Walker";
import {ParamType as ParamType2} from "../poly/ParamType";
import {ParamEvent as ParamEvent2} from "../poly/ParamEvent";
export class ParamPathParam extends TypedPathParam {
  static type() {
    return ParamType2.PARAM_PATH;
  }
  initialize_param() {
    this._value = new TypedParamPathParamValue();
  }
  get default_value_serialized() {
    return this.default_value;
  }
  get raw_input_serialized() {
    return `${this._raw_input}`;
  }
  get value_serialized() {
    return `${this.value}`;
  }
  _copy_value(param) {
    this.set(param.value_serialized);
  }
  static are_raw_input_equal(raw_input1, raw_input2) {
    return raw_input1 == raw_input2;
  }
  static are_values_equal(val1, val2) {
    return val1 == val2;
  }
  get is_default() {
    return this._raw_input == this.default_value;
  }
  setParam(param) {
    this.set(param.fullPath());
  }
  process_raw_input() {
    if (this._value.path() != this._raw_input) {
      this._value.set_path(this._raw_input);
      this.find_target();
      this.setDirty();
      this.emitController.emit(ParamEvent2.VALUE_UPDATED);
    }
  }
  async process_computation() {
    this.find_target();
  }
  find_target() {
    if (!this.node) {
      return;
    }
    const path = this._raw_input;
    let param = null;
    const path_non_empty = path != null && path !== "";
    this.scene().referencesController.reset_reference_from_param(this);
    this.decomposed_path.reset();
    if (path_non_empty) {
      param = CoreWalker.find_param(this.node, path, this.decomposed_path);
    }
    const current_found_entity = this._value.param();
    const newly_found_entity = param;
    this.scene().referencesController.set_named_nodes_from_param(this);
    if (param) {
      this.scene().referencesController.set_reference_from_param(this, param);
    }
    if (current_found_entity?.graphNodeId() !== newly_found_entity?.graphNodeId()) {
      const dependent_on_found_node = this.options.dependent_on_found_node();
      const previously_found_node = this._value.param();
      if (previously_found_node) {
        if (dependent_on_found_node) {
          this.removeGraphInput(previously_found_node);
        } else {
        }
      }
      if (param) {
        this._assign_found_node(param);
      } else {
        this._value.set_param(null);
      }
      this.options.execute_callback();
    }
    this.removeDirtyState();
  }
  _assign_found_node(param) {
    const dependent_on_found_node = this.options.dependent_on_found_node();
    this._value.set_param(param);
    if (dependent_on_found_node) {
      this.addGraphInput(param);
    }
  }
  notify_path_rebuild_required(param) {
    this.decomposed_path.update_from_name_change(param);
    const new_path = this.decomposed_path.to_path();
    this.set(new_path);
  }
  notify_target_param_owner_params_updated(node) {
    this.setDirty();
  }
}
