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);
};

var __spreadArray = this && this.__spreadArray || function (to, from) {
  for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) {
    to[j] = from[i];
  }

  return to;
};

import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import * as React from 'react';
import classNames from 'classnames';
import useToggle from 'react-use/lib/useToggle';
import { CircleType, prefixCls } from "./..";
import Circle from "./Circle";
import Icon from "../../icon";
import "../style/color-favor.css";

var ColorFavor = function ColorFavor(_a) {
  var color = _a.color,
      actived = _a.actived,
      labelFavor = _a.labelFavor,
      onChange = _a.onChange,
      className = _a.className,
      _b = _a.colors,
      colors = _b === void 0 ? [] : _b,
      labelFavorPlaceholder = _a.labelFavorPlaceholder,
      onChangeColors = _a.onChangeColors;

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

  var addColor = React.useCallback(function () {
    if (colors === null || colors === void 0 ? void 0 : colors.every(function (c) {
      return c !== color;
    })) {
      var newColors = __spreadArray(__spreadArray([], colors), [color]);

      onChangeColors && onChangeColors(newColors);
      return newColors;
    }
  }, [colors, color, onChangeColors]);
  var handleChange = React.useCallback(function (c) {
    if (onChange) {
      onChange(c, CircleType.favor);
    }
  }, [onChange]);

  var handleInnerDelete = function handleInnerDelete(index) {
    var newColors = __spreadArray([], colors);

    newColors.splice(index, 1);
    onChangeColors && onChangeColors(newColors);
    return newColors;
  };

  var expandCls = classNames(prefixCls + "-favor-expand", {
    expand: on
  });
  return _jsxs("div", __assign({
    className: classNames(prefixCls + "-favor", className)
  }, {
    children: [_jsxs("div", __assign({
      className: prefixCls + "-favor-top"
    }, {
      children: [_jsx("div", __assign({
        className: prefixCls + "-favor-label"
      }, {
        children: labelFavor
      }), void 0), _jsxs("div", __assign({
        className: prefixCls + "-favor-btns"
      }, {
        children: [_jsx(Icon, {
          icon: "add",
          onClick: addColor,
          className: prefixCls + "-favor-add"
        }, void 0), _jsx(Icon, {
          icon: "chevron-down",
          onClick: toggle,
          className: expandCls
        }, void 0)]
      }), void 0)]
    }), void 0), on && colors.length ? _jsx(Circle, {
      actived: actived,
      onDelete: handleInnerDelete,
      color: color,
      colors: colors,
      onChange: handleChange
    }, void 0) : _jsxs("div", __assign({
      className: prefixCls + "-favor-placeholder"
    }, {
      children: [_jsx("span", {
        className: prefixCls + "-favor-placeholder-circle"
      }, void 0), _jsx("span", {
        children: labelFavorPlaceholder
      }, void 0)]
    }), void 0)]
  }), void 0);
};

ColorFavor.defaultProps = {
  labelFavor: '收藏的颜色',
  labelFavorPlaceholder: '点击“+”添加颜色'
};
ColorFavor.displayName = 'ColorFavor';
export default ColorFavor;
var ColorFavorMemo = /*#__PURE__*/React.memo(ColorFavor);
ColorFavorMemo.displayName = 'ColorFavorMemo';
export { ColorFavorMemo };

