function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { GeoJSON } from "ol/format";
import MapboxStyleLayer from "../MapboxStyleLayer";

/**
 * Layer for zweitausbildung pois
 * Extends {@link https://mobility-toolbox-js.geops.io/doc/class/build/ol/layers/MapboxStyleLayer%20js~MapboxStyleLayer%20html-offset-anchor}
 * @private
 * @class
 * @param {Object} [options] Layer options.
 */
class ZweitausbildungPoisLayer extends MapboxStyleLayer {
  constructor() {
    let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    const {
      styleLayersFilter,
      properties: {
        zweitausbildung: {
          filter,
          sourceId
        }
      }
    } = options;
    // We request cluster and unclustered point, not the number.
    super(_objectSpread(_objectSpread({}, options), {}, {
      queryRenderedLayersFilter: _ref => {
        let {
          metadata
        } = _ref;
        const mdValue = !!metadata && metadata["trafimage.filter"];
        const isValid = styleLayersFilter && styleLayersFilter({
          metadata
        });
        return isValid && !/number/.test(mdValue);
      }
    }));
    this.filter = filter;
    this.sourceId = sourceId;
    this.highlightSourceId = "highlight";
    this.updateTimeout = null;
    this.format = new GeoJSON({
      dataProjection: "EPSG:4326",
      featureProjection: "EPSG:3857"
    });
    this.onIdle = this.onIdle.bind(this);
  }

  /**
   * @override
   */
  attachToMap(map) {
    super.attachToMap(map);
    this.olListenersKeys.push(this.on("change:visible", () => {
      this.updateClusterSource();
    }), this.map.on("movestart", () => {
      window.clearTimeout(this.updateTimeout);
    }), this.map.on("moveend", () => {
      var _this$mapboxLayer$mbM;
      (_this$mapboxLayer$mbM = this.mapboxLayer.mbMap) === null || _this$mapboxLayer$mbM === void 0 || _this$mapboxLayer$mbM.once("idle", this.onIdle);
    }));
  }

  /**
   * @override
   */
  detachFromMap(map) {
    window.clearTimeout(this.updateTimeout);
    const {
      mbMap
    } = this.mapboxLayer;
    if (mbMap) {
      mbMap.off("idle", this.onIdle);
      [this.sourceId, this.highlightSourceId].forEach(id => {
        const source = mbMap.getSource(id);
        if (source) {
          // Don't remove source just make it empty.
          // Because others layers during unmount still could rely on it.
          source.setData({
            type: "FeatureCollection",
            features: []
          });
        }
      });
    }
    super.detachFromMap(map);
  }

  /**
   * On Mapbox map load callback function. Add style sources then style layers.
   * @override
   */
  onLoad() {
    super.onLoad();
    this.updateClusterSource();
  }

  /**
   * Callback when the map is on idle state after a moveend event.
   */
  onIdle() {
    window.clearTimeout(this.updateTimeout);
    this.updateTimeout = window.setTimeout(() => {
      this.updateClusterSource();
    }, 150);
  }

  // Update sources for features with multiple POIs.
  updateClusterSource() {
    var _features;
    if (!this.visible || !this.map || !this.mapboxLayer.mbMap) {
      return;
    }
    const {
      mbMap
    } = this.mapboxLayer;
    const source = mbMap.getSource(this.sourceId);
    if (!source) {
      return;
    }
    let features;
    try {
      features = mbMap.querySourceFeatures("ch.sbb.zweitausbildung_pois", {
        sourceLayer: "ch.sbb.zweitausbildung_pois",
        filter: this.filter
      });
    } catch (e) {
      // eslint-disable-next-line no-console
      // console.error(e);
    }
    if (!((_features = features) !== null && _features !== void 0 && _features.length)) {
      return;
    }

    // Remove duplicated features.
    const ids = [];
    const uniqueFeatures = [];
    features.forEach(linee => {
      const indexOf = ids.indexOf(linee.id);
      if (indexOf === -1) {
        ids.push(linee.id);
        uniqueFeatures.push(linee);
      }
    });
    source.setData({
      type: "FeatureCollection",
      features: uniqueFeatures
    });
  }
  highlightFromPopup(feature, toggle) {
    const {
      mbMap
    } = this.mapboxLayer;
    if (!mbMap) {
      return;
    }
    const data = this.format.writeFeaturesObject(toggle ? [feature] : []);
    const source = mbMap.getSource(this.highlightSourceId);
    if (source) {
      source.setData(data);
    }

    // Launch animation
    mbMap.setPaintProperty(this.highlightSourceId, "circle-radius", toggle ? 14 : 0);
  }
}
export default ZweitausbildungPoisLayer;
//# sourceMappingURL=ZweitausbildungPoisLayer.js.map