"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isBright = exports.default = void 0;
var _vue = require("vue");
var _vcColorPicker = require("../../vc-color-picker");
var _util = require("../util");
var _LocaleReceiver = require("../../locale/LocaleReceiver");
var _en_US = _interopRequireDefault(require("../../locale/en_US"));
var _internal = require("../../theme/internal");
var _useMergedState = _interopRequireDefault(require("../../_util/hooks/useMergedState"));
var _collapse = _interopRequireDefault(require("../../collapse"));
var _classNames = _interopRequireDefault(require("../../_util/classNames"));
const colorPresetsProps = () => ({
  prefixCls: String,
  presets: Array,
  value: Object,
  onChange: Function
});
const genPresetColor = list => list.map(value => {
  value.colors = value.colors.map(_util.generateColor);
  return value;
});
const isBright = (value, bgColorToken) => {
  const {
    r,
    g,
    b,
    a
  } = value.toRgb();
  const hsv = new _vcColorPicker.Color(value.toRgbString()).onBackground(bgColorToken).toHsv();
  if (a <= 0.5) {
    // Adapted to dark mode
    return hsv.v > 0.5;
  }
  return r * 0.299 + g * 0.587 + b * 0.114 > 192;
};
exports.isBright = isBright;
const genCollapsePanelKey = (preset, index) => {
  var _a;
  const mergedKey = (_a = preset.key) !== null && _a !== void 0 ? _a : index;
  return `panel-${mergedKey}`;
};
const ColorPresets = (0, _vue.defineComponent)({
  name: 'ColorPresets',
  props: colorPresetsProps(),
  setup(props) {
    const [locale] = (0, _LocaleReceiver.useLocaleReceiver)('ColorPicker', _en_US.default.ColorPicker);
    const [, token] = (0, _internal.useToken)();
    const _presets = (0, _vue.computed)(() => genPresetColor(props.presets));
    const [presetsValue] = (0, _useMergedState.default)(_presets.value, {
      value: _presets,
      postState: genPresetColor
    });
    const colorPresetsPrefixCls = (0, _vue.computed)(() => `${props.prefixCls}-presets`);
    const activeKeys = (0, _vue.computed)(() => {
      return presetsValue.value.reduce((acc, preset, index) => {
        const {
          defaultOpen = true
        } = preset;
        if (defaultOpen) {
          acc.push(genCollapsePanelKey(preset, index));
        }
        return acc;
      }, []);
    });
    const handleClick = colorValue => {
      var _a;
      (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, colorValue);
    };
    return () => {
      const items = presetsValue.value.map((preset, index) => {
        var _a;
        return {
          key: genCollapsePanelKey(preset, index),
          label: (0, _vue.createVNode)("div", {
            "class": `${colorPresetsPrefixCls.value}-label`
          }, [preset === null || preset === void 0 ? void 0 : preset.label]),
          children: (0, _vue.createVNode)("div", {
            "class": `${colorPresetsPrefixCls.value}-items`
          }, [Array.isArray(preset === null || preset === void 0 ? void 0 : preset.colors) && ((_a = preset.colors) === null || _a === void 0 ? void 0 : _a.length) > 0 ? preset.colors.map((presetColor, index) => {
            var _a;
            return (0, _vue.createVNode)(_vcColorPicker.ColorBlock, {
              "key": `preset-${index}-${presetColor.toHexString()}`,
              "color": (0, _util.generateColor)(presetColor).toCssString(),
              "prefixCls": props.prefixCls,
              "className": (0, _classNames.default)(`${colorPresetsPrefixCls.value}-color`, {
                [`${colorPresetsPrefixCls.value}-color-checked`]: presetColor.toHexString() === ((_a = props.value) === null || _a === void 0 ? void 0 : _a.toHexString()),
                [`${colorPresetsPrefixCls.value}-color-bright`]: isBright(presetColor, token.value.colorBgElevated)
              }),
              "onClick": () => handleClick(presetColor)
            }, null);
          }) : (0, _vue.createVNode)("span", {
            "class": `${colorPresetsPrefixCls.value}-empty`
          }, [locale.value.presetEmpty])])
        };
      });
      return (0, _vue.createVNode)("div", {
        "class": colorPresetsPrefixCls.value
      }, [(0, _vue.createVNode)(_collapse.default, {
        "ghost": true,
        "items": items,
        "defaultActiveKey": activeKeys.value
      }, null)]);
    };
  }
});
var _default = exports.default = ColorPresets;