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 React from "react";
import { Typography } from "@mui/material";
import { Style, Circle, Fill, Stroke as OLStroke } from "ol/style";
import { MultiLineString, GeometryCollection, MultiPoint, Point } from "ol/geom";
import GeoJSON from "ol/format/GeoJSON";
import Search from "../Search";
import getGreaterNumber from "../../utils/getGreaterNumber";
const lineMeasuresRegExp = /([0-9]*)\s*([0-9]+\.?[0-9]*)-([0-9]*\.?[0-9]*)/;
const lineKilometerRegExp = /^([0-9]+)(\s)(\+)?(\s+)?([0-9]+(\.[0-9]+)?)$/;
const color = "rgba(0,61,155,0.5)";
const genLevelByMapRes = {
  21685.61508959875: 5,
  // zoom 0
  611.49622628141: 10,
  // zoom 8
  305.748113140705: 30,
  // zoom 9
  76.43702828517625: 100,
  // zoom 11
  9.554628535647032: 150 // zoom 14, the 150 generalization geometry is the same as the 100.
};
const format = new GeoJSON();
class Lines extends Search {
  constructor() {
    super();
    this.dataProjection = "EPSG:3857";
    this.getGeneralizedGeometry = (feature, resolution) => {
      const dataRes = getGreaterNumber(resolution, Object.keys(genLevelByMapRes));
      const generalizationLevel = genLevelByMapRes[dataRes];
      const generalizedGeometry = feature.get("geoms") && feature.get("geoms")[generalizationLevel];
      return generalizedGeometry;
    };
    this.highlightStyle = (feature, resolution) => {
      const generalizedGeometry = this.getGeneralizedGeometry(feature, resolution);
      const geometry = generalizedGeometry ? format.readGeometry(generalizedGeometry, {
        dataProjection: this.dataProjection,
        featureProjection: "EPSG:3857"
      }) : feature.getGeometry();
      return geometry instanceof MultiLineString || geometry instanceof GeometryCollection ? new Style({
        geometry,
        stroke: new OLStroke({
          color,
          width: 10
        }),
        image: geometry.getGeometries && geometry.getGeometries().find(geom => geom instanceof MultiPoint || geom instanceof Point) && new Circle({
          radius: 10,
          fill: new Fill({
            color
          })
        })
      }) : new Style({
        geometry,
        image: new Circle({
          radius: 10,
          fill: new Fill({
            color
          })
        })
      });
    };
    this.highlightStyle = this.highlightStyle.bind(this);
  }
  select(item, map) {
    if (!item || !map) {
      return item;
    }
    // Lines have generalization levels, so we need to adapt the geometry to the map resolution.
    const newItem = _objectSpread({}, item);
    newItem.geometry = this.getGeneralizedGeometry(format.readFeature(item), map.getView().getResolutionForZoom(14));
    return newItem;
  }

  // eslint-disable-next-line class-methods-use-this
  search(value) {
    let params = "line=".concat(encodeURIComponent(value));
    if (lineKilometerRegExp.test(value)) {
      const [, line,,,, km] = value.match(lineKilometerRegExp);
      params = "line=".concat(line, "&km=").concat(km);
    }
    if (lineMeasuresRegExp.test(value)) {
      const [, line, km, kmEnd] = value.match(lineMeasuresRegExp);
      params = "line=".concat(line, "&km=").concat(km, "&km_end=").concat(kmEnd);
    }
    if (this.abortController) {
      this.abortController.abort();
    }
    this.abortController = new AbortController();
    const {
      signal
    } = this.abortController;
    return fetch("".concat(this.searchUrl, "/search/lines?").concat(params), {
      signal
    }).then(data => data.json()).then(featureCollection => {
      return (featureCollection === null || featureCollection === void 0 ? void 0 : featureCollection.features) || [];
    }).catch(() => {
      return [];
    });
  }
  render(_ref) {
    let {
      properties
    } = _ref;
    const {
      t
    } = this.props;
    return /*#__PURE__*/React.createElement("div", {
      className: "wkp-search-suggestion-subtitled"
    }, /*#__PURE__*/React.createElement(Typography, null, "".concat(t("Linie"), " ").concat(properties.linie)), properties.name ? /*#__PURE__*/React.createElement(Typography, {
      variant: "subtitle1"
    }, properties.name, properties.start !== properties.end && /*#__PURE__*/React.createElement("div", {
      style: {
        color: "#999"
      }
    }, t("Kilometer"), " ", properties.start, properties.end ? "-".concat(properties.end) : "")) : null);
  }

  // eslint-disable-next-line class-methods-use-this
  value(_ref2) {
    let {
      properties
    } = _ref2;
    return properties.start !== properties.end ? "".concat(properties.linie, " ").concat(!properties.end ? "+" : "").concat(properties.start).concat(properties.end ? "-".concat(properties.end) : "") : "".concat(properties.linie);
  }
}
export default Lines;
//# sourceMappingURL=Lines.js.map