var _excluded = ["type", "icons"];
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React, { useState, useEffect } from 'react';
import { Button, Input, Popover, Radio, Space } from 'antd';
import { CloseOutlined, SearchOutlined } from '@ant-design/icons';
import { useControllableValue } from 'ahooks';
import { isUndefined } from '@pisell/utils';
import { getText } from "../../locales";
import { getIconList } from "./utils";
import "./index.less";
var IconGroupNameMap = {
  outlined: getText('pisell-icon-select-outlined'),
  filled: getText('pisell-icon-select-filled'),
  'two-tone': getText('pisell-icon-select-two-tone'),
  iconfont: getText('pisell-icon-select-iconfont')
};
var Icon = function Icon(props) {
  var type = props.type,
    _props$icons = props.icons,
    icons = _props$icons === void 0 ? {} : _props$icons,
    rest = _objectWithoutProperties(props, _excluded);
  var Comp = icons[type];
  if (!Comp) return null;
  return /*#__PURE__*/React.createElement(Comp, rest);
};
var IconContent = function IconContent(_ref) {
  var selectedGroup = _ref.selectedGroup,
    groups = _ref.groups,
    setSelectedGroup = _ref.setSelectedGroup,
    search = _ref.search,
    setSearch = _ref.setSearch,
    list = _ref.list,
    handleChange = _ref.handleChange,
    icons = _ref.icons;
  return /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-icon-select-popup"
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    className: "pisell-lowcode-icon-select-radio-group",
    size: "middle",
    value: selectedGroup,
    onChange: function onChange(e) {
      return setSelectedGroup(e.target.value);
    }
  }, groups.map(function (item) {
    return /*#__PURE__*/React.createElement(Radio.Button, {
      key: item.group,
      value: item.group
    }, IconGroupNameMap[item.group]);
  })), /*#__PURE__*/React.createElement(Input, {
    value: search,
    size: "middle",
    placeholder: getText('pisell-icon-select-search'),
    allowClear: true,
    prefix: /*#__PURE__*/React.createElement(SearchOutlined, null),
    onChange: function onChange(e) {
      return setSearch(e.target.value);
    },
    style: {
      width: '100%'
    },
    className: "pisell-lowcode-icon-select-search"
  }), /*#__PURE__*/React.createElement("ul", {
    className: "pisell-lowcode-icon-select-content"
  }, list.map(function (item) {
    return /*#__PURE__*/React.createElement("li", {
      className: "pisell-lowcode-icon-select-item",
      key: item.name,
      onClick: function onClick() {
        return handleChange(item.name);
      },
      title: item.name
    }, /*#__PURE__*/React.createElement(Icon, {
      type: item.name,
      icons: icons
    }));
  })));
};
var IconSelect = function IconSelect(props) {
  var disabled = props.disabled,
    defaultValue = props.defaultValue;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useState3 = useState(''),
    _useState4 = _slicedToArray(_useState3, 2),
    search = _useState4[0],
    setSearch = _useState4[1];
  var _useState5 = useState({}),
    _useState6 = _slicedToArray(_useState5, 2),
    icons = _useState6[0],
    setIcons = _useState6[1];
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    groups = _useState8[0],
    setGroups = _useState8[1];
  var _useState9 = useState('outlined'),
    _useState10 = _slicedToArray(_useState9, 2),
    selectedGroup = _useState10[0],
    setSelectedGroup = _useState10[1];
  var _useState11 = useState(true),
    _useState12 = _slicedToArray(_useState11, 2),
    firstLoad = _useState12[0],
    setFirstLoad = _useState12[1];
  var _useState13 = useState([]),
    _useState14 = _slicedToArray(_useState13, 2),
    list = _useState14[0],
    setList = _useState14[1];
  var _useControllableValue = useControllableValue(props, {
      defaultValue: defaultValue
    }),
    _useControllableValue2 = _slicedToArray(_useControllableValue, 2),
    value = _useControllableValue2[0],
    onChange = _useControllableValue2[1];
  if (firstLoad && defaultValue && isUndefined(value)) {
    onChange(defaultValue);
    setFirstLoad(false);
  }
  useEffect(function () {
    var _groups$;
    var iconList = getIconList();
    var groups = [];
    var icons = {};
    iconList.forEach(function (item) {
      var _ref2 = item,
        group = _ref2.group,
        name = _ref2.name,
        icon = _ref2.icon;
      if (groups.every(function (item) {
        return item.group !== group;
      })) {
        groups.push({
          group: group,
          list: []
        });
      }
      var target = groups.find(function (item) {
        return item.group === group;
      });
      target.list.push(item);
      icons[item.name] = item === null || item === void 0 ? void 0 : item.icon;
    });
    setIcons(icons);
    setGroups(groups);
    setSelectedGroup((_groups$ = groups[0]) === null || _groups$ === void 0 ? void 0 : _groups$.group);
  }, []);
  useEffect(function () {
    var _currentGroup$list;
    var currentGroup = groups.find(function (item) {
      return item.group === selectedGroup;
    });
    setList(((_currentGroup$list = currentGroup === null || currentGroup === void 0 ? void 0 : currentGroup.list) !== null && _currentGroup$list !== void 0 ? _currentGroup$list : []).filter(function (item) {
      return search ? item.name.toLowerCase().indexOf(search.toLowerCase()) > -1 : true;
    }));
  }, [selectedGroup, search, groups]);
  var handleChange = function handleChange(icon) {
    onChange === null || onChange === void 0 || onChange(icon);
    hide();
  };
  var triggerNode = /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode-icon-select-trigger"
  }, /*#__PURE__*/React.createElement(Icon, {
    type: value,
    icons: icons,
    style: {
      fontSize: 24
    }
  }));
  var hide = function hide() {
    setOpen(false);
  };
  var handleOpenChange = function handleOpenChange(newOpen) {
    setOpen(newOpen);
  };
  return /*#__PURE__*/React.createElement(Space.Compact, {
    block: true,
    className: "pisell-lowcode-icon-select"
  }, /*#__PURE__*/React.createElement(Popover, {
    content: /*#__PURE__*/React.createElement(IconContent, {
      selectedGroup: selectedGroup,
      groups: groups,
      setSelectedGroup: setSelectedGroup,
      search: search,
      setSearch: setSearch,
      list: list,
      handleChange: handleChange,
      icons: icons
    }),
    trigger: "click",
    placement: "bottom",
    open: open,
    onOpenChange: handleOpenChange
  }, value ? /*#__PURE__*/React.createElement(Button, {
    size: "large",
    disabled: disabled,
    icon: triggerNode,
    className: "pisell-lowcode-icon-select-button"
  }) : /*#__PURE__*/React.createElement(Button, {
    className: "pisell-lowcode-icon-select-button-text",
    size: "large"
  }, getText('pisell-icon-select-select'))), value && !disabled && /*#__PURE__*/React.createElement(Button, {
    size: "large",
    icon: /*#__PURE__*/React.createElement(CloseOutlined, null),
    className: "pisell-lowcode-icon-select-button",
    onClick: function onClick() {
      return onChange === null || onChange === void 0 ? void 0 : onChange('');
    }
  }));
};
export default IconSelect;