var __assign = this && this.__assign || function () {
  __assign = Object.assign || function (t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];

      for (var p in s) {
        if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
      }
    }

    return t;
  };

  return __assign.apply(this, arguments);
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import find from 'lodash/find';
import get from 'lodash/get';
import useToggle from 'react-use/lib/useToggle';
import { Dropmenu } from "../../dropdown";
import Circle from "./Circle";
import { CircleType, prefixCls, PRESET_COLORS_OPT } from "./constant";
import "../style/color-preset.css";
import Icon from "../../icon/Icon";
var useCallback = React.useCallback,
    useState = React.useState,
    useRef = React.useRef;

var ColorPreset = function ColorPreset(_a) {
  var actived = _a.actived,
      color = _a.color,
      onChange = _a.onChange,
      className = _a.className,
      getPopupContainer = _a.getPopupContainer,
      _b = _a.colorPresetOptions,
      colorPresetOptions = _b === void 0 ? PRESET_COLORS_OPT : _b,
      _c = _a.value,
      value = _c === void 0 ? PRESET_COLORS_OPT[0].key : _c;
  var ref = useRef(null);

  var _d = useToggle(true),
      on = _d[0],
      toggle = _d[1];

  var _e = useState(value),
      val = _e[0],
      setValues = _e[1];

  var fd = find(PRESET_COLORS_OPT, function (t) {
    return t.key === val;
  }) || PRESET_COLORS_OPT[0];
  var renderDropNode = useCallback(function (actived) {
    return _jsxs("span", __assign({
      className: classNames(prefixCls + "-preset-trigger", {
        actived: actived
      })
    }, {
      children: [get(fd, 'title'), _jsx(Icon, {
        size: 14,
        icon: "unfold-more"
      }, void 0)]
    }), void 0);
  }, [fd]);
  var handleChange = useCallback(function (item) {
    setValues(item.key);
  }, []);
  var colorChange = useCallback(function (c) {
    if (onChange) {
      onChange(c, CircleType.preset);
    }
  }, [onChange]);
  var expandCls = classNames(prefixCls + "-preset-expand", {
    expand: on
  });
  return _jsxs("div", __assign({
    ref: ref,
    className: classNames(prefixCls + "-preset", className)
  }, {
    children: [_jsxs("div", __assign({
      className: prefixCls + "-preset-top"
    }, {
      children: [_jsx(Dropmenu, __assign({
        values: [val],
        onClickMenuItem: handleChange,
        menus: colorPresetOptions,
        openClassName: prefixCls + "-preset-drop",
        getPopupContainer: getPopupContainer,
        renderDropNode: renderDropNode
      }, {
        children: val
      }), void 0), _jsx(Icon, {
        icon: "chevron-down",
        className: expandCls,
        onClick: toggle
      }, void 0)]
    }), void 0), on && _jsx(Circle, {
      actived: actived,
      color: color,
      colors: fd.colors,
      circleSize: 17,
      circleSpacing: 11,
      onChange: colorChange
    }, void 0)]
  }), void 0);
};

ColorPreset.displayName = 'ColorPreset';
export default ColorPreset;
var ColorPresetMemo = /*#__PURE__*/React.memo(ColorPreset);
ColorPresetMemo.displayName = 'ColorPresetMemo';
export { ColorPresetMemo };

