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 { MapsGeneralClass, MapsGeneralClassValues, MapsGeneralSubClass, MapsGeneralSubClassValues, MapsTrafimageFilter, MapsTrafimageFilterValues, STATIONS_SOURCE_ID } from "../../utils/constants";
import MapboxStyleLayer from "../MapboxStyleLayer";

/**
 * Layer for displaying blue stations circle on hover.
 * 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 StationsLayer extends MapboxStyleLayer {
  constructor() {
    let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    super(_objectSpread({
      styleLayersFilter: _ref => {
        let {
          metadata
        } = _ref;
        return !!metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.STATIONS;
      },
      properties: {
        hideInLegend: true,
        popupComponent: "StationPopup",
        useOverlay: true,
        isQueryable: true
      }
    }, options));
    this.sourceId = STATIONS_SOURCE_ID;
    this.onIdle = this.onIdle.bind(this);
    this.ready = false;
  }

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

  /**
   * @override
   */
  detachFromMap(map) {
    super.detachFromMap(map);
    const {
      mbMap
    } = this.mapboxLayer;
    if (mbMap) {
      mbMap.off("idle", this.onIdle);
      this.removeSource();
    }
  }

  /**
   * On Mapbox map load callback function. Add style sources then style layers.
   * @override
   */
  onLoad() {
    const {
      mbMap
    } = this.mapboxLayer;
    this.osmPointsLayers = mbMap.getStyle().layers.filter(_ref2 => {
      let {
        metadata
      } = _ref2;
      return metadata && metadata[MapsGeneralClass] === MapsGeneralClassValues.STATIONS && metadata[MapsGeneralSubClass] !== MapsGeneralSubClassValues.STOP_POSITION;
    }).map(layer => layer.id);
    this.addSource();
    super.onLoad();
    this.updateSource();
    mbMap.once("idle", this.onIdle);
  }

  /**
   * Callback when the map is on idle state after a moveend event.
   */
  onIdle() {
    var _this$mapboxLayer$mbM2;
    this.updateSource();

    // We warn the permalink that new data have been rendered.
    (_this$mapboxLayer$mbM2 = this.mapboxLayer.mbMap) === null || _this$mapboxLayer$mbM2 === void 0 || _this$mapboxLayer$mbM2.once("idle", () => {
      this.ready = true;

      // New data are rendered
      this.dispatchEvent({
        type: "datarendered",
        target: this
      });
    });
  }

  // Query the rendered stations then add them to the source.
  updateSource() {
    const {
      mbMap
    } = this.mapboxLayer;
    const source = mbMap === null || mbMap === void 0 ? void 0 : mbMap.getSource(this.sourceId);
    if (!this.osmPointsLayers || !source) {
      if (!source) {
        // eslint-disable-next-line no-console
        console.warn("Source for stations layer not found, can't update source");
      }
      return;
    }
    const osmPointsRendered = mbMap.queryRenderedFeatures({
      layers: this.osmPointsLayers
    }).map((feat, index) => {
      const good = {
        id: index + 1,
        type: feat.type,
        properties: feat.properties,
        geometry: feat.geometry
      };
      return good;
    });
    source.setData({
      type: "FeatureCollection",
      features: osmPointsRendered
    });
  }

  // Add source for stations.
  addSource() {
    if (!this.mapboxLayer.mbMap) {
      return;
    }
    const {
      mbMap
    } = this.mapboxLayer;
    if (!mbMap.getSource(this.sourceId)) {
      mbMap.addSource(this.sourceId, {
        type: "geojson",
        data: {
          type: "FeatureCollection",
          features: []
        }
      });
    }
  }

  // Remove source added by addSources().
  removeSource() {
    if (!this.mapboxLayer.mbMap) {
      return;
    }
    const {
      mbMap
    } = this.mapboxLayer;
    const source = mbMap.getSource(this.sourceId);
    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: []
      });
    }
  }

  // Deactivate highlight and select style.
  // eslint-disable-next-line class-methods-use-this
  highlight() {}

  // eslint-disable-next-line class-methods-use-this
  select() {}
  clone(newOptions) {
    return new StationsLayer(_objectSpread(_objectSpread({}, this.options), newOptions));
  }
}
export default StationsLayer;
//# sourceMappingURL=StationsLayer.js.map