import { Layer } from "mobility-toolbox-js/ol";
import MapboxStyleLayer from "../../layers/MapboxStyleLayer";
import TrafimageMapboxLayer from "../../layers/TrafimageMapboxLayer";
import StationsLayer from "../../layers/StationsLayer";
import PlatformsLayer from "../../layers/PlatformsLayer";
import TralisLayer from "../../layers/TralisLayer";
import { LevelLayer } from "../../layers";
import { BAHNHOFPLAENE_LAYER_NAME, DATA_LAYER_NAME, FLOOR_LEVELS, GESCHOSSE_LAYER_NAME, MapsGeneralFloor, MapsTrafimageFilter, MapsTrafimageFilterValues, NETZKARTE_AERIAL_LAYER_NAME, NETZKARTE_DARK_LAYER_NAME, NETZKARTE_LAYER_NAME, PLATFORMS_LAYER_NAME, STATIONS_LAYER_NAME, STOPO_LANDESKARTE_GRAU_LAYER_NAME, STOPO_LANDESKARTE_LAYER_NAME } from "../../utils/constants";

// eslint-disable-next-line import/prefer-default-export
export const getNetzkarteLayers = () => {
  const dataLayer = new TrafimageMapboxLayer({
    name: DATA_LAYER_NAME,
    visible: true,
    zIndex: -1,
    // Add zIndex as the MapboxLayer would block tiled layers (buslines)
    style: "basemap_bright_ch.sbb.netzkarte",
    properties: {
      hideInLegend: true
    },
    mapOptions: {
      preserveDrawingBuffer: true
    }
  });
  const netzkarteLayer = new MapboxStyleLayer({
    name: NETZKARTE_LAYER_NAME,
    key: "ch.sbb.netzkarte",
    group: "baseLayer",
    properties: {
      isBaseLayer: true,
      previewImage: "ch.sbb.netzkarte",
      hasLevels: true,
      hideInLegend: true
    },
    visible: false,
    mapboxLayer: dataLayer,
    style: "basemap_bright_ch.sbb.netzkarte"
  });
  const netzkarteNight = new MapboxStyleLayer({
    name: NETZKARTE_DARK_LAYER_NAME,
    key: "ch.sbb.netzkarte.dark",
    group: "baseLayer",
    properties: {
      isBaseLayer: true,
      previewImage: "ch.sbb.netzkarte.dark",
      hasLevels: true,
      hideInLegend: true
    },
    visible: false,
    mapboxLayer: dataLayer,
    style: "basemap_dark_ch.sbb.netzkarte"
  });
  const netzkarteAerial = new MapboxStyleLayer({
    name: NETZKARTE_AERIAL_LAYER_NAME,
    key: "ch.sbb.netzkarte.luftbild.group",
    group: "baseLayer",
    properties: {
      isBaseLayer: true,
      previewImage: "ch.sbb.netzkarte.luftbild.group"
    },
    visible: false,
    mapboxLayer: dataLayer,
    style: "basemap_aerial_sbbkey_ch.sbb.netzkarte"
  });
  const swisstopoLandeskarte = new MapboxStyleLayer({
    name: STOPO_LANDESKARTE_LAYER_NAME,
    key: "ch.sbb.netzkarte.landeskarte",
    group: "baseLayer",
    properties: {
      isBaseLayer: true,
      previewImage: "ch.sbb.netzkarte.landeskarte"
    },
    visible: false,
    mapboxLayer: dataLayer,
    style: "ch.swisstopo.backgrounds_ch.sbb.netzkarte.swisstopo",
    styleLayersFilter: _ref => {
      let {
        metadata
      } = _ref;
      return !!metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.PIXELKARTE_FARBE;
    }
  });
  const swisstopoLandeskarteGrau = new MapboxStyleLayer({
    name: STOPO_LANDESKARTE_GRAU_LAYER_NAME,
    key: "ch.sbb.netzkarte.landeskarte.grau",
    group: "baseLayer",
    properties: {
      isBaseLayer: true,
      previewImage: "ch.sbb.netzkarte.landeskarte.grau"
    },
    visible: false,
    mapboxLayer: dataLayer,
    style: "ch.swisstopo.backgrounds_ch.sbb.netzkarte.swisstopo",
    styleLayersFilter: _ref2 => {
      let {
        metadata
      } = _ref2;
      return !!metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.PIXELKARTE_GRAU;
    }
  });
  const passagierfrequenzen = new MapboxStyleLayer({
    name: "ch.sbb.bahnhoffrequenzen",
    visible: false,
    mapboxLayer: dataLayer,
    styleLayersFilter: _ref3 => {
      let {
        metadata
      } = _ref3;
      return !!metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.PASSAGIER_FREQ;
    },
    properties: {
      isQueryable: true,
      hasInfos: true,
      layerInfoComponent: "PassagierFrequenzenLayerInfo",
      popupComponent: "PassagierFrequenzenPopup",
      useOverlay: true,
      dataLink: "https://reporting.sbb.ch/bahnhoefe",
      dataLinkPortalName: "Statistikportal"
    }
  });
  const interactivePlans = new MapboxStyleLayer({
    name: "ch.sbb.bahnhofplaene.interaktiv",
    visible: false,
    mapboxLayer: dataLayer,
    styleLayersFilter: _ref4 => {
      let {
        metadata
      } = _ref4;
      return !!metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.INTERAKTIV;
    },
    group: "bahnhofplaene",
    properties: {
      isQueryable: true,
      hasInfos: true,
      description: "ch.sbb.bahnhofplaene.interaktiv-desc",
      popupComponent: "StationPopup",
      useOverlay: true
    }
  });
  const printPlans = new MapboxStyleLayer({
    name: "ch.sbb.bahnhofplaene.printprodukte",
    visible: false,
    mapboxLayer: dataLayer,
    styleLayersFilter: _ref5 => {
      let {
        metadata
      } = _ref5;
      return !!metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.PRINTPRODUKTE;
    },
    group: "bahnhofplaene",
    properties: {
      isQueryable: true,
      hasInfos: true,
      description: "ch.sbb.bahnhofplaene.printprodukte-desc",
      popupComponent: "StationPopup",
      useOverlay: true
    }
  });
  const bahnhofplaene = new Layer({
    name: BAHNHOFPLAENE_LAYER_NAME,
    visible: false,
    children: [interactivePlans.clone(), printPlans.clone()],
    properties: {
      hasInfos: true,
      description: "ch.sbb.bahnhofplaene-desc",
      dataLink: "https://data.sbb.ch/explore/dataset/haltestelle-karte-trafimage/information/",
      dataService: true
    }
  });
  const punctuality = new TralisLayer({
    name: "ch.sbb.puenktlichkeit",
    visible: false,
    properties: {
      hasAccessibility: true,
      hasInfos: true,
      layerInfoComponent: "PunctualityLayerInfo"
    }
  });

  // if a permalink parameter is defined (see TralisLayer), we don't display children.
  if (!punctuality.permalinkFilter) {
    punctuality.children = [new Layer({
      name: "ch.sbb.puenktlichkeit-gondola",
      visible: false,
      properties: {
        mots: ["gondola"]
      }
    }), new Layer({
      name: "ch.sbb.puenktlichkeit-funicular",
      visible: false,
      properties: {
        mots: ["funicular", "cablecar"]
      }
    }), new Layer({
      name: "ch.sbb.puenktlichkeit-ferry",
      visible: false,
      properties: {
        mots: ["ferry"]
      }
    }), new Layer({
      name: "ch.sbb.puenktlichkeit-bus",
      visible: false,
      properties: {
        mots: ["bus"]
      }
    }), new Layer({
      name: "ch.sbb.puenktlichkeit-tram",
      visible: false,
      properties: {
        mots: ["tram", "subway"]
      }
    }), new Layer({
      name: "ch.sbb.puenktlichkeit-nv",
      visible: false,
      properties: {
        filter: _ref6 => {
          let {
            properties
          } = _ref6;
          const {
            type,
            line
          } = properties;
          return type === "rail" && !/(IR|IC|EC|RJX|TGV)/.test(line === null || line === void 0 ? void 0 : line.name);
        }
      }
    }), new Layer({
      name: "ch.sbb.puenktlichkeit-fv",
      visible: false,
      properties: {
        filter: _ref7 => {
          let {
            properties
          } = _ref7;
          const {
            type,
            line
          } = properties;
          return type === "rail" && /(IR|IC|EC|RJX|TGV)/.test(line === null || line === void 0 ? void 0 : line.name);
        }
      }
    })];
    punctuality.children.forEach(layer => {
      layer.on("change:visible", () => {
        let mots = [];
        let filters = [];
        punctuality.children.forEach(child => {
          if (child.visible) {
            if (child.get("mots")) {
              mots = mots.concat(child.get("mots"));
            }
            if (child.get("filter")) {
              filters.push(child.get("filter"));
            }
          }
        });
        filters = [trajectory => {
          return mots.includes(trajectory.properties.type);
        }, ...filters];
        punctuality.filter = trajectory => {
          return filters.find(filterFunc => filterFunc(trajectory));
        };
        punctuality.renderTrajectories();
      });
    });
  }
  const stationsLayer = new StationsLayer({
    name: STATIONS_LAYER_NAME,
    mapboxLayer: dataLayer
  });
  const platformsLayer = new PlatformsLayer({
    name: PLATFORMS_LAYER_NAME,
    mapboxLayer: dataLayer
  });
  const buslines = new MapboxStyleLayer({
    name: "ch.sbb.netzkarte.buslinien",
    mapboxLayer: dataLayer,
    styleLayersFilter: _ref8 => {
      let {
        metadata
      } = _ref8;
      return !!metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.BUS;
    },
    visible: false,
    properties: {
      isQueryable: map => {
        return map.getView().getZoom() >= 15;
      },
      hasInfos: true,
      layerInfoComponent: "BuslinesLayerInfo",
      popupComponent: "BusLinePopup",
      useOverlay: true
    }
  });
  buslines.on("change:visible", () => {
    var _dataLayer$mbMap;
    (_dataLayer$mbMap = dataLayer.mbMap) === null || _dataLayer$mbMap === void 0 || _dataLayer$mbMap.once("idle", () => stationsLayer.updateSource());
  });
  const gemeindegrenzen = new MapboxStyleLayer({
    name: "ch.sbb.ch_gemeinden",
    mapboxLayer: dataLayer,
    visible: false,
    styleLayersFilter: _ref9 => {
      let {
        metadata
      } = _ref9;
      return metadata && metadata[MapsTrafimageFilter] === MapsTrafimageFilterValues.MUNICIPALITY_BORDERS;
    },
    properties: {
      hasInfos: true,
      description: "ch.sbb.ch_gemeinden-desc"
    }
  });
  const geschosseLayer = new Layer({
    name: GESCHOSSE_LAYER_NAME,
    visible: true,
    properties: {
      hideInLayerTree: true
    }
  });
  geschosseLayer.children = FLOOR_LEVELS.map(level => {
    return new LevelLayer({
      name: "ch.sbb.geschosse".concat(level),
      visible: level === "2D",
      mapboxLayer: dataLayer,
      styleLayersFilter: _ref10 => {
        let {
          metadata
        } = _ref10;
        return metadata && metadata[MapsGeneralFloor];
      },
      level,
      group: "ch.sbb.geschosse-layer",
      properties: {
        parent: geschosseLayer,
        hideInLayerTree: true
      }
    });
  });
  const defaultBaseLayers = [dataLayer, netzkarteLayer, netzkarteNight, netzkarteAerial, swisstopoLandeskarte, swisstopoLandeskarteGrau];
  return [...defaultBaseLayers, punctuality, stationsLayer, platformsLayer, passagierfrequenzen, gemeindegrenzen, buslines, bahnhofplaene.clone({
    children: [interactivePlans.clone(), printPlans.clone()] // There is a bug in MTJS that breaks the radio buttons, so we need to clone the layers.
  }), geschosseLayer];
};
//# sourceMappingURL=index.js.map