"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _vue = require("vue");
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _LocaleReceiver = require("../../locale-provider/LocaleReceiver");
var _collapse = _interopRequireDefault(require("../../collapse"));
var _useMergedState3 = _interopRequireDefault(require("../../_util/hooks/useMergedState"));
var _vcColorPicker = require("../../vc-color-picker");
var _util = require("../util");
var _classNames2 = _interopRequireDefault(require("../../_util/classNames"));
// import useConfigInject from '../../_util/hooks/useConfigInject';

var Panel = _collapse.default.Panel;
var genPresetColor = function genPresetColor(list) {
  return list.map(function (value) {
    value.colors = value.colors.map(_util.generateColor);
    return value;
  });
};
var isBright = function isBright(value) {
  var _value$toRgb = value.toRgb(),
    r = _value$toRgb.r,
    g = _value$toRgb.g,
    b = _value$toRgb.b,
    a = _value$toRgb.a;
  if (a <= 0.5) {
    return true;
  }
  return r * 0.299 + g * 0.587 + b * 0.114 > 192;
};
var ColorPresets = (0, _vue.defineComponent)({
  name: 'ColorPresets',
  props: ['prefixCls', 'presets', 'value', 'onChange', 'color'],
  setup: function setup(props) {
    var _useLocaleReceiver = (0, _LocaleReceiver.useLocaleReceiver)('ColorPicker'),
      _useLocaleReceiver2 = (0, _slicedToArray2.default)(_useLocaleReceiver, 1),
      locale = _useLocaleReceiver2[0];
    var colorPresetsPrefixCls = (0, _vue.computed)(function () {
      return "".concat(props.prefixCls, "-presets");
    });
    var presets = (0, _vue.computed)(function () {
      return genPresetColor(props.presets);
    });
    var _useMergedState = (0, _useMergedState3.default)(genPresetColor(props.presets), {
        value: presets,
        postState: genPresetColor
      }),
      _useMergedState2 = (0, _slicedToArray2.default)(_useMergedState, 1),
      presetsValue = _useMergedState2[0];
    var activeKeys = (0, _vue.computed)(function () {
      return presetsValue.value.map(function (preset) {
        return "panel-".concat(preset.label);
      });
    });
    var handleClick = function handleClick(colorValue) {
      var _props$onChange;
      (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, colorValue);
    };
    return function () {
      return (0, _vue.createVNode)("div", {
        "class": colorPresetsPrefixCls.value
      }, [(0, _vue.createVNode)(_collapse.default, {
        "defaultActiveKey": activeKeys.value,
        "ghost": true
      }, {
        default: function _default() {
          return [presetsValue.value.map(function (preset) {
            var _preset$colors;
            return (0, _vue.createVNode)(Panel, {
              "header": (0, _vue.createVNode)("div", {
                "class": "".concat(colorPresetsPrefixCls.value, "-label")
              }, [preset === null || preset === void 0 ? void 0 : preset.label]),
              "key": "panel-".concat(preset === null || preset === void 0 ? void 0 : preset.label)
            }, {
              default: function _default() {
                return [(0, _vue.createVNode)("div", {
                  "class": "".concat(colorPresetsPrefixCls.value, "-items")
                }, [Array.isArray(preset === null || preset === void 0 ? void 0 : preset.colors) && ((_preset$colors = preset.colors) === null || _preset$colors === void 0 ? void 0 : _preset$colors.length) > 0 ? preset.colors.map(function (presetColor) {
                  var _props$color, _classNames;
                  return (0, _vue.createVNode)(_vcColorPicker.ColorBlock, {
                    "key": "preset-".concat(presetColor.toHexString()),
                    "color": (0, _util.generateColor)(presetColor).toRgbString(),
                    "prefixCls": props.prefixCls,
                    "class": (0, _classNames2.default)("".concat(colorPresetsPrefixCls.value, "-color"), (_classNames = {}, (0, _defineProperty2.default)(_classNames, "".concat(colorPresetsPrefixCls.value, "-color-checked"), presetColor.toHexString() === ((_props$color = props.color) === null || _props$color === void 0 ? void 0 : _props$color.toHexString())), (0, _defineProperty2.default)(_classNames, "".concat(colorPresetsPrefixCls.value, "-color-bright"), isBright(presetColor)), _classNames)),
                    "onClick": function onClick() {
                      return handleClick(presetColor);
                    }
                  }, null);
                }) : (0, _vue.createVNode)("span", {
                  "class": "".concat(colorPresetsPrefixCls.value, "-empty")
                }, [locale.value.presetEmpty])])];
              }
            });
          })];
        }
      })]);
    };
  }
});
var _default2 = ColorPresets;
exports.default = _default2;