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

/**
 * Layer for displaying blue circle on platforms 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 PlatformsLayer extends MapboxStyleLayer {
  constructor() {
    let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    const id = PLATFORMS_SOURCE_ID;
    super(_objectSpread({
      styleLayersFilter: _ref => {
        let {
          metadata
        } = _ref;
        return !!metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.PLATFORMS;
      },
      properties: {
        hideInLegend: true,
        popupComponent: "StationPopup",
        useOverlay: true,
        isQueryable: true
      }
    }, options));
    this.source = {
      id,
      type: "geojson",
      data: {
        type: "FeatureCollection",
        features: []
      }
    };
    this.onIdle = this.onIdle.bind(this);
  }

  /**
   * @override
   */
  attachToMap(map) {
    super.attachToMap(map);
    this.olListenersKeys.push(this.on("change:visible", () => {
      this.updateSource();
    }), 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) {
    const {
      mbMap
    } = this.mapboxLayer;
    if (mbMap) {
      mbMap.off("idle", this.onIdle);
      this.removeSource();
    }
    super.detachFromMap(map);
  }

  /**
   * On Mapbox map load callback function. Add style sources then style layers.
   * @override
   */
  onLoad() {
    const {
      mbMap
    } = this.mapboxLayer;
    this.platformLayers = mbMap.getStyle().layers.filter(_ref2 => {
      let {
        metadata
      } = _ref2;
      return !!metadata && metadata[MapsGeneralSubClass] === MapsGeneralSubClassValues.STOP_POSITION;
    });
    this.platformLayers = this.platformLayers.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() {
    this.updateSource();
  }

  // Query the rendered stations then add them to the source.
  updateSource() {
    const {
      mbMap
    } = this.mapboxLayer;
    const source = mbMap.getSource(this.source.id);
    if (!this.platformLayers || !source) {
      if (!source) {
        // eslint-disable-next-line no-console
        console.warn("Source with id ".concat(this.source.id, " not found in the style, impossible to display platforms polygons highlight."));
      }
      return;
    }
    const uids = []; // ['==', 'uid', '496211a5d7ec6962'];
    const pointsRendered = mbMap.queryRenderedFeatures({
      layers: this.platformLayers
    }).map((feat, index) => {
      const {
        geometry
      } = feat;
      if (geometry.type === "Polygon") {
        // if it's a polygon we store the uid for the filter.
        uids.push(feat.properties.uid);
      }
      const good = {
        id: index + 1,
        type: feat.type,
        properties: feat.properties,
        geometry
      };
      return good;
    });
    if (mbMap.getLayer(PLATFORMS_POLYGON_HIGHLIGHT_LAYER_ID)) {
      // we display only visible platorm polygons
      mbMap.setFilter(PLATFORMS_POLYGON_HIGHLIGHT_LAYER_ID, ["all", ["==", ["geometry-type"], "Polygon"], ["in", ["get", "uid"], ["literal", uids]]]);
    } else {
      // eslint-disable-next-line no-console
      console.warn("Layer with id ".concat(PLATFORMS_POLYGON_HIGHLIGHT_LAYER_ID, " not found in the style, impossible to display platforms polygons highlight."));
    }
    source.setData({
      type: "FeatureCollection",
      features: pointsRendered
    });
  }

  // Add source for stations.
  addSource() {
    if (!this.mapboxLayer.mbMap) {
      return;
    }
    const {
      mbMap
    } = this.mapboxLayer;
    const {
      id
    } = this.source;
    if (!mbMap.getSource(id)) {
      const withoutId = _objectSpread({}, this.source);
      delete withoutId.id;
      mbMap.addSource(id, withoutId);
    }
  }

  // Remove source added by addSources().
  removeSource() {
    if (!this.mapboxLayer.mbMap) {
      return;
    }
    const {
      mbMap
    } = this.mapboxLayer;
    const {
      id
    } = this.source;
    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: []
      });
    }
  }
  getFeatureInfoAtCoordinate(coordinate, options) {
    return super.getFeatureInfoAtCoordinate(coordinate, options).then(featureInfo => {
      // We want only one platform selected at a time, to avoid having duplication of data in the popup
      return _objectSpread(_objectSpread({}, featureInfo), {}, {
        features: featureInfo.features.length ? [featureInfo.features[0]] : []
      });
    });
  }
}
export default PlatformsLayer;
//# sourceMappingURL=PlatformsLayer.js.map