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 { Circle, Dom, FeatureLayer, Map } from '@vtx/ol-map';
import { useUpdateEffect } from 'ahooks';
import { Tooltip } from 'antd';
import classNames from 'classnames';
import { register } from 'ol/proj/proj4';
import proj4 from 'proj4';
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 VmOlMap(_ref) {
  var _mapInfo$olProps, _points$data2, _polygons$data2, _newPoints$data3;
  var mapCenter = _ref.mapCenter,
    _ref$mapPoints = _ref.mapPoints,
    mapPoints = _ref$mapPoints === void 0 ? [] : _ref$mapPoints,
    _ref$newMapPoints = _ref.newMapPoints,
    newMapPoints = _ref$newMapPoints === void 0 ? [] : _ref$newMapPoints,
    _ref$mapPolygons = _ref.mapPolygons,
    mapPolygons = _ref$mapPolygons === void 0 ? [] : _ref$mapPolygons,
    _ref$mapCircles = _ref.mapCircles,
    mapCircles = _ref$mapCircles === void 0 ? [] : _ref$mapCircles,
    geometryType = _ref.geometryType,
    address = _ref.address;
  var mapRef = useRef({});
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    defaultCenter = _useState2[0],
    setDefaultCenter = _useState2[1];
  var _useSettings = useSettings(),
    isDark = _useSettings.isDark,
    theme = _useSettings.theme,
    mapInfo = _useSettings.mapInfo;
  var projectionRef = useRef(mapInfo.epsg ? "EPSG:".concat(mapInfo.epsg) : 'EPSG:4326');
  var _useState3 = useState({
      data: [],
      style: {}
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    newPoints = _useState4[0],
    setNewPoints = _useState4[1];
  useEffect(function () {
    if (mapInfo !== null && mapInfo !== void 0 && mapInfo.longitude) {
      if (mapInfo.epsg) {
        projectionRef.current = "EPSG:".concat(mapInfo.epsg);
        proj4.defs("EPSG:".concat(mapInfo.epsg), mapInfo.proj4);
        register(proj4);
      }
      setDefaultCenter([mapInfo.longitude, mapInfo.latitude]);
    }
  }, [mapInfo]);

  // 处理VtxMap数据为@vtx/ol-map
  function formatData(arr, type) {
    var data = [],
      style = {},
      coordType = 'wgs84';
    arr.forEach(function (item) {
      if (type === 'polygon' || type === 'polyline') {
        data.push(_objectSpread(_objectSpread({}, item), {}, {
          geometryInfo: {
            type: type,
            lngLats: item.rings,
            coordType: coordType
          }
        }));
        style = type === 'polygon' ? {
          type: 'Polygon',
          style: {
            stroke: {
              color: '#1899ff',
              opacity: 1,
              width: 2
            },
            fill: {
              color: '#1899ff',
              opacity: 0.4
            }
          }
        } : {
          type: 'Polyline',
          style: {
            stroke: {
              color: '#1899ff',
              opacity: 1,
              width: 6
            }
          }
        };
      } else {
        data.push(_objectSpread(_objectSpread({}, item), {}, {
          geometryInfo: {
            type: 'point',
            lngLats: "".concat(item.lng, ",").concat(item.lat),
            coordType: coordType
          }
        }));
        style = {
          type: 'Marker',
          style: item.url ? {
            image: item.url,
            scale: 0.8
          } : {
            icon: item.icon || 'MRSS',
            color: item.color || '#1899ff'
          }
        };
      }
    });
    return {
      data: data,
      style: style
    };
  }

  // 处理不同的点位图标
  function newFormData(arr) {
    var data = [];
    arr.forEach(function (item) {
      data.push(_objectSpread(_objectSpread({}, item), {}, {
        geometryInfo: {
          type: 'point',
          lngLats: "".concat(item.lng, ",").concat(item.lat),
          coordType: 'wgs84'
        }
      }));
    });
    return {
      data: data
    };
  }
  var points = formatData(mapPoints);
  var polygons = formatData(mapPolygons, geometryType);
  function fitView() {
    if (mapRef.current.hasOwnProperty('tk') && ((mapPoints === null || mapPoints === void 0 ? void 0 : mapPoints.length) > 0 || (mapPolygons === null || mapPolygons === void 0 ? void 0 : mapPolygons.length) > 0)) {
      var _mapRef$current, _polygons$data, _points$data;
      (_mapRef$current = mapRef.current) === null || _mapRef$current === void 0 || _mapRef$current.fitView({
        geometryInfo: ((polygons === null || polygons === void 0 || (_polygons$data = polygons.data) === null || _polygons$data === void 0 ? void 0 : _polygons$data[0]) || (points === null || points === void 0 || (_points$data = points.data) === null || _points$data === void 0 ? void 0 : _points$data[0])).geometryInfo
      }, {
        maxZoom: 16,
        padding: [10, 10, 10, 10]
      });
    }
  }
  useUpdateEffect(function () {
    var _newPoints$data, _mapRef$current2, _newPoints$data2;
    var _newPoints = newFormData(newMapPoints);
    setNewPoints(_newPoints);
    (newMapPoints === null || newMapPoints === void 0 ? void 0 : newMapPoints.length) === 1 && mapRef.current && ((_newPoints$data = _newPoints.data) === null || _newPoints$data === void 0 ? void 0 : _newPoints$data.length) > 0 && ((_mapRef$current2 = mapRef.current) === null || _mapRef$current2 === void 0 ? void 0 : _mapRef$current2.fitView({
      geometryInfo: (_newPoints$data2 = _newPoints.data) === null || _newPoints$data2 === void 0 || (_newPoints$data2 = _newPoints$data2[0]) === null || _newPoints$data2 === void 0 ? void 0 : _newPoints$data2.geometryInfo
    }, {
      maxZoom: 16,
      padding: [0, 0, 0, 0]
    }));
  }, [JSON.stringify(newMapPoints)]);
  useUpdateEffect(function () {
    fitView();
  }, [JSON.stringify(mapPoints), JSON.stringify(mapPolygons), JSON.stringify(newMapPoints)]);

  // 自定义图层逻辑
  /**
   * 需要引入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);
      }
    }
  });
  //mapCenter未传参情况下，要监听defaultCenter有值
  if (!(defaultCenter !== null && defaultCenter !== void 0 && defaultCenter.length)) return null;
  return /*#__PURE__*/_jsx("div", {
    className: classNames('vm-map-wrap', {
      dark: isDark
    }),
    children: /*#__PURE__*/_jsxs(Map, {
      createdMap: function createdMap(viewer) {
        mapRef.current = viewer;
        fitView();
      },
      center: (mapCenter !== null && mapCenter !== void 0 && mapCenter.length ? mapCenter : defaultCenter).map(function (v) {
        return Number(v);
      }),
      zoom: 16,
      dark: isDark || theme === 'blue',
      tk: "1adbb6e47668cb33757cd799548bf1f4",
      projection: projectionRef.current,
      customBaseLayers: layers.length > 0 ? function () {
        return layers;
      } : null,
      children: [((_points$data2 = points.data) === null || _points$data2 === void 0 ? void 0 : _points$data2.length) > 0 ? /*#__PURE__*/_jsx(FeatureLayer, _objectSpread({
        id: "vm_ol_map_points",
        zIndex: 8
      }, points)) : null, ((_polygons$data2 = polygons.data) === null || _polygons$data2 === void 0 ? void 0 : _polygons$data2.length) > 0 ? /*#__PURE__*/_jsx(FeatureLayer, _objectSpread({
        id: "vm_ol_map_polygons",
        zIndex: 7
      }, polygons)) : null, (mapCircles === null || mapCircles === void 0 ? void 0 : mapCircles.length) > 0 ? /*#__PURE__*/_jsx(FeatureLayer, {
        children: mapCircles.map(function (item) {
          return /*#__PURE__*/_jsx(Circle, {
            data: item.data,
            radius: item.radius,
            style: {
              stroke: {
                color: '#ff0000',
                opacity: 1,
                width: 3
              },
              fill: {
                color: '#ff0000',
                opacity: 0.4
              }
            }
          }, item.id);
        })
      }) : null, (newPoints === null || newPoints === void 0 || (_newPoints$data3 = newPoints.data) === null || _newPoints$data3 === void 0 ? void 0 : _newPoints$data3.length) > 0 ? newPoints.data.map(function (ele, index) {
        return /*#__PURE__*/_jsx(Dom, {
          data: [ele.lng, ele.lat],
          offset: [0, 20],
          children: /*#__PURE__*/_jsxs("div", {
            style: {
              display: 'flex',
              flexDirection: 'column',
              justifyContent: 'center',
              alignItems: 'center',
              minWidth: 30
            },
            children: [/*#__PURE__*/_jsx("img", {
              src: ele.url,
              style: {
                width: 30,
                height: 30
              }
            }), !!ele.staffName && /*#__PURE__*/_jsx("div", {
              style: {
                padding: '0 6px',
                color: '#096dd9',
                fontSize: 12,
                border: '1px solid #096dd9',
                background: '#fff',
                borderRadius: 4
              },
              children: ele.staffName
            })]
          })
        }, index);
      }) : null, address && /*#__PURE__*/_jsx("div", {
        className: 'vm-map-address-wrap',
        children: /*#__PURE__*/_jsx(Tooltip, {
          title: address,
          children: /*#__PURE__*/_jsx("div", {
            className: 'vm-map-address',
            children: address
          })
        })
      })]
    })
  });
}