"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("@wii/wau-react/lib/wau-page-loading/style");
var _wauPageLoading = _interopRequireDefault(require("@wii/wau-react/lib/wau-page-loading"));
require("@wii/wau-react/lib/result/style");
var _result = _interopRequireDefault(require("@wii/wau-react/lib/result"));
require("@wii/wau-react/lib/button/style");
var _button = _interopRequireDefault(require("@wii/wau-react/lib/button"));
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
require("core-js/modules/es.promise.js");
var _react = _interopRequireWildcard(require("react"));
var _utils = require("./utils");
var _constants = require("./constants");
var _useMapLoad = require("./use-map-load");
var _configProvider = _interopRequireDefault(require("../config-provider"));
require("./style");
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 WauMap = ({
  mapKey,
  center,
  options = _constants.options,
  style,
  onInitialize,
  onMapClick
}) => {
  const [loading, setLoading] = (0, _react.useState)(false);
  const mapRef = (0, _react.useRef)();
  const containerRef = (0, _react.useRef)(null);
  const centerRef = (0, _react.useRef)(center);
  const hooksRef = (0, _react.useRef)({
    onInitialize,
    onMapClick
  });
  const optionsRef = (0, _react.useRef)(options);
  const mapKeyRef = (0, _react.useRef)(mapKey);
  (0, _react.useEffect)(() => {
    optionsRef.current = options;
  }, [options]);
  (0, _react.useEffect)(() => {
    mapKeyRef.current = mapKey;
  }, [mapKey]);
  (0, _react.useEffect)(() => {
    hooksRef.current = {
      onInitialize,
      onMapClick
    };
  }, [onInitialize, onMapClick]);
  (0, _react.useEffect)(() => {
    centerRef.current = center;
  }, [center]);
  const initMap = (0, _react.useCallback)(async () => {
    if (containerRef.current) {
      try {
        var _centerRef$current, _hooksRef$current, _hooksRef$current$onI;
        let centerMerged = [];
        if (((_centerRef$current = centerRef.current) === null || _centerRef$current === void 0 ? void 0 : _centerRef$current.length) === 2) {
          centerMerged = centerRef.current;
        } else {
          setLoading(true);
          try {
            const position = await (0, _utils.getPosition)();
            centerMerged = [position.lat, position.lng];
          } catch (e) {
            const {
              result
            } = await (0, _utils.getCenterByIP)(mapKeyRef.current);
            if (result) {
              centerMerged = [result.location.lat, result.location.lng];
            }
          }
          setLoading(false);
        }
        const TMap = window.TMap;
        const mapOptions = {
          ...optionsRef.current
        };
        if (centerMerged.length) {
          // @ts-ignore
          mapOptions.center = new TMap.LatLng(...centerMerged);
        }
        const map = new TMap.Map(containerRef.current, mapOptions);
        mapRef.current = map;
        (_hooksRef$current = hooksRef.current) === null || _hooksRef$current === void 0 || (_hooksRef$current$onI = _hooksRef$current.onInitialize) === null || _hooksRef$current$onI === void 0 ? void 0 : _hooksRef$current$onI.call(_hooksRef$current, map, centerMerged);
        map.on('click', evt => {
          var _hooksRef$current$onM, _hooksRef$current2;
          (_hooksRef$current$onM = (_hooksRef$current2 = hooksRef.current).onMapClick) === null || _hooksRef$current$onM === void 0 ? void 0 : _hooksRef$current$onM.call(_hooksRef$current2, evt);
        });
        // eslint-disable-next-line no-empty
      } catch (err) {
        console.error(err);
      } finally {
        setLoading(false);
      }
    }
  }, []);
  const {
    loading: scriptLoading,
    error,
    loadScript: reloadScript
  } = (0, _useMapLoad.useMapLoad)(mapKey, initMap);
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('map');
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
    className: prefixCls,
    style: {
      ...style
    },
    children: error ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_result.default, {
      status: "500",
      title: "\u52A0\u8F7D\u5730\u56FE\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",
      extra: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.default, {
        type: "primary",
        onClick: reloadScript,
        children: "\u70B9\u51FB\u91CD\u8BD5"
      })
    }) : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      ref: containerRef,
      className: `${prefixCls}-container`,
      children: scriptLoading || loading ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauPageLoading.default, {}) : null
    })
  });
};
var _default = WauMap;
exports.default = _default;