import VectorSource from "ol/source/Vector";
import DirektverbindungenLayer from ".";

/**
 * Layer for visualizing international train connections by name
 *
 * @class
 * @param {Object} [options] Layer options.
 * @inheritdoc
 * @private
 */
class DirektverbindungenSingleLayer extends DirektverbindungenLayer {
  /**
   * This function try to find fetaures to select on loading the page based a the line name.
   */
  getFeaturesInfosFromLineName(lineName) {
    var _this$allFeatures;
    const names = lineName === null || lineName === void 0 ? void 0 : lineName.split(",");
    if (!(names !== null && names !== void 0 && names.length)) {
      return Promise.resolve([]);
    }
    const getFeatureInfo = features => {
      const found = features.filter(feature => {
        // console.log("select cartaroIds", lineName, feature.get("name"));

        return names.includes(feature.get("name")) || names.includes(String(feature.get("cartaro_id")));
      });
      return [{
        features: found,
        layer: this
      }];
    };
    if ((_this$allFeatures = this.allFeatures) !== null && _this$allFeatures !== void 0 && _this$allFeatures.length) {
      return Promise.resolve(getFeatureInfo(this.allFeatures));
    }
    const promise = new Promise(resolve => {
      this.on("sync:features", _ref => {
        let {
          features = []
        } = _ref;
        resolve(getFeatureInfo(features));
      });
    });
    return promise;
  }

  // eslint-disable-next-line class-methods-use-this
  highlight() {}
  select() {
    let features = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
    const cartaroIds = features.map(feat => feat.get("cartaro_id") || feat.get("direktverbindung_cartaro_id"));
    if (cartaroIds.length) {
      var _this$mapboxLayer3;
      // console.log("select cartaroIds", cartaroIds);

      // console.log(
      //   " this.mapboxLayer?.mbMap",
      //   this.mapboxLayer?.mbMap.getStyle(),
      // );

      const applyFilter = () => {
        var _this$mapboxLayer;
        (_this$mapboxLayer = this.mapboxLayer) === null || _this$mapboxLayer === void 0 || (_this$mapboxLayer = _this$mapboxLayer.mbMap) === null || _this$mapboxLayer === void 0 || (_this$mapboxLayer = _this$mapboxLayer.getStyle()) === null || _this$mapboxLayer === void 0 || (_this$mapboxLayer = _this$mapboxLayer.layers) === null || _this$mapboxLayer === void 0 || _this$mapboxLayer.forEach(layer => {
          if (this.styleLayersFilter(layer)) {
            var _this$mapboxLayer2;
            (_this$mapboxLayer2 = this.mapboxLayer) === null || _this$mapboxLayer2 === void 0 || (_this$mapboxLayer2 = _this$mapboxLayer2.mbMap) === null || _this$mapboxLayer2 === void 0 || _this$mapboxLayer2.setFilter(layer.id, ["all", ["!=", ["get", "data_variant"], "night_and_day"], ["any", ...cartaroIds.map(id => ["==", ["get", "cartaro_id"], id]), ...cartaroIds.map(id => ["==", ["get", "direktverbindung_cartaro_id"], id])]]);
            this.visible = true;
          }
        });
      };
      const style = (_this$mapboxLayer3 = this.mapboxLayer) === null || _this$mapboxLayer3 === void 0 || (_this$mapboxLayer3 = _this$mapboxLayer3.mbMap) === null || _this$mapboxLayer3 === void 0 ? void 0 : _this$mapboxLayer3.getStyle();
      if (!style) {
        var _this$mapboxLayer4;
        (_this$mapboxLayer4 = this.mapboxLayer) === null || _this$mapboxLayer4 === void 0 || (_this$mapboxLayer4 = _this$mapboxLayer4.mbMap) === null || _this$mapboxLayer4 === void 0 || _this$mapboxLayer4.once("idle", () => {
          applyFilter();
        });
      } else {
        applyFilter();
      }
      const source = new VectorSource({
        features
      });
      const extent = source.getExtent();
      this.map.getView().fit(extent, {
        padding: [100, 100, 100, 100]
      });
    } else {
      this.visible = false;
    }
  }

  // eslint-disable-next-line class-methods-use-this
  getCurrentLayer() {}

  // eslint-disable-next-line class-methods-use-this
  highlightStation() {}
}
export default DirektverbindungenSingleLayer;
//# sourceMappingURL=DirektverbindungenSingleLayer.js.map