function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

// @ts-nocheck
import React, { useEffect, useState } from 'react';
import Popover from "antd/es/popover";
import Button from "antd/es/button";
import Badge from "antd/es/badge";
import IconList from './IconList';
import * as FishxIcons from '@fishx/icons';
import { createFromIconfontCN, CloseCircleFilled } from '@fishx/icons';
var allIcons = FishxIcons;

var IconPicker = function IconPicker(_ref) {
  var _ref$mode = _ref.mode,
      mode = _ref$mode === void 0 ? 'inline' : _ref$mode,
      defaultValue = _ref.defaultValue,
      value = _ref.value,
      _ref$readonly = _ref.readonly,
      readonly = _ref$readonly === void 0 ? false : _ref$readonly,
      onChange = _ref.onChange,
      _ref$className = _ref.className,
      className = _ref$className === void 0 ? '' : _ref$className,
      _ref$style = _ref.style,
      style = _ref$style === void 0 ? {} : _ref$style,
      placeholder = _ref.placeholder,
      overlayClassName = _ref.overlayClassName,
      overlayStyle = _ref.overlayStyle,
      displayFromIconfontCN = _ref.displayFromIconfontCN,
      showClearBtn = _ref.showClearBtn,
      showColorSelect = _ref.showColorSelect;

  var _useState = useState(value || defaultValue),
      _useState2 = _slicedToArray(_useState, 2),
      iconType = _useState2[0],
      setIconType = _useState2[1];

  var _useState3 = useState(false),
      _useState4 = _slicedToArray(_useState3, 2),
      showDot = _useState4[0],
      setShowDot = _useState4[1];

  var _useState5 = useState(''),
      _useState6 = _slicedToArray(_useState5, 2),
      currentColor = _useState6[0],
      setCurrentColor = _useState6[1];

  var _useState7 = useState(''),
      _useState8 = _slicedToArray(_useState7, 2),
      selectedColor = _useState8[0],
      setSelectedColor = _useState8[1];

  var Component = allIcons[iconType];
  var departmentIconList = React.useMemo(function () {
    return Object.keys(allIcons).filter(function (iconName) {
      return iconName.indexOf('P') === 0 && iconName.substring(1, 2).match(/^.*[A-Z]+.*$/);
    });
  }, []);
  var IconFont = createFromIconfontCN({
    scriptUrl: displayFromIconfontCN
  });
  useEffect(function () {
    setCurrentColor(null);
  }, [currentColor]);
  useEffect(function () {
    setIconType(value);
  }, [value]);

  var handleHover = function handleHover(v) {
    if (showClearBtn && mode !== 'inline' && iconType) {
      setShowDot(v);
    }
  };

  var handleClick = function handleClick() {
    if (showClearBtn && mode !== 'inline' && iconType) {
      setIconType('');
      setShowDot(false);
      setCurrentColor('');
      setSelectedColor('');
    }
  };

  var defaultStyle = {
    padding: '8px',
    maxHeight: '250px',
    overflowY: 'auto'
  };

  var modalStaticEl = function modalStaticEl() {
    return /*#__PURE__*/React.createElement("div", {
      onMouseLeave: function onMouseLeave() {
        handleHover(false);
      },
      style: {
        display: 'inline-block'
      }
    }, /*#__PURE__*/React.createElement(Badge, {
      count: /*#__PURE__*/React.createElement(CloseCircleFilled, {
        style: {
          color: '#EF222C',
          fontSize: '16px',
          visibility: showDot ? 'visible' : 'hidden',
          opacity: showDot ? 1 : 0,
          transition: 'all 0.3s ease-in-out'
        },
        onClick: function onClick() {
          return handleClick();
        }
      })
    }, /*#__PURE__*/React.createElement(Button, {
      className: "cursor-pointer ".concat(className),
      style: style,
      onMouseEnter: function onMouseEnter() {
        handleHover(true);
      }
    }, !iconType ? placeholder || 'please select' : displayFromIconfontCN ? /*#__PURE__*/React.createElement(IconFont, {
      type: iconType,
      style: {
        color: selectedColor
      }
    }) : /*#__PURE__*/React.createElement(Component, {
      style: {
        color: selectedColor
      }
    }))));
  };

  var ModalEl = /*#__PURE__*/React.createElement(Popover, {
    overlayClassName: overlayClassName || "defaultOverlay",
    overlayStyle: overlayStyle || {
      width: '319px'
    },
    trigger: "click",
    placement: "topLeft",
    content: /*#__PURE__*/React.createElement(IconList, {
      dataSource: departmentIconList,
      currentIcon: iconType,
      setIconType: setIconType,
      iconChange: onChange,
      readonly: readonly,
      displayFromIconfontCN: displayFromIconfontCN,
      showColorSelect: showColorSelect,
      currentColor: currentColor,
      colorChange: setSelectedColor
    })
  }, modalStaticEl());
  return /*#__PURE__*/React.createElement(React.Fragment, null, mode === 'inline' ? /*#__PURE__*/React.createElement("div", {
    className: className,
    style: _objectSpread(_objectSpread({}, defaultStyle), style)
  }, /*#__PURE__*/React.createElement(IconList, {
    dataSource: departmentIconList,
    currentIcon: iconType,
    setIconType: setIconType,
    iconChange: onChange,
    readonly: readonly,
    displayFromIconfontCN: displayFromIconfontCN
  })) : readonly ? modalStaticEl() : ModalEl);
};

export default IconPicker;