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); }
/* eslint-disable class-methods-use-this */
import { MapboxStyleLayer } from "mobility-toolbox-js/ol";
import { Feature } from "ol";

/**
 * Layer for kilometrage popup
 * 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 KilometrageLayer extends MapboxStyleLayer {
  constructor() {
    let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    super(_objectSpread(_objectSpread({
      name: "ch.sbb.kilometrage",
      key: "ch.sbb.kilometrage",
      queryRenderedLayersFilter: _ref => {
        let {
          type,
          source
        } = _ref;
        return type === "line" && source === "base";
      },
      visible: true
    }, options), {}, {
      properties: _objectSpread({
        isQueryable: true,
        hideInLegend: true,
        useOverlay: false,
        popupComponent: "KilometragePopup"
      }, (options || {}).properties)
    }));
    this.abortController = new AbortController();
  }
  fetchKilometrage(coordinate, lines, generalization) {
    var _this$abortController;
    (_this$abortController = this.abortController) === null || _this$abortController === void 0 || _this$abortController.abort();
    this.abortController = new AbortController();
    return fetch("".concat(this.searchUrl, "/search/measure?coords=").concat(coordinate, "&generalization_level=").concat(generalization, "&lines=").concat(lines.toString()), {
      signal: this.abortController.signal
    }).then(data => data.json()).then(data => {
      if (data.error || data.detail || !data.length) {
        return {
          features: [],
          layer: this,
          coordinate
        };
      }
      return {
        features: [new Feature({
          lines: data
        })],
        layer: this,
        coordinate
      };
    }).catch(error => {
      // eslint-disable-next-line no-console
      console.error("Could not fetch kilometrage data", error);
      return {
        features: [],
        layer: this,
        coordinate
      };
    });
  }
  getFeatureInfoAtCoordinate(coordinate, eventType) {
    return super.getFeatureInfoAtCoordinate(coordinate).then(info => {
      var _features$find;
      const {
        features
      } = info;
      const lines = features.reduce((all, current) => {
        const lineNumber = current.get("line_number");
        return lineNumber && !all.includes(lineNumber) ? [...all, lineNumber] : all;
      }, []);
      const generalization = (_features$find = features.find(feat => feat.get("line_number"))) === null || _features$find === void 0 ? void 0 : _features$find.get("generalization_level");
      return eventType === "singleclick" && lines.length ? this.fetchKilometrage(coordinate, lines, generalization) : {
        features: lines.length ? [new Feature()] : [],
        // Return empty feature for cursor style
        layer: this,
        coordinate
      };
    });
  }
  setSearchUrl(searchUrl) {
    this.searchUrl = searchUrl;
  }
  highlight() {} // Omit highlight

  select() {} // Omit select
}
export default KilometrageLayer;
//# sourceMappingURL=KilometrageLayer.js.map