/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var hooks_useConfig = require('../../hooks/useConfig.js');
var colorPicker_props = require('../props.js');
var colorPicker_hooks = require('../hooks.js');
var hooks_useVModel = require('../../hooks/useVModel.js');
var hooks_useDefaultValue = require('../../hooks/useDefaultValue.js');
var colorPicker_panel_header = require('./header.js');
var colorPicker_panel_linearGradient = require('./linear-gradient.js');
var colorPicker_panel_saturation = require('./saturation.js');
var colorPicker_panel_hue = require('./hue.js');
var colorPicker_panel_alpha = require('./alpha.js');
var colorPicker_panel_format_index = require('./format/index.js');
var colorPicker_panel_swatches = require('./swatches.js');
var configProvider_useConfig = require('../../config-provider/useConfig.js');
var _common_js_colorPicker_constants = require('../../_common/js/color-picker/constants.js');
var _common_js_colorPicker_color = require('../../_common/js/color-picker/color.js');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
require('../../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../../_chunks/dep-7f0559c3.js');
require('dayjs');
require('lodash/kebabCase');
require('../../radio/index.js');
require('../../utils/withInstall.js');
require('../../radio/radio.js');
require('lodash/isUndefined');
require('../../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/camelCase');
require('lodash/isNull');
require('lodash/isNumber');
require('../../form/hooks.js');
require('../../hooks/tnode.js');
require('../../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../../radio/constants.js');
require('../../radio/props.js');
require('../../radio/group.js');
require('@babel/runtime/helpers/asyncToGenerator');
require('@babel/runtime/regenerator');
require('lodash/isNil');
require('../../watermark/hooks.js');
require('../../radio/radio-group-props.js');
require('../../radio/useKeyboard.js');
require('../../utils/dom.js');
require('../../utils/easing.js');
require('../../radio/radio-button.js');
require('../../input-number/index.js');
require('../../input-number/input-number.js');
require('@ldesign/icons-vue-next');
require('../../button/index.js');
require('../../button/button.js');
require('../../loading/index.js');
require('../../loading/directive.js');
require('../../loading/plugin.js');
require('../../loading/loading.js');
require('../../hooks/useTeleport.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../_common/js/loading/circle-adapter.js');
require('../../_common/js/utils/set-style.js');
require('../../_common/js/utils/helper.js');
require('../../hooks/useRipple.js');
require('../../utils/set-style.js');
require('../../hooks/useKeepAnimation.js');
require('../../button/props.js');
require('../../input/index.js');
require('../../input/input.js');
require('../../hooks/useGlobalIcon.js');
require('../../input/props.js');
require('../../input/useInput.js');
require('../../form/const.js');
require('../../input/useLengthLimit.js');
require('../../_common/js/log/log.js');
require('../../input/useInputEventHandler.js');
require('../../input/useInputWidth.js');
require('../../input/input-group.js');
require('../../input/input-group-props.js');
require('../../input-number/props.js');
require('../../input-number/useInputNumber.js');
require('../../hooks/useCommonClassName.js');
require('../../_common/js/input-number/number.js');
require('../../_common/js/input-number/large-number.js');
require('./base-props.js');
require('../../_common/js/color-picker/draggable.js');
require('@babel/runtime/helpers/classCallCheck');
require('@babel/runtime/helpers/createClass');
require('./slider.js');
require('lodash/upperCase');
require('../const.js');
require('../../select/index.js');
require('../../select/select.js');
require('lodash/pick');
require('lodash/debounce');
require('lodash/get');
require('lodash/intersection');
require('../../common-components/fake-arrow.js');
require('../../select-input/index.js');
require('../../select-input/select-input.js');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../popup/container.js');
require('../../hooks/useResizeObserver.js');
require('../../popup/props.js');
require('../../select-input/props.js');
require('../../select-input/useSingle.js');
require('../../select-input/useMultiple.js');
require('../../tag-input/index.js');
require('../../tag-input/tag-input.js');
require('../../tag-input/props.js');
require('../../tag-input/hooks/useTagScroll.js');
require('../../tag-input/useTagList.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../tag/check-tag-props.js');
require('../../tag-input/hooks/useHover.js');
require('../../tag-input/hooks/useDragSorter.js');
require('../../select-input/useOverlayInnerStyle.js');
require('../../select/props.js');
require('../../select/select-panel.js');
require('lodash/omit');
require('../../select/option.js');
require('../../checkbox/index.js');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../checkbox/constants.js');
require('../../checkbox/group.js');
require('../../hooks/slot.js');
require('../../checkbox/checkbox-group-props.js');
require('../../select/option-props.js');
require('../../select/helper.js');
require('../../select/optionGroup.js');
require('../../select/option-group-props.js');
require('../../select/hooks/usePanelVirtualScroll.js');
require('../../hooks/useVirtualScroll.js');
require('../../select/hooks/useSelectOptions.js');
require('./format/inputs.js');
require('lodash/throttle');
require('./format/config.js');
require('tinycolor2');
require('../../_common/js/color-picker/cmyk.js');
require('../../_common/js/color-picker/gradient.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var ColorPanel = vue.defineComponent({
  name: "ColorPanel",
  props: _objectSpread(_objectSpread({}, colorPicker_props["default"]), {}, {
    togglePopup: {
      type: Function
    }
  }),
  setup: function setup(props2) {
    var baseClassName = colorPicker_hooks.useBaseClassName();
    var _useCommonClassName = hooks_useConfig.useCommonClassName(),
      STATUS = _useCommonClassName.STATUS;
    var _useConfig = configProvider_useConfig.useConfig("colorPicker"),
      t = _useConfig.t,
      globalConfig = _useConfig.globalConfig;
    var statusClassNames = STATUS.value;
    var _toRefs = vue.toRefs(props2),
      inputValue = _toRefs.value,
      modelValue = _toRefs.modelValue,
      recentColors = _toRefs.recentColors;
    var _useVModel = hooks_useVModel["default"](inputValue, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      innerValue = _useVModel2[0],
      setInnerValue = _useVModel2[1];
    var color = vue.ref(new _common_js_colorPicker_color.Color(innerValue.value || _common_js_colorPicker_constants.DEFAULT_COLOR));
    var updateColor = function updateColor() {
      return color.value.update(innerValue.value || _common_js_colorPicker_constants.DEFAULT_COLOR);
    };
    var mode = vue.ref(color.value.isGradient ? "linear-gradient" : "monochrome");
    var formatModel = vue.ref(color.value.isGradient ? "CSS" : "RGB");
    var _useDefaultValue = hooks_useDefaultValue["default"](recentColors, props2.defaultRecentColors, props2.onRecentColorsChange, "recentColors"),
      _useDefaultValue2 = _slicedToArray__default["default"](_useDefaultValue, 2),
      recentlyUsedColors = _useDefaultValue2[0],
      setRecentlyUsedColors = _useDefaultValue2[1];
    if (props2.colorModes.length === 1) {
      var m = props2.colorModes[0];
      mode.value = m;
    }
    var formatValue = function formatValue() {
      if (mode.value === "linear-gradient") return color.value.linearGradient;
      return color.value.getFormatsColorMap()[props2.format] || color.value.css;
    };
    var addRecentlyUsedColor = function addRecentlyUsedColor() {
      if (recentlyUsedColors.value === null || recentlyUsedColors.value === false) return;
      var colors = recentlyUsedColors.value || [];
      var currentColor = color.value.isGradient ? color.value.linearGradient : color.value.rgba;
      var index = colors.indexOf(currentColor);
      if (index > -1) colors.splice(index, 1);
      colors.unshift(currentColor);
      if (colors.length > _common_js_colorPicker_constants.TD_COLOR_USED_COLORS_MAX_SIZE) colors.length = _common_js_colorPicker_constants.TD_COLOR_USED_COLORS_MAX_SIZE;
      handleRecentlyUsedColorsChange(colors);
    };
    var handleRecentlyUsedColorsChange = function handleRecentlyUsedColorsChange(colors) {
      recentlyUsedColors.value = colors;
      setRecentlyUsedColors(colors);
    };
    var emitColorChange = function emitColorChange(trigger) {
      setInnerValue(formatValue(), {
        color: _common_js_colorPicker_color.getColorObject(color.value),
        trigger: trigger || "palette-saturation-brightness"
      });
    };
    vue.watch(function () {
      return [props2.defaultValue, props2.enableAlpha];
    }, updateColor);
    vue.watch(function () {
      return innerValue.value;
    }, function (newColor) {
      if (newColor !== formatValue()) {
        updateColor();
        mode.value = color.value.isGradient ? "linear-gradient" : "monochrome";
      }
    });
    var handleModeChange = function handleModeChange(value) {
      mode.value = value;
      if (value === "linear-gradient") {
        color.value.update(color.value.gradientColors.length > 0 ? color.value.linearGradient : _common_js_colorPicker_constants.DEFAULT_LINEAR_GRADIENT);
      } else {
        color.value.update(color.value.rgba);
      }
      emitColorChange();
    };
    var handleFormatModeChange = function handleFormatModeChange(format) {
      return formatModel.value = format;
    };
    var handleSatAndValueChange = function handleSatAndValueChange(_ref) {
      var saturation = _ref.saturation,
        value = _ref.value;
      var _color$value = color.value,
        sat = _color$value.saturation,
        val = _color$value.value;
      var changeTrigger = "palette-saturation-brightness";
      if (value !== val && saturation !== sat) {
        color.value.saturation = saturation;
        color.value.value = value;
        changeTrigger = "palette-saturation-brightness";
      } else if (saturation !== sat) {
        color.value.saturation = saturation;
        changeTrigger = "palette-saturation";
      } else if (value !== val) {
        color.value.value = value;
        changeTrigger = "palette-brightness";
      } else {
        return;
      }
      emitColorChange(changeTrigger);
    };
    var handleHueChange = function handleHueChange(hue) {
      var _props2$onPaletteBarC;
      color.value.hue = hue;
      emitColorChange("palette-hue-bar");
      (_props2$onPaletteBarC = props2.onPaletteBarChange) === null || _props2$onPaletteBarC === void 0 ? void 0 : _props2$onPaletteBarC.call(props2, {
        color: _common_js_colorPicker_color.getColorObject(color.value)
      });
    };
    var handleAlphaChange = function handleAlphaChange(alpha) {
      color.value.alpha = alpha;
      emitColorChange("palette-alpha-bar");
    };
    var handleInputChange = function handleInputChange(input, alpha) {
      color.value.update(input);
      color.value.alpha = alpha;
      emitColorChange("input");
    };
    var handleGradientChange = function handleGradientChange(_ref2) {
      var key = _ref2.key,
        payload = _ref2.payload;
      var trigger = "palette-saturation-brightness";
      switch (key) {
        case "degree":
          color.value.gradientDegree = payload;
          trigger = "input";
          break;
        case "selectedId":
          color.value.gradientSelectedId = payload;
          break;
        case "colors":
          color.value.gradientColors = payload;
          break;
      }
      emitColorChange(trigger);
    };
    var handleSetColor = function handleSetColor(type, value) {
      var isGradientValue = _common_js_colorPicker_color.Color.isGradientColor(value);
      if (isGradientValue) {
        if (props2.colorModes.includes("linear-gradient")) {
          mode.value = "linear-gradient";
          color.value.update(value);
          color.value.updateCurrentGradientColor();
        } else {
          console.warn("\u8BE5\u6A21\u5F0F\u4E0D\u652F\u6301\u6E10\u53D8\u8272");
        }
      } else if (mode.value === "linear-gradient") {
        color.value.updateStates(value);
        color.value.updateCurrentGradientColor();
      } else {
        color.value.update(value);
      }
      emitColorChange();
    };
    return {
      baseClassName: baseClassName,
      statusClassNames: statusClassNames,
      t: t,
      globalConfig: globalConfig,
      color: color,
      mode: mode,
      formatModel: formatModel,
      recentlyUsedColors: recentlyUsedColors,
      addRecentlyUsedColor: addRecentlyUsedColor,
      handleModeChange: handleModeChange,
      handleSatAndValueChange: handleSatAndValueChange,
      handleHueChange: handleHueChange,
      handleAlphaChange: handleAlphaChange,
      handleGradientChange: handleGradientChange,
      handleSetColor: handleSetColor,
      handleFormatModeChange: handleFormatModeChange,
      handleInputChange: handleInputChange,
      handleRecentlyUsedColorsChange: handleRecentlyUsedColorsChange
    };
  },
  render: function render() {
    var _systemColors,
      _this = this;
    var t = this.t,
      baseClassName = this.baseClassName,
      statusClassNames = this.statusClassNames,
      globalConfig = this.globalConfig,
      recentColors = this.recentColors,
      swatchColors = this.swatchColors,
      showPrimaryColorPreview = this.showPrimaryColorPreview;
    var baseProps = {
      color: this.color,
      disabled: this.disabled
    };
    var showUsedColors = recentColors !== null && recentColors !== false;
    var systemColors = swatchColors;
    if (systemColors === void 0) systemColors = _toConsumableArray__default["default"](_common_js_colorPicker_constants.DEFAULT_SYSTEM_SWATCH_COLORS);
    var showSystemColors = ((_systemColors = systemColors) === null || _systemColors === void 0 ? void 0 : _systemColors.length) > 0;
    var renderSwatches = function renderSwatches() {
      if (!showSystemColors && !showUsedColors) return null;
      return vue.createVNode(vue.Fragment, null, [vue.createVNode("div", {
        "class": "".concat(baseClassName, "__swatches-wrap")
      }, [showUsedColors ? vue.createVNode(colorPicker_panel_swatches["default"], vue.mergeProps(baseProps, {
        "title": t(globalConfig.recentColorTitle),
        "editable": true,
        "colors": _this.recentlyUsedColors,
        "handleAddColor": _this.addRecentlyUsedColor,
        "onSetColor": function onSetColor(color) {
          return _this.handleSetColor("used", color);
        },
        "onChange": _this.handleRecentlyUsedColorsChange
      }), null) : null, showSystemColors ? vue.createVNode(colorPicker_panel_swatches["default"], vue.mergeProps(baseProps, {
        "title": t(globalConfig.swatchColorTitle),
        "colors": systemColors,
        "onSetColor": function onSetColor(color) {
          return _this.handleSetColor("system", color);
        }
      }), null) : null])]);
    };
    var isGradient = this.mode === "linear-gradient";
    return vue.createVNode("div", {
      "class": ["".concat(baseClassName, "__panel"), this.disabled ? statusClassNames.disabled : false]
    }, [vue.createVNode(colorPicker_panel_header["default"], vue.mergeProps(this.$props, {
      "mode": this.mode,
      "onModeChange": this.handleModeChange
    }), null), vue.createVNode("div", {
      "class": ["".concat(baseClassName, "__body")]
    }, [isGradient ? vue.createVNode(colorPicker_panel_linearGradient["default"], vue.mergeProps(baseProps, {
      "onChange": this.handleGradientChange
    }), null) : null, vue.createVNode(colorPicker_panel_saturation["default"], vue.mergeProps(baseProps, {
      "onChange": this.handleSatAndValueChange
    }), null), vue.createVNode("div", {
      "class": ["".concat(baseClassName, "__sliders-wrapper")]
    }, [vue.createVNode("div", {
      "class": ["".concat(baseClassName, "__sliders")]
    }, [vue.createVNode(colorPicker_panel_hue["default"], vue.mergeProps(baseProps, {
      "onChange": this.handleHueChange
    }), null), this.enableAlpha ? vue.createVNode(colorPicker_panel_alpha["default"], vue.mergeProps(baseProps, {
      "onChange": this.handleAlphaChange
    }), null) : null]), showPrimaryColorPreview ? vue.createVNode("div", {
      "class": ["".concat(baseClassName, "__sliders-preview"), "".concat(baseClassName, "--bg-alpha")]
    }, [vue.createVNode("span", {
      "class": "".concat(baseClassName, "__sliders-preview-inner"),
      "style": {
        background: isGradient ? this.color.linearGradient : this.color.rgba
      }
    }, null)]) : null]), vue.createVNode(colorPicker_panel_format_index["default"], vue.mergeProps(this.$props, {
      "color": this.color,
      "format": this.formatModel,
      "onModeChange": this.handleFormatModeChange,
      "onInputChange": this.handleInputChange
    }), null), renderSwatches()])]);
  }
});

exports["default"] = ColorPanel;
//# sourceMappingURL=index.js.map
