import { computed, defineComponent, createVNode as _createVNode } from 'vue';
import { ColorBlock, Color as VcColor } from '../../vc-color-picker';
import { generateColor } from '../util';
import { useLocaleReceiver } from '../../locale/LocaleReceiver';
import defaultLocale from '../../locale/en_US';
import { useToken } from '../../theme/internal';
import useMergedState from '../../_util/hooks/useMergedState';
import Collapse from '../../collapse';
import classNames from '../../_util/classNames';
const colorPresetsProps = () => ({
  prefixCls: String,
  presets: Array,
  value: Object,
  onChange: Function
});
const genPresetColor = list => list.map(value => {
  value.colors = value.colors.map(generateColor);
  return value;
});
export const isBright = (value, bgColorToken) => {
  const {
    r,
    g,
    b,
    a
  } = value.toRgb();
  const hsv = new VcColor(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;
};
const genCollapsePanelKey = (preset, index) => {
  var _a;
  const mergedKey = (_a = preset.key) !== null && _a !== void 0 ? _a : index;
  return `panel-${mergedKey}`;
};
const ColorPresets = defineComponent({
  name: 'ColorPresets',
  props: colorPresetsProps(),
  setup(props) {
    const [locale] = useLocaleReceiver('ColorPicker', defaultLocale.ColorPicker);
    const [, token] = useToken();
    const _presets = computed(() => genPresetColor(props.presets));
    const [presetsValue] = useMergedState(_presets.value, {
      value: _presets,
      postState: genPresetColor
    });
    const colorPresetsPrefixCls = computed(() => `${props.prefixCls}-presets`);
    const activeKeys = 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: _createVNode("div", {
            "class": `${colorPresetsPrefixCls.value}-label`
          }, [preset === null || preset === void 0 ? void 0 : preset.label]),
          children: _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 _createVNode(ColorBlock, {
              "key": `preset-${index}-${presetColor.toHexString()}`,
              "color": generateColor(presetColor).toCssString(),
              "prefixCls": props.prefixCls,
              "className": classNames(`${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);
          }) : _createVNode("span", {
            "class": `${colorPresetsPrefixCls.value}-empty`
          }, [locale.value.presetEmpty])])
        };
      });
      return _createVNode("div", {
        "class": colorPresetsPrefixCls.value
      }, [_createVNode(Collapse, {
        "ghost": true,
        "items": items,
        "defaultActiveKey": activeKeys.value
      }, null)]);
    };
  }
});
export default ColorPresets;