'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-b8c5f2d4.js');
const i18n = require('./i18n-92762f48.js');
const utils = require('./utils-94ef970a.js');
require('./_commonjsHelpers-dcc4cf71.js');

const FeatureList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.data = undefined;
    this.limit = undefined;
    this.batchSize = 20;
    this.component = undefined;
    this.componentOptions = undefined;
    this.display = "visible";
    this.features = [];
    this.item = true;
    this.loadingSpinner = "bubbles";
    this.loadingText = i18n._t("webmapgl.feature-list.loading");
    this.mapId = undefined;
    this.orderBy = undefined;
    this.order = "asc";
    this.styleId = undefined;
    this.sourceId = undefined;
  }
  async componentWillLoad() {
    await this.handleSourceId();
  }
  componentDidLoad() {
    const map = utils.getMap(this.mapId).map;
    if (map != undefined)
      map.on("moveend", async () => await this.filter());
  }
  handleInfiniteScroll(e) {
    this.limit += this.batchSize;
    e.target.complete();
  }
  handleStyleUpdated(e) {
    if (e.detail.id === this.styleId)
      this.handleSourceId();
  }
  async handleSourceId() {
    const style = document.querySelector(`#${this.styleId}`);
    if (style == undefined || style.json.sources == undefined)
      return;
    const data = style.json.sources[this.sourceId].data;
    if (typeof data === "string") {
      let res = await fetch(data);
      this.data = (await res.json());
    }
    else {
      this.data = data;
    }
    this.filter();
  }
  async filter() {
    var _a;
    if (((_a = this.data) === null || _a === void 0 ? void 0 : _a.features) == undefined || this.data.features.length === 0)
      return;
    let features;
    if (this.display === "all") {
      features = this.data.features;
    }
    else {
      let bounds = utils.getMap(this.mapId).map.getBounds();
      features = this.data.features.filter((feature) => {
        let [lng, lat] = feature.geometry.coordinates;
        return (lng >= bounds.getWest() &&
          lng <= bounds.getEast() &&
          lat >= bounds.getSouth() &&
          lat <= bounds.getNorth());
      });
    }
    this.limit = this.batchSize;
    this.features = this.sort(features);
  }
  sort(features) {
    if (this.order === "none" || this.orderBy == undefined)
      return features;
    features.sort((aFeature, bFeature) => {
      const a = aFeature.properties[this.orderBy];
      const b = bFeature.properties[this.orderBy];
      if (a < b)
        return this.order === "asc" ? -1 : 1;
      if (a > b)
        return this.order === "asc" ? 1 : -1;
      return 0;
    });
    return features;
  }
  render() {
    let ComponentTag = this.component;
    let features = this.limit !== 0 ? this.features.slice(0, this.limit) : this.features;
    let hasMore = features.length < this.features.length;
    let items = features.map((feature) => {
      let component = (index.h(ComponentTag, Object.assign({ feature: feature }, this.componentOptions)));
      return this.item ? index.h("ion-item", null, component) : component;
    });
    return (index.h(index.Host, null, index.h("ion-list", null, index.h("slot", { name: "start" }), items, index.h("slot", { name: "end" })), index.h("ion-infinite-scroll", { disabled: !hasMore }, index.h("ion-infinite-scroll-content", { "loading-spinner": this.loadingSpinner, "loading-text": this.loadingText }))));
  }
  static get watchers() { return {
    "styleId": ["handleSourceId"],
    "sourceId": ["handleSourceId"]
  }; }
};

exports.gl_feature_list = FeatureList;

//# sourceMappingURL=gl-feature-list.cjs.entry.js.map