import { r as registerInstance } from './index-cb74209c.js';

class LikeProxy {
  constructor(feature, options = {}) {
    this.feature = feature;
    if (options.keyPrefix == undefined)
      options.keyPrefix = "webmapgl.like";
    if (options.clientId == undefined) {
      options.clientId = localStorage.getItem(options.keyPrefix + ".clientId");
      if (options.clientId == null) {
        options.clientId = this.makeId(64);
        localStorage.setItem(options.keyPrefix + ".clientId", options.clientId);
      }
    }
    this.options = options;
  }
  getLayerId() {
    var _a;
    return "layer" in this.feature && Boolean((_a = this.feature.layer) === null || _a === void 0 ? void 0 : _a.id)
      ? this.feature.layer.id
      : undefined;
  }
  getClientId() {
    return this.options.clientId;
  }
  isLiked() {
    let likes = this.getArray(this.getLayerId());
    return likes.indexOf(Number(this.feature.id)) !== -1;
  }
  like() {
    let likes = this.getArray(this.getLayerId());
    if (likes.indexOf(Number(this.feature.id)) === -1)
      likes.push(Number(this.feature.id));
    this.setArray(this.getLayerId(), likes);
  }
  unlike() {
    let likes = this.getArray(this.getLayerId());
    let idx = likes.indexOf(Number(this.feature.id));
    if (idx !== -1)
      likes.splice(idx, 1);
    this.setArray(this.getLayerId(), likes);
  }
  makeId(length) {
    var id = "";
    var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789";
    for (let i = 0; i < length; i++)
      id += chars.charAt(Math.floor(Math.random() * chars.length));
    return id;
  }
  getLayerKey(layerId) {
    return (this.options.keyPrefix +
      "|" +
      document.location.pathname +
      "|" +
      (layerId || "default"));
  }
  getArray(layer) {
    let str = localStorage.getItem(this.getLayerKey(layer));
    if (str == null)
      return [];
    return str
      .split(",")
      .map((v) => parseInt(v))
      .filter((v) => !isNaN(v));
  }
  setArray(layer, values) {
    let str = values
      .filter((v) => !isNaN(v))
      .map((v) => v.toString())
      .join(",");
    localStorage.setItem(this.getLayerKey(layer), str);
  }
}

const LikeController = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
  }
  /**
   * Creates a proxy to communicate likes
   * @param {Feature | MapboxGeoJSONFeature} feature The feature whose likes to change. Can be either just a geojson Feature or a MapboxGeoJSONFeature with extra layer information
   * @param {LikeOptions} options Extra options for the like controller
   * @returns
   */
  async create(feature, options = {}) {
    return new LikeProxy(feature, options);
  }
};

export { LikeController as gl_like_controller };

//# sourceMappingURL=gl-like-controller.entry.js.map