"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = VmOlMap;
var _react = _interopRequireWildcard(require("react"));
var _useSettings2 = _interopRequireDefault(require("../vm-hooks/useSettings"));
var _olMap = require("@vtx/ol-map");
var _ahooks = require("ahooks");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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(r) { if (Array.isArray(r)) return r; }
function VmOlMap(_ref) {
  var mapCenter = _ref.mapCenter,
    _ref$mapType = _ref.mapType,
    mapType = _ref$mapType === void 0 ? 'bmap' : _ref$mapType,
    _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,
    geometryType = _ref.geometryType;
  var _a, _b, _c, _d;
  var mapRef = (0, _react.useRef)({});
  var _useState = (0, _react.useState)([]),
    _useState2 = _slicedToArray(_useState, 2),
    defaultCenter = _useState2[0],
    setDefaultCenter = _useState2[1];
  var _useSettings = (0, _useSettings2["default"])(),
    isDark = _useSettings.isDark,
    theme = _useSettings.theme,
    mapInfo = _useSettings.mapInfo;
  var _useState3 = (0, _react.useState)({
      data: [],
      style: {}
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    newPoints = _useState4[0],
    setNewPoints = _useState4[1];
  (0, _react.useEffect)(function () {
    if (mapInfo === null || mapInfo === void 0 ? void 0 : mapInfo.longitude) {
      setDefaultCenter([mapInfo.longitude, mapInfo.latitude]);
    }
  }, [mapInfo]);
  // 处理VtxMap数据为@vtx/ol-map
  function formatData(arr, type) {
    var data = [],
      style = {},
      coordType = ['amap', 'bmap'].includes(mapType) ? mapType : 'wgs84';
    arr.map(function (item) {
      if (type == 'polygon' || type == 'polyline') {
        data.push(Object.assign(Object.assign({}, 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(Object.assign(Object.assign({}, 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: 'MRSS',
            color: '#1899ff'
          }
        };
      }
    });
    return {
      data: data,
      style: style
    };
  }
  // 处理不同的点位图标
  function newFormData(arr) {
    var data = [];
    arr.map(function (item) {
      data.push(Object.assign(Object.assign({}, 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() {
    var _a, _b, _c;
    if (mapRef.current && ((mapPoints === null || mapPoints === void 0 ? void 0 : mapPoints.length) > 0 || (mapPolygons === null || mapPolygons === void 0 ? void 0 : mapPolygons.length) > 0)) {
      (_a = mapRef.current) === null || _a === void 0 ? void 0 : _a.fitView({
        geometryInfo: (((_b = polygons.data) === null || _b === void 0 ? void 0 : _b[0]) || ((_c = points.data) === null || _c === void 0 ? void 0 : _c[0])).geometryInfo
      }, {
        maxZoom: 16
      });
    }
  }
  (0, _ahooks.useUpdateEffect)(function () {
    var _a, _b, _c, _d;
    var _newPoints = newFormData(newMapPoints);
    setNewPoints(_newPoints);
    (newMapPoints === null || newMapPoints === void 0 ? void 0 : newMapPoints.length) === 1 && mapRef.current && ((_a = _newPoints.data) === null || _a === void 0 ? void 0 : _a.length) > 0 && ((_b = mapRef.current) === null || _b === void 0 ? void 0 : _b.fitView({
      geometryInfo: (_d = (_c = _newPoints.data) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.geometryInfo
    }, {
      maxZoom: 16
    }));
  }, [JSON.stringify(newMapPoints)]);
  (0, _ahooks.useUpdateEffect)(function () {
    fitView();
  }, [JSON.stringify(mapPoints), JSON.stringify(mapPolygons), JSON.stringify(newMapPoints)]);
  // 自定义图层逻辑
  /**
   * 需要引入ol.js和olScreen.js
   */
  var layers = [];
  (_a = mapInfo === null || mapInfo === void 0 ? void 0 : mapInfo.olProps) === null || _a === void 0 ? void 0 : _a.olCoverage.map(function (item) {
    if (item.coverage && (window === null || window === void 0 ? void 0 : window.ol)) {
      var temp = null;
      if (item.type == 'superMap') {
        temp = new window.ol.layer.Tile({
          source: new window.ol.source.TileSuperMapRest({
            url: item.coverage
          })
        });
      } else if (window === null || window === void 0 ? void 0 : window.OL_SCREEN) {
        temp = eval(window.OL_SCREEN[item.coverage].initLayer);
      }
      if (temp) {
        layers = layers.concat(temp);
      }
    }
  });
  return /*#__PURE__*/_react["default"].createElement(_olMap.Map, {
    createdMap: function createdMap(viewer) {
      mapRef.current = viewer;
      fitView();
    },
    center: (mapCenter || defaultCenter).map(function (v) {
      return Number(v);
    }),
    zoom: 16,
    dark: isDark || theme == 'blue',
    tk: "1adbb6e47668cb33757cd799548bf1f4",
    customBaseLayers: layers.length > 0 ? function () {
      return layers;
    } : null
  }, ((_b = points.data) === null || _b === void 0 ? void 0 : _b.length) > 0 ? /*#__PURE__*/_react["default"].createElement(_olMap.FeatureLayer, Object.assign({
    id: "vm_ol_map_points",
    zIndex: 8
  }, points)) : null, ((_c = polygons.data) === null || _c === void 0 ? void 0 : _c.length) > 0 ? /*#__PURE__*/_react["default"].createElement(_olMap.FeatureLayer, Object.assign({
    id: "vm_ol_map_polygons",
    zIndex: 7
  }, polygons)) : null, ((_d = newPoints === null || newPoints === void 0 ? void 0 : newPoints.data) === null || _d === void 0 ? void 0 : _d.length) > 0 ? newPoints.data.map(function (ele) {
    return /*#__PURE__*/_react["default"].createElement(_olMap.Dom, {
      data: [ele.lng, ele.lat],
      offset: [0, 20]
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        display: 'flex',
        flexDirection: 'column',
        justifyContent: 'center',
        alignItems: 'center',
        minWidth: 30
      }
    }, /*#__PURE__*/_react["default"].createElement("img", {
      src: ele.url,
      style: {
        width: 30,
        height: 30
      }
    }), !!ele.staffName && /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        padding: '0 6px',
        color: '#096dd9',
        fontSize: 12,
        border: '1px solid #096dd9',
        background: '#fff',
        borderRadius: 4
      }
    }, ele.staffName)));
  }) : null);
}
//# sourceMappingURL=index.js.map
