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 { MapboxStyleLayer as MTMapboxStyleLayer } from "mobility-toolbox-js/ol";

/**
 * Layer for visualizing information about stations (default) or airports.
 * The popup contains links to station plans, station coordinates
 * and links to timetable, services, shopping, handicap information.
 *
 * <img src="img/layers/stationsLayer/layer.png" alt="Layer preview" title="Layer preview">
 *
 * Extends {@link https://mobility-toolbox-js.netlify.app/api/class/src/ol/layers/Layer%20js~Layer%20html}
 * @class
 * @param {Object} [options] Layer options.
 * @inheritdoc
 * @private
 */
class MapboxStyleLayer extends MTMapboxStyleLayer {
  constructor() {
    let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    super(_objectSpread(_objectSpread({}, options), {}, {
      isHoverActive: false,
      isClickActive: false
    }));
    this.style = options.style;
    this.filters = options.filters;
    this.hidePopupFunc = options.hidePopup;
    this.hoveredFeatures = []; // When the feature is under the mouse and selectable
    this.highlightedFeatures = []; // When the clicked is clicked abnd available in feature information.
    this.selectedFeatures = []; // When the feature properties is displayed in feature information.
    this.onMouseLeave = () => {
      this.hover();
    };
  }
  attachToMap(map) {
    var _this$map;
    super.attachToMap(map);
    this.olListenersKeys.push(this.on("change:visible", () => {
      if (!this.visible) {
        this.cleanFeatureState();
      }
    }));
    (_this$map = this.map) === null || _this$map === void 0 || (_this$map = _this$map.getTargetElement()) === null || _this$map === void 0 || _this$map.addEventListener("mouseleave", this.onMouseLeave);
  }
  detachFromMap(map) {
    var _this$map2;
    (_this$map2 = this.map) === null || _this$map2 === void 0 || (_this$map2 = _this$map2.getTargetElement()) === null || _this$map2 === void 0 || _this$map2.removeEventListener("mouseleave", this.onMouseLeave);
    super.detachFromMap(map);
  }
  cleanFeatureState() {
    // Cleaning order is important to avoid having a feature with a bad hover state
    this.select();
    this.highlight();
    this.hover();
    this.hoveredFeatures = [];
    this.highlightedFeatures = [];
    this.selectedFeatures = [];
  }
  hidePopup(feat, layer, featureInfo) {
    return this.hidePopupFunc && this.hidePopupFunc(feat, layer, featureInfo);
  }

  /**
   * Apply visibility to style layers that fits the filter function.
   * @private
   */
  applyLayoutVisibility(evt) {
    const {
      visible
    } = this;
    const {
      mbMap
    } = this.mapboxLayer;
    if (!mbMap) {
      return;
    }
    const style = mbMap.getStyle();
    if (!style) {
      return;
    }
    if (this.style && visible) {
      this.mapboxLayer.forceUrl = this.forceUrl;
      this.mapboxLayer.setStyle(this.style);
    }
    super.applyLayoutVisibility(evt);
  }
  getFeatureInfoAtCoordinate() {
    return super.getFeatureInfoAtCoordinate(...arguments).then(featureInfo => {
      let {
        features
      } = featureInfo;
      const filter = this.get("featureInfoFilter");
      if (filter) {
        features = featureInfo.features.filter(filter);
      }
      return _objectSpread(_objectSpread({}, featureInfo), {}, {
        features,
        layer: this
      });
    });
  }

  /**
   * Set the hover state to true/false to a list of features.
   * @param {Array<ol/Feature~Feature>} features
   * @param {boolean} state Is the feature hovered
   * @private
   */
  setFeatureState(features, state) {
    var _this$mapboxLayer;
    if (!((_this$mapboxLayer = this.mapboxLayer) !== null && _this$mapboxLayer !== void 0 && _this$mapboxLayer.mbMap)) {
      return;
    }
    const {
      mbMap
    } = this.mapboxLayer;
    if (!features || !mbMap) {
      return;
    }
    features.forEach(feature => {
      const {
        source,
        sourceLayer
      } = feature.get("mapboxFeature") || {};
      if (!source && !sourceLayer || !feature.getId()) {
        if (!feature.getId()) {
          // eslint-disable-next-line no-console
          console.warn("No feature's id found. To use the feature state functionnality, tiles must be generated with --generate-ids. See https://github.com/mapbox/tippecanoe#adding-calculated-attributes.", feature.getId(), feature.getProperties());
        }
        return;
      }
      mbMap.setFeatureState({
        id: feature.getId(),
        source,
        sourceLayer
      }, state);
    });
  }

  /**
   * Set the hover state to true/false to a list of features.
   * @param {Array<ol/Feature~Feature>} features
   * @param {boolean} state Is the feature hovered
   * @private
   */
  setHoverState(features, hoverState) {
    this.setFeatureState(features, {
      hover: hoverState
    });
  }

  /**
   * Highlight a list of features, setting the hover state to true to a list of features.
   * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
   * @private
   */
  hover() {
    var _this$hoveredFeatures;
    let features = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    // Filter out highlighted and selected features
    const filtered = ((_this$hoveredFeatures = this.hoveredFeatures) === null || _this$hoveredFeatures === void 0 ? void 0 : _this$hoveredFeatures.filter(feature => ![...(this.selectedFeatures || []), ...(this.highlightedFeatures || [])].map(feat => feat.getId()).includes(feature.getId()))) || [];
    this.setHoverState(filtered, false);
    this.hoveredFeatures = features;
    this.setHoverState(this.hoveredFeatures, true);
  }

  /**
   * Highlight a list of features, setting the hover state to true to a list of features.
   * @param {Array<ol/Feature~Feature>} [features=[]] Features to highlight.
   * @private
   */
  highlight() {
    var _this$highlightedFeat;
    let features = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    // Filter out selected features
    const filtered = ((_this$highlightedFeat = this.highlightedFeatures) === null || _this$highlightedFeat === void 0 ? void 0 : _this$highlightedFeat.filter(feature => !(this.selectedFeatures || []).map(feat => feat.getId()).includes(feature.getId()))) || [];
    this.setHoverState(filtered, false);
    this.highlightedFeatures = features;
    this.setHoverState(this.highlightedFeatures, true);
  }

  /**
   * Apply a select style to alist of features.
   * @param {Array<ol/Feature~Feature>} [features=[]] Features to select.
   * @private
   */
  // eslint-disable-next-line class-methods-use-this
  select() {
    // By default no select state is applied
    // this.setFeatureState(this.selectedFeatures || [], {
    //   selected: false,
    // });
    // this.selectedFeatures = features;
    // this.setFeatureState(this.selectedFeatures || [], {
    //   selected: true,
    // });
  }

  /**
   * Create exact copy of the MapboxLayer
   * @returns {MapboxLayer} MapboxLayer
   */
  clone(newOptions) {
    return new MapboxStyleLayer(_objectSpread(_objectSpread({}, this.options), newOptions));
  }
}
export default MapboxStyleLayer;
//# sourceMappingURL=MapboxStyleLayer.js.map