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 Feature from "ol/Feature";
import { DV_DAY_LAYER_KEY, DV_NIGHT_LAYER_KEY } from "../../config/ch.sbb.direktverbindungen";
import parseDvFeatures from "../../config/ch.sbb.direktverbindungen/dvParseFeatures";
import { getId } from "../../utils/removeDuplicateFeatures";
import { GTTOS_LAYER_KEY } from "../../config/ch.sbb.sts";

/**
 * Rearrange the features for the popup
 */
export const parseFeaturesInfos = function (infos) {
  let tours = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  const infoFeatures = {};
  let featuresForPopup = [];

  // We order features by layer's name
  infos.forEach(info => {
    const {
      layer,
      features
    } = info;
    features.forEach(feature => {
      feature.set("layer", layer);
      infoFeatures[layer.key] = infoFeatures[layer.key] || [];
      infoFeatures[layer.key].push(feature);
    });
  });

  // Then we put all direktverbindung features
  if (infoFeatures[DV_DAY_LAYER_KEY] || infoFeatures[DV_NIGHT_LAYER_KEY]) {
    parseDvFeatures([...(infoFeatures[DV_DAY_LAYER_KEY] || []), ...(infoFeatures[DV_NIGHT_LAYER_KEY] || [])]).forEach(feature => {
      featuresForPopup.push(feature);
    });
  }

  // Then we display the gttos features if a validity text has not been
  // found with the poi highlights.
  if (infoFeatures[GTTOS_LAYER_KEY]) {
    const [feature] = [...(infoFeatures[GTTOS_LAYER_KEY] || [])];
    const layerName = feature.get("layer").key;
    // Read routes for gttos or panorama layers
    const propertyName = "route_names_gttos";
    feature.set("routeProperty", propertyName);
    const routeNames = (feature.get(propertyName) || "").split(",");
    const toursByRoute = {};

    // For each route name we create a new feature.
    // eslint-disable-next-line
    routeNames.forEach(routeName => {
      if (/Optional Shortcut/.test(routeName)) {
        featuresForPopup.push(new Feature({
          title: "Optional Shortcut",
          routeProperty: propertyName
        }));
      } else {
        toursByRoute[routeName] = tours.find(tour => tour.title === routeName.trim());
        const clone = feature.clone();
        clone.setProperties(_objectSpread(_objectSpread(_objectSpread({}, feature.getProperties()), toursByRoute[routeName]), {}, {
          id: getId(feature)
        }));
        if (toursByRoute[routeName]) {
          featuresForPopup = featuresForPopup.concat(clone);
        } else {
          // eslint-disable-next-line no-console
          console.warn("Layer ".concat(layerName, ": There is no info for the tour '").concat(routeName, "' "), clone.getProperties());
        }
      }
    });

    // Add a feature for Optional Shortcut which have not pois
    if (/Optional Shortcut/.test(feature.get("title"))) {
      featuresForPopup.push(new Feature({
        title: "Optional Shortcut"
      }));
    }
  }

  // Remove duplicate routes (at intersections)
  const routeNames = [];
  featuresForPopup = featuresForPopup.filter(f => {
    const title = f.get("title");
    if (!title || routeNames.indexOf(title) === -1) {
      routeNames.push(title);
      return true;
    }
    return false;
  });
  return featuresForPopup;
};
export default parseFeaturesInfos;
//# sourceMappingURL=stsParseFeatureInfo.js.map