import {TypedSopNode} from "./_Base";
import {JsonDataLoader} from "../../../core/loader/geometry/JsonData";
import {NodeParamsConfig, ParamConfig} from "../utils/params/ParamsConfig";
import {CsvLoader} from "../../../core/loader/geometry/Csv";
import {ObjectType} from "../../../core/geometry/Constant";
import {ASSETS_ROOT} from "../../../core/loader/AssetsUtils";
export var DataType;
(function(DataType2) {
  DataType2["JSON"] = "json";
  DataType2["CSV"] = "csv";
})(DataType || (DataType = {}));
export const DATA_TYPES = [DataType.JSON, DataType.CSV];
const DEFAULT_URL = `${ASSETS_ROOT}/nodes/sop/DataUrl/basic.json`;
class DataUrlSopParamsConfig extends NodeParamsConfig {
  constructor() {
    super(...arguments);
    this.dataType = ParamConfig.INTEGER(DATA_TYPES.indexOf(DataType.JSON), {
      menu: {
        entries: DATA_TYPES.map((t, i) => {
          return {
            name: t,
            value: i
          };
        })
      }
    });
    this.url = ParamConfig.STRING(DEFAULT_URL);
    this.jsonDataKeysPrefix = ParamConfig.STRING("", {
      visibleIf: {dataType: DATA_TYPES.indexOf(DataType.JSON)}
    });
    this.skipEntries = ParamConfig.STRING("", {
      visibleIf: {dataType: DATA_TYPES.indexOf(DataType.JSON)}
    });
    this.convert = ParamConfig.BOOLEAN(0, {
      visibleIf: {dataType: DATA_TYPES.indexOf(DataType.JSON)}
    });
    this.convertToNumeric = ParamConfig.STRING("", {
      visibleIf: {
        dataType: DATA_TYPES.indexOf(DataType.JSON),
        convert: 1
      }
    });
    this.readAttribNamesFromFile = ParamConfig.BOOLEAN(1, {
      visibleIf: {dataType: DATA_TYPES.indexOf(DataType.CSV)}
    });
    this.attribNames = ParamConfig.STRING("height scale", {
      visibleIf: {
        dataType: DATA_TYPES.indexOf(DataType.CSV),
        readAttribNamesFromFile: 0
      }
    });
    this.reload = ParamConfig.BUTTON(null, {
      callback: (node, param) => {
        DataUrlSopNode.PARAM_CALLBACK_reload(node, param);
      }
    });
  }
}
const ParamsConfig2 = new DataUrlSopParamsConfig();
export class DataUrlSopNode extends TypedSopNode {
  constructor() {
    super(...arguments);
    this.params_config = ParamsConfig2;
  }
  static type() {
    return "dataUrl";
  }
  async cook() {
    switch (DATA_TYPES[this.pv.dataType]) {
      case DataType.JSON:
        return this._load_json();
      case DataType.CSV:
        return this._load_csv();
    }
  }
  _url() {
    const assets_root = this.scene().assets.root();
    if (assets_root) {
      return `${assets_root}${this.pv.url}`;
    } else {
      return this.pv.url;
    }
  }
  _load_json() {
    const loader = new JsonDataLoader({
      dataKeysPrefix: this.pv.jsonDataKeysPrefix,
      skipEntries: this.pv.skipEntries,
      doConvert: this.pv.convert,
      convertToNumeric: this.pv.convertToNumeric
    });
    loader.load(this._url(), this._on_load.bind(this), void 0, this._on_error.bind(this));
  }
  _on_load(geometry) {
    this.setGeometry(geometry, ObjectType.POINTS);
  }
  _on_error(error) {
    this.states.error.set(`could not load geometry from ${this._url()} (${error})`);
    this.cookController.end_cook();
  }
  async _load_csv() {
    const attribNames = this.pv.readAttribNamesFromFile ? void 0 : this.pv.attribNames.split(" ");
    const loader = new CsvLoader(attribNames);
    const geometry = await loader.load(this._url());
    if (geometry) {
      this.setGeometry(geometry, ObjectType.POINTS);
    } else {
      this.states.error.set("could not generate points");
    }
  }
  static PARAM_CALLBACK_reload(node, param) {
    node.param_callback_reload();
  }
  param_callback_reload() {
    this.p.url.setDirty();
  }
}
