"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("@wii/wau-react/lib/spin/style");
var _spin = _interopRequireDefault(require("@wii/wau-react/lib/spin"));
require("@wii/wau-react/lib/button/style");
var _button = _interopRequireDefault(require("@wii/wau-react/lib/button"));
require("@wii/wau-react/lib/row/style");
var _row = _interopRequireDefault(require("@wii/wau-react/lib/row"));
require("@wii/wau-react/lib/input/style");
var _input = _interopRequireDefault(require("@wii/wau-react/lib/input"));
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 _classnames = _interopRequireDefault(require("classnames"));
var _mapCircle = _interopRequireDefault(require("./map-circle"));
var _configProvider = _interopRequireDefault(require("../config-provider"));
var _utils = require("./utils");
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; }
/* eslint-disable no-shadow */

const WauMapSelector = ({
  filter,
  onChange,
  mapKey,
  center,
  options,
  radius = 120,
  style
}) => {
  const currentRef = (0, _react.useRef)(1);
  const [searchKey, setSearchKey] = (0, _react.useState)();
  const [isDataEnd, setIsDataEnd] = (0, _react.useState)(false);
  const [placeList, setPlaceList] = (0, _react.useState)([]);
  const [loading, setLoading] = (0, _react.useState)(false);
  const [currentPlace, setCurrentPlace] = (0, _react.useState)({});
  const [centerPoint, setCenter] = (0, _react.useState)(center || []);
  const {
    getPrefixCls
  } = (0, _react.useContext)(_configProvider.default.ConfigContext);
  const prefixCls = getPrefixCls('map-selector');
  const handlePlaceSelect = (0, _react.useCallback)(item => {
    setSearchKey(item.title);
    setCurrentPlace(item);
    onChange === null || onChange === void 0 ? void 0 : onChange(item);
    const location = item.location;
    setCenter([location.lat, location.lng]);
  }, [onChange]);
  const handleSearch = (0, _react.useCallback)(async (current, center = centerPoint, isLoadMore = false) => {
    try {
      setLoading(true);
      const res = await (0, _utils.getPlaceByRadius)({
        center,
        radius,
        keyWords: searchKey,
        pageNo: current,
        pageSize: 15,
        key: mapKey,
        filter
      });
      currentRef.current = current;
      if (res.status === 0) {
        var _res$data;
        if ((_res$data = res.data) !== null && _res$data !== void 0 && _res$data.length) {
          setPlaceList(placeList => {
            if (isLoadMore) {
              return [...placeList, ...res.data];
            }
            return [...res.data];
          });
          setIsDataEnd(false);
          if (!isLoadMore) {
            const location = res.data[0].location;
            setCenter([location.lat, location.lng]);
          }
        } else {
          setIsDataEnd(true);
        }
      }
    } finally {
      setLoading(false);
    }
  }, [centerPoint, radius, searchKey, mapKey, filter]);
  const handleCenterChange = (0, _react.useCallback)(center => {
    setSearchKey(undefined);
    handleSearch(1, center);
  }, [handleSearch]);
  const handleLoadMore = (0, _react.useCallback)(current => {
    currentRef.current = current;
    handleSearch(current, centerPoint, true);
  }, [centerPoint, handleSearch]);
  const handleSearchAction = (0, _react.useCallback)(() => {
    handleSearch(1);
  }, [handleSearch]);
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: prefixCls,
    style: style,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: `${prefixCls}-overlay`,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${prefixCls}-search-box`,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_input.default.Search, {
          placeholder: "\u8BF7\u8F93\u5165\u8981\u67E5\u8BE2\u7684\u5730\u70B9",
          onSearch: handleSearchAction,
          onChange: e => {
            setSearchKey(e.target.value);
          },
          enterButton: true,
          size: "large",
          value: searchKey
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: `${prefixCls}-content-box`,
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_spin.default, {
          spinning: loading,
          children: [placeList.length ? placeList.map((item, ind) => /*#__PURE__*/(0, _jsxRuntime.jsxs)(_row.default, {
            className: (0, _classnames.default)(`${prefixCls}-place-item`, currentPlace.id === item.id && 'active'),
            onClick: () => handlePlaceSelect(item),
            children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: `${prefixCls}-place-item-title`,
              children: item.title
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
              className: `${prefixCls}-place-item-address`,
              children: item.address
            })]
          }, ind)) : /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: `${prefixCls}-search-no-data`,
            children: "\u6682\u65E0\u6570\u636E\uFF0C\u8BF7\u5728\u5730\u56FE\u4E0A\u70B9\u51FB\u6216\u641C\u7D22"
          }), isDataEnd ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: `${prefixCls}-search-all-data`,
            children: "\u5DF2\u7ECF\u5230\u5E95\u4E86"
          }) : placeList.length > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
            className: `${prefixCls}-load-more-btn`,
            children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_button.default, {
              size: "small",
              type: "link",
              onClick: () => handleLoadMore(currentRef.current + 1),
              children: "\u52A0\u8F7D\u66F4\u591A"
            })
          })]
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_mapCircle.default, {
      onChange: handleCenterChange,
      mapKey: mapKey,
      center: centerPoint,
      options: options,
      radius: radius
    })]
  });
};
var _default = WauMapSelector;
exports.default = _default;