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 from "../MapboxStyleLayer";
import lines from "./lines";
import getStyleLayers from "./getStyleLayers";

/**
 * Layer for zweitausbildung routes
 * 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 ZweitausbildungRoutesLayer extends MapboxStyleLayer {
  constructor() {
    let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    const {
      property
    } = options.properties.zweitausbildung || {};
    super(_objectSpread(_objectSpread({}, options), {}, {
      styleLayers: [],
      beforeId: "ch.sbb.zweitausbildung_stations.aufbau.not.border"
    }));
    this.property = property;
    this.sourceId = "ch.sbb.zweitausbildung";
    this.sourceLayer = "ch.sbb.zweitausbildung";
  }

  /**
   * On Mapbox map load callback function. Add the zweitausbildung data source then style layers.
   * @ignore
   */
  async onLoad() {
    const {
      mbMap
    } = this.mapboxLayer;

    // if the selected lines as extra sources we add it now, before adding stylelayers
    Object.entries(lines).forEach(_ref => {
      let [key, {
        property,
        extraSources
      }] = _ref;
      if (property !== this.property || !extraSources) {
        return;
      }
      Object.entries(extraSources).forEach(_ref2 => {
        let [sourceKey, sourceToAdd] = _ref2;
        if (!mbMap.getSource(sourceKey)) {
          // First we add a property to the features so the lines will be automatically highlighted.
          sourceToAdd.data.features.forEach(feat => {
            // eslint-disable-next-line no-param-reassign
            feat.properties[this.property] = key;
          });
          mbMap.addSource(sourceKey, sourceToAdd);
        }
      });
    });

    // We need to get the tileJson file of this source to have the list of
    // possible values for attribute hauptlinie and turistische_linie.
    // These values are generated by a script during tiles generation.
    const source = mbMap.getSource(this.sourceId);
    if (!source || !source.url) {
      return;
    }
    const tileJson = await fetch(source.url).then(response => response.json());
    this.styleLayers = getStyleLayers(this, tileJson);

    // We should not have to define again styleLayersFilter.
    // it's a bug in mbt, it should be fixed in next version.
    this.styleLayersFilter = styleLayer => {
      var _this$styleLayers;
      return !!((_this$styleLayers = this.styleLayers) !== null && _this$styleLayers !== void 0 && _this$styleLayers.find(sl => styleLayer.id === sl.id));
    };
    super.onLoad();
  }
}
export default ZweitausbildungRoutesLayer;
//# sourceMappingURL=ZweitausbildungRoutesLayer.js.map