import "@wii/wau-react/es/spin/style";
import _Spin from "@wii/wau-react/es/spin";
import "@wii/wau-react/es/button/style";
import _Button from "@wii/wau-react/es/button";
import "@wii/wau-react/es/row/style";
import _Row from "@wii/wau-react/es/row";
import "@wii/wau-react/es/input/style";
import _Input from "@wii/wau-react/es/input";
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
import "core-js/modules/es.promise.js";
/* eslint-disable no-shadow */
import React, { useState, useCallback, useContext, useRef } from 'react';
import classNames from 'classnames';
import WauMapCircle from './map-circle';
import ConfigProvider from '../config-provider';
import { getPlaceByRadius } from './utils';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
const WauMapSelector = ({
  filter,
  onChange,
  mapKey,
  center,
  options,
  radius = 120,
  style
}) => {
  const currentRef = useRef(1);
  const [searchKey, setSearchKey] = useState();
  const [isDataEnd, setIsDataEnd] = useState(false);
  const [placeList, setPlaceList] = useState([]);
  const [loading, setLoading] = useState(false);
  const [currentPlace, setCurrentPlace] = useState({});
  const [centerPoint, setCenter] = useState(center || []);
  const {
    getPrefixCls
  } = useContext(ConfigProvider.ConfigContext);
  const prefixCls = getPrefixCls('map-selector');
  const handlePlaceSelect = 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 = useCallback(async (current, center = centerPoint, isLoadMore = false) => {
    try {
      setLoading(true);
      const res = await 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 = useCallback(center => {
    setSearchKey(undefined);
    handleSearch(1, center);
  }, [handleSearch]);
  const handleLoadMore = useCallback(current => {
    currentRef.current = current;
    handleSearch(current, centerPoint, true);
  }, [centerPoint, handleSearch]);
  const handleSearchAction = useCallback(() => {
    handleSearch(1);
  }, [handleSearch]);
  return /*#__PURE__*/_jsxs("div", {
    className: prefixCls,
    style: style,
    children: [/*#__PURE__*/_jsxs("div", {
      className: `${prefixCls}-overlay`,
      children: [/*#__PURE__*/_jsx("div", {
        className: `${prefixCls}-search-box`,
        children: /*#__PURE__*/_jsx(_Input.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__*/_jsx("div", {
        className: `${prefixCls}-content-box`,
        children: /*#__PURE__*/_jsxs(_Spin, {
          spinning: loading,
          children: [placeList.length ? placeList.map((item, ind) => /*#__PURE__*/_jsxs(_Row, {
            className: classNames(`${prefixCls}-place-item`, currentPlace.id === item.id && 'active'),
            onClick: () => handlePlaceSelect(item),
            children: [/*#__PURE__*/_jsx("div", {
              className: `${prefixCls}-place-item-title`,
              children: item.title
            }), /*#__PURE__*/_jsx("div", {
              className: `${prefixCls}-place-item-address`,
              children: item.address
            })]
          }, ind)) : /*#__PURE__*/_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__*/_jsx("div", {
            className: `${prefixCls}-search-all-data`,
            children: "\u5DF2\u7ECF\u5230\u5E95\u4E86"
          }) : placeList.length > 0 && /*#__PURE__*/_jsx("div", {
            className: `${prefixCls}-load-more-btn`,
            children: /*#__PURE__*/_jsx(_Button, {
              size: "small",
              type: "link",
              onClick: () => handleLoadMore(currentRef.current + 1),
              children: "\u52A0\u8F7D\u66F4\u591A"
            })
          })]
        })
      })]
    }), /*#__PURE__*/_jsx(WauMapCircle, {
      onChange: handleCenterChange,
      mapKey: mapKey,
      center: centerPoint,
      options: options,
      radius: radius
    })]
  });
};
export default WauMapSelector;