function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
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(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(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); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import * as turf from '@turf/turf';
import { MapIcon } from '@vtx/map-icon';
import { FeatureLayer, FlyLineLayer, Legend, Map } from '@vtx/ol-map';
import { useEffect, useRef, useState } from 'react';
import useSettings from "../../../_hooks/useSettings";
import "./index.less";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default function FlowMap(_ref) {
  var _mapInfo$olProps, _turf$center2;
  var _ref$flowData = _ref.flowData,
    flowData = _ref$flowData === void 0 ? [] : _ref$flowData,
    _ref$info = _ref.info,
    info = _ref$info === void 0 ? {} : _ref$info;
  console.log('%c🤪 ~ file: FlowMap.js:8 : ', 'color: #8873C5', flowData);
  var _ref2 = info || {},
    id = _ref2.id,
    geometryInfo = _ref2.geometryInfo;
  var _ref3 = geometryInfo || {},
    type = _ref3.type,
    lngLats = _ref3.lngLats;
  var _useSettings = useSettings(),
    isDark = _useSettings.isDark,
    mapInfo = _useSettings.mapInfo;
  var mapRef = useRef({});
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    defaultCenter = _useState2[0],
    setDefaultCenter = _useState2[1];
  // 自定义图层逻辑，需要引入ol.js和olScreen.js
  var layers = [];
  mapInfo === null || mapInfo === void 0 || (_mapInfo$olProps = mapInfo.olProps) === null || _mapInfo$olProps === void 0 || _mapInfo$olProps.olCoverage.forEach(function (item) {
    var _window;
    if (item.coverage && (_window = window) !== null && _window !== void 0 && _window.ol) {
      var _window2;
      var temp = null;
      if (item.type === 'superMap') {
        temp = new window.ol.layer.Tile({
          source: new window.ol.source.TileSuperMapRest({
            url: item.coverage
          })
        });
      } else if ((_window2 = window) !== null && _window2 !== void 0 && _window2.OL_SCREEN) {
        // eslint-disable-next-line no-eval
        temp = eval(window.OL_SCREEN[item.coverage].initLayer);
      }
      if (temp) {
        layers = layers.concat(temp);
      }
    }
  });
  useEffect(function () {
    if (mapInfo !== null && mapInfo !== void 0 && mapInfo.longitude) {
      setDefaultCenter([mapInfo.longitude, mapInfo.latitude]);
    }
  }, [mapInfo]);
  var mapProps = {
    createdMap: function createdMap(viewer) {
      var _mapRef$current;
      mapRef.current = viewer;
      mapRef === null || mapRef === void 0 || (_mapRef$current = mapRef.current) === null || _mapRef$current === void 0 || _mapRef$current.fitView();
    },
    center: defaultCenter.length > 0 ? defaultCenter.map(function (v) {
      return Number(v);
    }) : null,
    // zoom: 16,
    dark: isDark,
    tk: '1adbb6e47668cb33757cd799548bf1f4',
    customBaseLayers: layers.length > 0 ? function () {
      return layers;
    } : null
  };
  var startData = lngLats ? [{
      name: info === null || info === void 0 ? void 0 : info.name,
      geometryInfo: geometryInfo
    }] : [],
    endData = [],
    endCenters = [];
  flowData.map(function (item) {
    var _item$endingPointAddr;
    if (item !== null && item !== void 0 && (_item$endingPointAddr = item.endingPointAddress) !== null && _item$endingPointAddr !== void 0 && _item$endingPointAddr.lngLats) {
      var _item$endingPointAddr2, _turf$center;
      endData.push({
        name: item.endingPointName,
        geometryInfo: {
          type: 'polygon',
          lngLats: item.endingPointAddress.lngLats,
          coordType: 'wgs84'
        }
      });
      var endFeatures = turf.points((_item$endingPointAddr2 = item.endingPointAddress) === null || _item$endingPointAddr2 === void 0 || (_item$endingPointAddr2 = _item$endingPointAddr2.lngLats) === null || _item$endingPointAddr2 === void 0 ? void 0 : _item$endingPointAddr2.split(';').map(function (item) {
        return item.split(',').map(function (item) {
          return Number(item);
        });
      }));
      endCenters.push((_turf$center = turf.center(endFeatures)) === null || _turf$center === void 0 || (_turf$center = _turf$center.geometry) === null || _turf$center === void 0 || (_turf$center = _turf$center.coordinates) === null || _turf$center === void 0 ? void 0 : _turf$center.join());
    }
  });
  var startFeatures = turf.points(lngLats === null || lngLats === void 0 ? void 0 : lngLats.split(';').map(function (item) {
    return item.split(',').map(function (item) {
      return Number(item);
    });
  }));
  var startCenter = (_turf$center2 = turf.center(startFeatures)) === null || _turf$center2 === void 0 || (_turf$center2 = _turf$center2.geometry) === null || _turf$center2 === void 0 || (_turf$center2 = _turf$center2.coordinates) === null || _turf$center2 === void 0 ? void 0 : _turf$center2.join();
  var flyLineData = endCenters.map(function (item) {
    return {
      geometryInfo: {
        coordType: 'wgs84',
        lngLats: "".concat(startCenter, ";").concat(item),
        type: 'polyline'
      }
    };
  });
  useEffect(function () {
    if (mapRef !== null && mapRef !== void 0 && mapRef.current) {
      var _mapRef$current2;
      mapRef === null || mapRef === void 0 || (_mapRef$current2 = mapRef.current) === null || _mapRef$current2 === void 0 || _mapRef$current2.fitView([].concat(endData, startData), {
        padding: [0, 0, 0, 0]
      });
    }
  }, [mapRef === null || mapRef === void 0 ? void 0 : mapRef.current, JSON.stringify(endData), JSON.stringify(startData)]);

  // if (!defaultCenter.length) {
  //   return null;
  // }

  return /*#__PURE__*/_jsx("div", {
    className: "dispose-flowMap",
    children: /*#__PURE__*/_jsxs(Map, _objectSpread(_objectSpread({}, mapProps), {}, {
      children: [/*#__PURE__*/_jsx(Legend, {
        style: {
          left: 16,
          bottom: 16
        }
      }), /*#__PURE__*/_jsx(FeatureLayer, {
        data: startData,
        style: {
          type: 'Polygon',
          style: {
            stroke: {
              color: '#3ab4b4',
              opacity: 1,
              width: 2
            },
            fill: {
              color: '#3ab4b4',
              opacity: 0.4
            }
          }
        },
        id: "start",
        labelField: "name",
        name: "\u8D77\u70B9",
        legend: {
          image: new MapIcon.Icon({
            canvasIcon: 'AREA',
            color: '#3ab4b4'
          }).getImgUrl()
        }
      }), /*#__PURE__*/_jsx(FeatureLayer, {
        data: endData,
        style: {
          type: 'Polygon',
          style: {
            stroke: {
              color: '#2968eb',
              opacity: 1,
              width: 2
            },
            fill: {
              color: '#2968eb',
              opacity: 0.4
            }
          }
        },
        id: "end",
        labelField: "name",
        name: "\u7EC8\u70B9",
        legend: {
          image: new MapIcon.Icon({
            canvasIcon: 'AREA',
            color: '#2968eb'
          }).getImgUrl()
        }
      }), (flyLineData === null || flyLineData === void 0 ? void 0 : flyLineData.length) > 0 && /*#__PURE__*/_jsx(FlyLineLayer, {
        color: "#0080ff",
        id: "fly_line_czz",
        width: 3,
        flyWidth: 6,
        lineStyle: {
          color: '#0080ff'
        },
        data: flyLineData
      })]
    }))
  });
}