import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import { createVNode as _createVNode } from "vue";
import { computed, defineComponent } from 'vue';
// import useConfigInject from '../../_util/hooks/useConfigInject';
import { useLocaleReceiver } from '../../locale-provider/LocaleReceiver';
import Collapse from '../../collapse';
import useMergedState from '../../_util/hooks/useMergedState';
import { ColorBlock } from '../../vc-color-picker';
import { generateColor } from '../util';
import classNames from '../../_util/classNames';
var Panel = Collapse.Panel;
var genPresetColor = function genPresetColor(list) {
  return list.map(function (value) {
    value.colors = value.colors.map(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 = defineComponent({
  name: 'ColorPresets',
  props: ['prefixCls', 'presets', 'value', 'onChange', 'color'],
  setup: function setup(props) {
    var _useLocaleReceiver = useLocaleReceiver('ColorPicker'),
      _useLocaleReceiver2 = _slicedToArray(_useLocaleReceiver, 1),
      locale = _useLocaleReceiver2[0];
    var colorPresetsPrefixCls = computed(function () {
      return "".concat(props.prefixCls, "-presets");
    });
    var presets = computed(function () {
      return genPresetColor(props.presets);
    });
    var _useMergedState = useMergedState(genPresetColor(props.presets), {
        value: presets,
        postState: genPresetColor
      }),
      _useMergedState2 = _slicedToArray(_useMergedState, 1),
      presetsValue = _useMergedState2[0];
    var activeKeys = 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 _createVNode("div", {
        "class": colorPresetsPrefixCls.value
      }, [_createVNode(Collapse, {
        "defaultActiveKey": activeKeys.value,
        "ghost": true
      }, {
        default: function _default() {
          return [presetsValue.value.map(function (preset) {
            var _preset$colors;
            return _createVNode(Panel, {
              "header": _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 [_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 _createVNode(ColorBlock, {
                    "key": "preset-".concat(presetColor.toHexString()),
                    "color": generateColor(presetColor).toRgbString(),
                    "prefixCls": props.prefixCls,
                    "class": classNames("".concat(colorPresetsPrefixCls.value, "-color"), (_classNames = {}, _defineProperty(_classNames, "".concat(colorPresetsPrefixCls.value, "-color-checked"), presetColor.toHexString() === ((_props$color = props.color) === null || _props$color === void 0 ? void 0 : _props$color.toHexString())), _defineProperty(_classNames, "".concat(colorPresetsPrefixCls.value, "-color-bright"), isBright(presetColor)), _classNames)),
                    "onClick": function onClick() {
                      return handleClick(presetColor);
                    }
                  }, null);
                }) : _createVNode("span", {
                  "class": "".concat(colorPresetsPrefixCls.value, "-empty")
                }, [locale.value.presetEmpty])])];
              }
            });
          })];
        }
      })]);
    };
  }
});
export default ColorPresets;