"use strict";

var __assign = void 0 && (void 0).__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 __createBinding = void 0 && (void 0).__createBinding || (Object.create ? function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  Object.defineProperty(o, k2, {
    enumerable: true,
    get: function get() {
      return m[k];
    }
  });
} : function (o, m, k, k2) {
  if (k2 === undefined) k2 = k;
  o[k2] = m[k];
});

var __setModuleDefault = void 0 && (void 0).__setModuleDefault || (Object.create ? function (o, v) {
  Object.defineProperty(o, "default", {
    enumerable: true,
    value: v
  });
} : function (o, v) {
  o["default"] = v;
});

var __importStar = void 0 && (void 0).__importStar || function (mod) {
  if (mod && mod.__esModule) return mod;
  var result = {};
  if (mod != null) for (var k in mod) {
    if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  }

  __setModuleDefault(result, mod);

  return result;
};

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

  return to;
};

var __importDefault = void 0 && (void 0).__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ColorFavorMemo = void 0;

var jsx_runtime_1 = require("react/jsx-runtime");

var React = __importStar(require("react"));

var classnames_1 = __importDefault(require("classnames"));

var useToggle_1 = __importDefault(require("react-use/lib/useToggle"));

var color_picker_1 = require("./..");

var Circle_1 = __importDefault(require("./Circle"));

var icon_1 = __importDefault(require("../../icon"));

require("../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_1["default"](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, color_picker_1.CircleType.favor);
    }
  }, [onChange]);

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

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

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

ColorFavor.defaultProps = {
  labelFavor: '收藏的颜色',
  labelFavorPlaceholder: '点击“+”添加颜色'
};
ColorFavor.displayName = 'ColorFavor';
exports["default"] = ColorFavor;
var ColorFavorMemo = React.memo(ColorFavor);
exports.ColorFavorMemo = ColorFavorMemo;
ColorFavorMemo.displayName = 'ColorFavorMemo';

