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 } from "react/jsx-runtime";
import * as React from 'react';
import findIndex from 'lodash/findIndex';
import { ColorWrap } from 'react-color/lib/components/common';
import CircleSwatch from "./CircleSwatch";
import { prefixCls } from "./constant";
import "../style/circle.css";
import { useEffect } from 'react';

var Circle = function Circle(_a) {
  var onDelete = _a.onDelete,
      hex = _a.hex,
      actived = _a.actived,
      onChange = _a.onChange,
      onSwatchHover = _a.onSwatchHover,
      _b = _a.colors,
      colors = _b === void 0 ? [] : _b;

  var _c = React.useState(findIndex(colors, function (t) {
    return t.toLowerCase() === hex;
  })),
      index = _c[0],
      setIndex = _c[1];

  var handleChange = React.useCallback(function (hexCode, e, idx) {
    onChange({
      hex: hexCode,
      source: 'hex'
    }, e);
    setIndex(idx);
  }, [onChange]);
  useEffect(function () {
    setIndex(findIndex(colors, function (t) {
      return t.toLowerCase() === hex;
    }));
  }, [colors, hex]);
  return _jsx("div", __assign({
    className: prefixCls + "-circle"
  }, {
    children: colors.map(function (c, i) {
      return _jsx(CircleSwatch, {
        color: c,
        index: i,
        active: !!actived && i === index,
        onDelete: onDelete,
        onSwatchHover: onSwatchHover,
        onClick: function onClick(hex1, e) {
          return handleChange(hex1, e, i);
        }
      }, c);
    })
  }), void 0);
};

var CircleWrap = ColorWrap(Circle);
var CircleMemo = /*#__PURE__*/React.memo(CircleWrap);
CircleMemo.displayName = 'Circle';
export { CircleMemo };
export default CircleWrap;

