import { CoreString } from "@polygonjs/polygonjs/dist/src/core/String";
import { FeatureConverter } from "../../../core/mapbox/FeatureConverter";
const DEFAULT_LIST = [
  "road-primary",
  "road-secondary-tertiary",
  "road-street"
].join(" ");
import { NodeParamsConfig, ParamConfig } from "@polygonjs/polygonjs/dist/src/engine/nodes/utils/params/ParamsConfig";
import { MapboxListenerParamConfig, MapboxListenerSopNode } from "./utils/MapboxListener";
import { MapUtils } from "@polygonjs/polygonjs/dist/src/core/MapUtils";
import { ArrayUtils } from "@polygonjs/polygonjs/dist/src/core/ArrayUtils";
class MapboxLayerSopParamsConfig extends MapboxListenerParamConfig(NodeParamsConfig) {
  constructor() {
    super(...arguments);
    this.layers = ParamConfig.STRING(DEFAULT_LIST);
  }
}
const ParamsConfig = new MapboxLayerSopParamsConfig();
export class MapboxLayerSopNode extends MapboxListenerSopNode {
  constructor() {
    super(...arguments);
    this.paramsConfig = ParamsConfig;
    this._features_by_name = /* @__PURE__ */ new Map();
  }
  static type() {
    return "mapboxLayer";
  }
  cook() {
    this._mapboxListener.cook();
  }
  _postInitController() {
    if (!this._cameraNode) {
      return;
    }
    const firstMap = this._cameraNode.firstMap();
    if (firstMap == null) {
      this.states.error.set("map not initialized yet");
      return;
    }
    const layerNames = CoreString.attribNames(this.pv.layers);
    const existingLayerNames = [];
    for (let layerName of layerNames) {
      if (firstMap.getLayer(layerName)) {
        existingLayerNames.push(layerName);
      } else {
        this.states.error.set(`layer ${layerName} does not exist`);
        return;
      }
    }
    const features = firstMap.queryRenderedFeatures(void 0, {
      layers: existingLayerNames
    });
    const objects = [];
    if (features) {
      const featuresByName = this._groupFeaturesByName(features);
      featuresByName.forEach((featuresForName, featureName) => {
        const converter = new FeatureConverter(this, featureName, featuresForName);
        const new_object = converter.createObject();
        if (new_object) {
          objects.push(new_object);
        }
      });
    }
    this.setObjects(objects);
  }
  _groupFeaturesByName(features) {
    this._features_by_name.clear();
    for (let feature of features) {
      const name = this._feature_name(feature);
      if (name) {
        MapUtils.pushOnArrayAtEntry(this._features_by_name, name, feature);
      }
    }
    return this._features_by_name;
  }
  _feature_name(feature) {
    const properties = feature["properties"];
    let name;
    if (properties) {
      name = properties["name"] || properties["name_en"];
      if (name == null) {
        name = this._id_from_feature(feature);
      }
    }
    return name;
  }
  _id_from_feature(feature) {
    const json_str = JSON.stringify(feature.geometry).replace(/{|}|"|:|\[|\]|,|\./g, "");
    const json_str_elements = json_str.split("");
    const letters_count = 30;
    const chunks = ArrayUtils.chunk(json_str_elements, json_str_elements.length / letters_count);
    const first_elements = chunks.map((c) => c[0]);
    return first_elements.join("");
  }
}
