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 no-param-reassign */
import { MapsGeneralFloor, MapsGeneralFloorValues } from "../../utils/constants";
import MapboxStyleLayer from "../MapboxStyleLayer";

// Caching filters to reset to the original when toggle off the layer.
const filterCacheByMbStyle = {};

/**
 * Layer for visualizing station levels.
 *
 * @class
 * @param {Object} [options] Layer options.
 * @inheritdoc
 * @private
 */
class LevelLayer extends MapboxStyleLayer {
  constructor() {
    let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    super(options);
    this.level = options.level;
  }

  /**
   * Apply visibility to style layers that fits the filter function.
   */
  applyLayoutVisibility(evt) {
    const {
      visible
    } = this;
    const {
      mbMap
    } = this.mapboxLayer;
    const style = mbMap.getStyle();
    if (!mbMap || !style) {
      return;
    }

    // Since style layer can have the same id between styles,
    // we store the filters depending on the style.
    if (!filterCacheByMbStyle[style.name]) {
      filterCacheByMbStyle[style.name] = {};
    }
    const filterCache = filterCacheByMbStyle[style.name];
    const isInit = !evt;
    if (!this.styleLayersFilter) {
      return;
    }
    for (let i = 0; i < style.layers.length; i += 1) {
      var _styleLayer$metadata;
      const styleLayer = style.layers[i];
      // Return the value of the metadata geops.filter. if it exists.
      const metadata = this.styleLayersFilter(styleLayer);
      if (!metadata) {
        // eslint-disable-next-line no-continue
        continue;
      }

      // Store the initial filter value
      if (!filterCache[styleLayer.id]) {
        filterCache[styleLayer.id] = [...(styleLayer.filter || [])];
      }

      // level 2D
      if (metadata === MapsGeneralFloorValues["2D"] && this.level === MapsGeneralFloorValues["2D"]) {
        if (visible && styleLayer.layout.visibility === "none") {
          mbMap.setLayoutProperty(styleLayer.id, "visibility", "visible");
        } else if (!visible && styleLayer.layout.visibility === "visible") {
          mbMap.setLayoutProperty(styleLayer.id, "visibility", "none");
        }

        // others levels
      } else if (metadata === MapsGeneralFloorValues.LEVEL && this.level !== MapsGeneralFloorValues["2D"]) {
        // If visible apply the new level filter.
        if (visible) {
          const currentFilter = [...filterCache[styleLayer.id]];
          currentFilter[1] = ["==", ["case", ["has", "level"], ["get", "level"], 0], this.level];
          mbMap.setFilter(styleLayer.id, currentFilter);
          if (styleLayer.layout.visibility === "none") {
            mbMap.setLayoutProperty(styleLayer.id, "visibility", "visible");
          }
        } else if (!isInit && styleLayer.layout.visibility === "visible") {
          // We set the visibility to none only if others siblings level layer are also hidden.
          // it can happens when we load the topic via urls aand then we switch topic.
          // In that case change:visible events are not registered in the same order.
          if (!this.get("parent").children.find(child => child.level !== MapsGeneralFloorValues["2D"] && child.visible)) {
            mbMap.setLayoutProperty(styleLayer.id, "visibility", "none");
          }
        }
      }
      if (((_styleLayer$metadata = styleLayer.metadata) === null || _styleLayer$metadata === void 0 ? void 0 : _styleLayer$metadata[MapsGeneralFloor]) === MapsGeneralFloorValues.LEVEL_GREYOUT) {
        var _this$get;
        if (this.visible && this.level !== 0 && this.level !== MapsGeneralFloorValues["2D"]) {
          mbMap.setLayoutProperty(styleLayer.id, "visibility", "visible");
        } else if (this.visible) {
          mbMap.setLayoutProperty(styleLayer.id, "visibility", "none");
        }
        const areAllHidden = !((_this$get = this.get("parent")) !== null && _this$get !== void 0 && _this$get.children.some(l => l.visible));
        if (areAllHidden) {
          mbMap.setLayoutProperty(styleLayer.id, "visibility", "none");
        }
      }
    }
  }

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