import "@wii/wau-react/es/wau-page-loading/style";
import _WauPageLoading from "@wii/wau-react/es/wau-page-loading";
import "@wii/wau-react/es/result/style";
import _Result from "@wii/wau-react/es/result";
import "@wii/wau-react/es/button/style";
import _Button from "@wii/wau-react/es/button";
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/es.promise.js";
import React, { useEffect, useRef, useState, useContext, useCallback } from 'react';
import { getCenterByIP, getPosition } from './utils';
import { options as defaultOptions } from './constants';
import { useMapLoad } from './use-map-load';
import ConfigProvider from '../config-provider';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
const WauMap = ({
  mapKey,
  center,
  options = defaultOptions,
  style,
  onInitialize,
  onMapClick
}) => {
  const [loading, setLoading] = useState(false);
  const mapRef = useRef();
  const containerRef = useRef(null);
  const centerRef = useRef(center);
  const hooksRef = useRef({
    onInitialize,
    onMapClick
  });
  const optionsRef = useRef(options);
  const mapKeyRef = useRef(mapKey);
  useEffect(() => {
    optionsRef.current = options;
  }, [options]);
  useEffect(() => {
    mapKeyRef.current = mapKey;
  }, [mapKey]);
  useEffect(() => {
    hooksRef.current = {
      onInitialize,
      onMapClick
    };
  }, [onInitialize, onMapClick]);
  useEffect(() => {
    centerRef.current = center;
  }, [center]);
  const initMap = 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 getPosition();
            centerMerged = [position.lat, position.lng];
          } catch (e) {
            const {
              result
            } = await 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 ? 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
  } = useMapLoad(mapKey, initMap);
  const {
    getPrefixCls
  } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('map');
  return /*#__PURE__*/_jsx("div", {
    className: prefixCls,
    style: {
      ...style
    },
    children: error ? /*#__PURE__*/_jsx(_Result, {
      status: "500",
      title: "\u52A0\u8F7D\u5730\u56FE\u5931\u8D25\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5",
      extra: /*#__PURE__*/_jsx(_Button, {
        type: "primary",
        onClick: reloadScript,
        children: "\u70B9\u51FB\u91CD\u8BD5"
      })
    }) : /*#__PURE__*/_jsx("div", {
      ref: containerRef,
      className: `${prefixCls}-container`,
      children: scriptLoading || loading ? /*#__PURE__*/_jsx(_WauPageLoading, {}) : null
    })
  });
};
export default WauMap;