"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
var _react = _interopRequireWildcard(require("react"));
var _wauMap = _interopRequireDefault(require("../wau-map"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
const WauMapCircle = ({
  onChange,
  center,
  radius = 120,
  ...props
}) => {
  const mapRef = (0, _react.useRef)();
  const markerLayerRef = (0, _react.useRef)();
  const centerRef = (0, _react.useRef)([]);
  const circleRef = (0, _react.useRef)();
  const drawCircle = (0, _react.useCallback)(latLng => {
    var _circleRef$current;
    (_circleRef$current = circleRef.current) === null || _circleRef$current === void 0 ? void 0 : _circleRef$current.updateGeometries([{
      id: '1',
      styleId: 'circle',
      center: latLng,
      radius
    }]);
  }, [radius]);
  const handleInitialize = (0, _react.useCallback)((map, center) => {
    mapRef.current = map;
    if (center.length) {
      const markerLayer = new window.TMap.MultiMarker({
        id: 'marker-layer',
        map
      });
      markerLayer.add({
        position: new window.TMap.LatLng(...center)
      });
      markerLayerRef.current = markerLayer;
      // 画圈
      circleRef.current = new window.TMap.MultiCircle({
        map,
        styles: {
          // 设置圆形样式
          circle: new window.TMap.CircleStyle({
            color: 'rgba(41,91,255,0.16)',
            showBorder: true,
            borderColor: 'rgba(41,91,255,1)',
            borderWidth: 2
          })
        }
      });
      onChange === null || onChange === void 0 ? void 0 : onChange(center);
      drawCircle(new window.TMap.LatLng(...center));
    }
  }, [drawCircle, onChange]);
  const handleMapClick = (0, _react.useCallback)(evt => {
    const markerLayer = markerLayerRef.current;
    if (markerLayer) {
      drawCircle(evt.latLng);
      markerLayer.setGeometries([]);
      centerRef.current = [evt.latLng.lat, evt.latLng.lng];
      markerLayer.add({
        position: evt.latLng
      });
      onChange === null || onChange === void 0 ? void 0 : onChange(centerRef.current);
    }
  }, [drawCircle, onChange]);
  (0, _react.useEffect)(() => {
    const map = mapRef.current;
    const markerLayer = markerLayerRef.current;
    if (map && markerLayer && center && (center === null || center === void 0 ? void 0 : center.length) > 0) {
      drawCircle(new window.TMap.LatLng(...center));
      markerLayer.setGeometries([]);
      markerLayer.add({
        position: new window.TMap.LatLng(...center)
      });
      setTimeout(() => {
        map.setCenter(new window.TMap.LatLng(...(center || [])));
      });
    }
  }, [drawCircle, center]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauMap.default, {
    center: center,
    ...props,
    onInitialize: handleInitialize,
    onMapClick: handleMapClick
  });
};
var _default = WauMapCircle;
exports.default = _default;