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 { Layer } from "mobility-toolbox-js/ol";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import Feature from "ol/Feature";
import { fromLonLat } from "ol/proj";
import Point from "ol/geom/Point";
import { Icon, Style } from "ol/style";
import pin from "../../img/sbb/location-pin-m-medium-bg-red.url.svg";
const style = new Style({
  image: new Icon({
    src: pin,
    scale: 1.5,
    anchor: [0.5, 30],
    anchorYUnits: "pixels"
  })
});
class MesswagenLayer extends Layer {
  constructor() {
    let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
    super(_objectSpread(_objectSpread({}, options), {}, {
      olLayer: new VectorLayer({
        updateWhileAnimating: true,
        updateWhileInteracting: true,
        source: new VectorSource({
          features: []
        }),
        style
      }),
      properties: _objectSpread({
        follow: true
      }, (options === null || options === void 0 ? void 0 : options.properties) || {})
    }));
  }
  attachToMap(map) {
    super.attachToMap(map);
    if (this.visible) {
      this.start();
    }
    this.olListenersKeys.push(this.olLayer.on("change:visible", () => {
      if (this.visible) {
        this.start();
      } else {
        this.stop();
      }
    }), this.on("change:feature", evt => {
      const feature = this.get(evt.key);
      if (feature && this.get("follow")) {
        const size = map.getSize();
        const extent = map.getView().calculateExtent([size[0] - 200, size[1] - 400]);
        if (!feature.getGeometry().intersectsExtent(extent)) {
          this.centerOn(feature, undefined);
        }
        if (feature && !evt.oldValue) {
          this.centerOn(feature, 17);
        }
      }
    }));
  }
  detachFromMap(map) {
    this.stop();
    this.olLayer.getSource().clear();
    super.detachFromMap(map);
  }
  start() {
    if (!this.get("fileName")) return;
    const feature = this.get("feature");
    if (feature) {
      this.centerOn(feature);
    }
    this.stopped = false;
    this.updateData();
  }
  stop() {
    var _this$abortController;
    this.stopped = true;
    (_this$abortController = this.abortController) === null || _this$abortController === void 0 || _this$abortController.abort();
    clearTimeout(this.abortTimeout);
    clearTimeout(this.timeout);
  }
  updateData() {
    var _this$abortController2;
    (_this$abortController2 = this.abortController) === null || _this$abortController2 === void 0 || _this$abortController2.abort();
    this.abortController = new AbortController();

    // Auto abort when request takes too long
    clearTimeout(this.abortTimeout);
    this.abortTimeout = setTimeout(() => {
      this.abortController.abort();
    }, 10000);

    // We put it in a variable for easy testing.
    this.fetch = fetch("https://trafimage-services1.geops.de/messwagen/".concat(this.get("fileName"), ".json"), {
      signal: this.abortController.signal
    }).then(response => response.json()).then(data => {
      this.olLayer.getSource().clear();
      if (data !== null && data !== void 0 && data.latitude && data !== null && data !== void 0 && data.longitude) {
        const feature = new Feature(_objectSpread({
          geometry: new Point(fromLonLat([data.longitude, data.latitude]))
        }, data));
        this.olLayer.getSource().addFeatures([feature]);
        this.set("feature", feature);
      }
    }).catch(() => {}).finally(() => {
      clearTimeout(this.abortTimeout);
      clearTimeout(this.timeout);

      // The finally occurs asynchronously when we call the abort in stop().
      // So we have to recheck if the layer is stopped or not.
      if (!this.stopped) {
        // Update data in one second
        this.timeout = setTimeout(() => {
          this.updateData();
        }, 1000);
      }
    });
  }
  centerOn(feature, zoom) {
    var _feature$getGeometry;
    const center = feature === null || feature === void 0 || (_feature$getGeometry = feature.getGeometry()) === null || _feature$getGeometry === void 0 ? void 0 : _feature$getGeometry.getCoordinates();
    if (center) {
      var _this$map, _this$map2;
      (_this$map = this.map) === null || _this$map === void 0 || _this$map.getView().cancelAnimations();
      (_this$map2 = this.map) === null || _this$map2 === void 0 || _this$map2.getView().animate({
        center,
        zoom
      });
    }
  }
}
export default MesswagenLayer;
//# sourceMappingURL=MesswagenLayer.js.map