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

import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { defineComponent, toRefs, ref, watch, computed } from '@vue/composition-api';
import '../../hooks/useConfig.js';
import useCommonClassName from '../../hooks/useCommonClassName.js';
import props from '../props.js';
import '../const.js';
import '../utils/index.js';
import { useBaseClassName } from '../hooks.js';
import useVModel from '../../hooks/useVModel.js';
import useDefaultValue from '../../hooks/useDefaultValue.js';
import PanelHeader from './header.js';
import LinearGradient from './linear-gradient.js';
import SaturationPanel from './saturation.js';
import HueSlider from './hue.js';
import AlphaSlider from './alpha.js';
import FormatPanel from './format/index.js';
import SwatchesPanel from './swatches.js';
import { useConfig } from '../../config-provider/useConfig.js';
import { Color, getColorObject } from '../../_common/js/color-picker/color.js';
import { DEFAULT_COLOR, TD_COLOR_USED_COLORS_MAX_SIZE, DEFAULT_LINEAR_GRADIENT, DEFAULT_SYSTEM_SWATCH_COLORS } from '../../_common/js/color-picker/constants.js';
import '../../config-provider/context.js';
import 'lodash/merge';
import 'lodash/mergeWith';
import '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../../_chunks/dep-be599ec8.js';
import 'dayjs';
import '../../_chunks/dep-67ff4010.js';
import '../../config-provider/type.js';
import '../../_common/js/color-picker/index.js';
import '../../_common/js/color-picker/cmyk.js';
import '../../_common/js/color-picker/draggable.js';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';
import '../../_common/js/color-picker/gradient.js';
import 'lodash/isString';
import 'lodash/isNull';
import 'tinycolor2';
import 'lodash/kebabCase';
import '../../radio/index.js';
import '../../utils/map-props.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'vue';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import '../../radio/radio.js';
import '../../_chunks/dep-c89aafbb.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import '../../utils/render-tnode.js';
import '../../utils/event.js';
import '../../config-provider/config-receiver.js';
import '../../utils/mixins.js';
import '../../radio/props.js';
import '../../radio/group.js';
import 'lodash/isNumber';
import '../../utils/dom.js';
import 'raf';
import '../../utils/easing.js';
import '../../radio/radio-group-props.js';
import '../../radio/radio-button.js';
import '../../radio/style/css.js';
import '../../radio/type.js';
import 'lodash/cloneDeep';
import '../../input-number/index.js';
import '../../input-number/input-number.js';
import '@ldesign/icons-vue';
import '../../button/index.js';
import '../../button/button.js';
import '../../loading/index.js';
import '../../loading/directive.js';
import 'lodash/isObject';
import 'lodash/mapKeys';
import 'lodash/isEqual';
import '../../loading/plugin.js';
import '../../config.js';
import '../../loading/loading.js';
import '../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../_common/js/loading/circle-adapter.js';
import '../../_common/js/utils/set-style.js';
import '../../_common/js/utils/helper.js';
import 'lodash/isUndefined';
import 'lodash/isArray';
import '../../loading/style/css.js';
import '../../loading/type.js';
import '../../utils/ripple.js';
import '../../button/props.js';
import '../../button/style/css.js';
import '../../button/type.js';
import '../../input/index.js';
import '../../input/input.js';
import '../../_common/js/log/index.js';
import '../../_common/js/log/log.js';
import '../../input/props.js';
import '../../input/input-group.js';
import '../../input/style/css.js';
import '../../input/type.js';
import '../../hooks/useGlobalIcon.js';
import '../../input-number/props.js';
import '../../input-number/useInputNumber.js';
import '../../_common/js/input-number/number.js';
import 'lodash/isNil';
import '../../_common/js/input-number/large-number.js';
import '../../hooks/useFormDisabled.js';
import '../../input-number/style/css.js';
import '../../input-number/type.js';
import './base-props.js';
import '../../select/index.js';
import '../../select/select.js';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import '../../hooks/tnode.js';
import 'lodash/isEmpty';
import '../../hooks/render-tnode.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../popup/utils.js';
import '../../popup/trigger.js';
import '../../popup/style/css.js';
import '../../popup/type.js';
import '../../popup/plugin.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/check-tag-props.js';
import '../../tag/style/css.js';
import '../../tag/type.js';
import '../../select-input/index.js';
import '../../select-input/select-input.js';
import '../../select-input/props.js';
import '../../select-input/useSingle.js';
import 'lodash/pick';
import '../../select-input/useMultiple.js';
import '../../tag-input/index.js';
import '../../tag-input/tag-input.js';
import '../../tag-input/props.js';
import '../../tag-input/hooks/useTagScroll.js';
import '../../tag-input/useTagList.js';
import '../../tag-input/hooks/useHover.js';
import '../../tag-input/hooks/useDragSorter.js';
import '../../tag-input/style/css.js';
import '../../tag-input/type.js';
import '../../tag-input/interface.js';
import '../../select-input/useOverlayInnerStyle.js';
import '../../select-input/style/css.js';
import '../../select-input/type.js';
import '../../common-components/fake-arrow.js';
import '../../select/option.js';
import '../../checkbox/index.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../checkbox/checkbox-group-props.js';
import '../../checkbox/style/css.js';
import '../../checkbox/type.js';
import '../../hooks/useLazyLoad.js';
import '../../_common/js/utils/observe.js';
import '../../select/util.js';
import '../../select/option-props.js';
import '../../select/select-panel.js';
import '../../hooks/useVirtualScroll.js';
import '../../select/hooks/useSelectOptions.js';
import '../../select/props.js';
import '../../select/optionGroup.js';
import '../../select/option-group-props.js';
import '../../select/style/css.js';
import '../../select/type.js';
import './slider.js';
import 'lodash/upperCase';
import './format/inputs.js';
import 'lodash/throttle';
import './format/config.js';
import 'lodash';

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(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 = defineComponent({
  name: "ColorPanel",
  components: {
    PanelHeader: PanelHeader,
    LinearGradient: LinearGradient,
    SaturationPanel: SaturationPanel,
    HueSlider: HueSlider,
    AlphaSlider: AlphaSlider,
    FormatPanel: FormatPanel,
    SwatchesPanel: SwatchesPanel
  },
  props: _objectSpread(_objectSpread({}, props), {}, {
    togglePopup: {
      type: Function
    }
  }),
  setup: function setup(props2) {
    var baseClassName = useBaseClassName();
    var _useCommonClassName = useCommonClassName(),
      statusClassNames = _useCommonClassName.statusClassNames;
    var _useConfig = useConfig("colorPicker"),
      t = _useConfig.t,
      global = _useConfig.global;
    var _toRefs = toRefs(props2),
      inputValue = _toRefs.value,
      recentColors = _toRefs.recentColors;
    var _useVModel = useVModel(inputValue, props2.defaultValue, props2.onChange, "change"),
      _useVModel2 = _slicedToArray(_useVModel, 2),
      innerValue = _useVModel2[0],
      setInnerValue = _useVModel2[1];
    var color = ref(new Color(innerValue.value || DEFAULT_COLOR));
    var updateColor = function updateColor() {
      return color.value.update(innerValue.value || DEFAULT_COLOR);
    };
    var mode = ref(color.value.isGradient ? "linear-gradient" : "monochrome");
    var formatModel = ref(color.value.isGradient ? "CSS" : "RGB");
    var _useDefaultValue = useDefaultValue(recentColors, props2.defaultRecentColors, props2.onRecentColorsChange, "recentColors", "recentColorsChange"),
      _useDefaultValue2 = _slicedToArray(_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 > TD_COLOR_USED_COLORS_MAX_SIZE) colors.length = 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: getColorObject(color.value),
        trigger: trigger || "palette-saturation-brightness"
      });
    };
    watch(function () {
      return [props2.defaultValue, props2.enableAlpha];
    }, updateColor);
    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 : 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: 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 = 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();
    };
    var previewColorStyle = computed(function () {
      return mode.value === "linear-gradient" ? color.value.linearGradient : color.value.rgba;
    });
    return {
      baseClassName: baseClassName,
      statusClassNames: statusClassNames,
      t: t,
      global: global,
      color: color,
      mode: mode,
      formatModel: formatModel,
      recentlyUsedColors: recentlyUsedColors,
      previewColorStyle: previewColorStyle,
      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 h = arguments[0];
    var baseClassName = this.baseClassName,
      statusClassNames = this.statusClassNames,
      t = this.t,
      global = this.global,
      recentColors = this.recentColors,
      swatchColors = this.swatchColors,
      previewColorStyle = this.previewColorStyle;
    var showUsedColors = recentColors !== null && recentColors !== false;
    var systemColors = swatchColors;
    if (systemColors === void 0) systemColors = _toConsumableArray(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 h("div", {
        "class": "".concat(baseClassName, "__swatches-wrap")
      }, [showUsedColors ? h("swatches-panel", {
        "attrs": {
          "color": _this.color,
          "disabled": _this.disabled,
          "title": t(global.recentColorTitle),
          "editable": true,
          "colors": _this.recentlyUsedColors,
          "handleAddColor": _this.addRecentlyUsedColor,
          "handleSetColor": function handleSetColor(color) {
            _this.handleSetColor("used", color);
          },
          "handleChange": _this.handleRecentlyUsedColorsChange
        }
      }) : null, showSystemColors ? h("swatches-panel", {
        "attrs": {
          "color": _this.color,
          "disabled": _this.disabled,
          "title": t(global.swatchColorTitle),
          "colors": systemColors,
          "handleSetColor": function handleSetColor(color) {
            return _this.handleSetColor("system", color);
          }
        }
      }) : null]);
    };
    var isGradient = this.mode === "linear-gradient";
    return h("div", {
      "class": ["".concat(baseClassName, "__panel"), this.disabled ? statusClassNames.disabled : false]
    }, [h("panel-header", {
      "props": _objectSpread({}, _objectSpread({}, this.$props)),
      "attrs": {
        "mode": this.mode,
        "handleModeChange": this.handleModeChange
      }
    }), h("div", {
      "class": ["".concat(baseClassName, "__body")]
    }, [isGradient ? h("linear-gradient", {
      "attrs": {
        "color": this.color,
        "disabled": this.disabled,
        "handleChange": this.handleGradientChange,
        "enableMultipleGradient": this.enableMultipleGradient
      }
    }) : null, h("saturation-panel", {
      "attrs": {
        "color": this.color,
        "disabled": this.disabled,
        "handleChange": this.handleSatAndValueChange
      }
    }), h("div", {
      "class": ["".concat(baseClassName, "__sliders-wrapper")]
    }, [h("div", {
      "class": ["".concat(baseClassName, "__sliders")]
    }, [h("hue-slider", {
      "attrs": {
        "color": this.color,
        "disabled": this.disabled,
        "handleChange": this.handleHueChange
      }
    }), this.enableAlpha ? h("alpha-slider", {
      "attrs": {
        "color": this.color,
        "disabled": this.disabled,
        "handleChange": this.handleAlphaChange
      }
    }) : null]), this.showPrimaryColorPreview ? h("div", {
      "class": ["".concat(baseClassName, "__sliders-preview"), "".concat(baseClassName, "--bg-alpha")]
    }, [h("span", {
      "class": "".concat(baseClassName, "__sliders-preview-inner"),
      "style": {
        background: previewColorStyle
      }
    })]) : null]), h("format-panel", {
      "props": _objectSpread({}, _objectSpread(_objectSpread({}, this.$props), {}, {
        format: this.formatModel
      })),
      "attrs": {
        "color": this.color,
        "handleFormatModeChange": this.handleFormatModeChange,
        "handleFormatInputChange": this.handleInputChange
      }
    }), renderSwatches()])]);
  }
});

export { ColorPanel as default };
//# sourceMappingURL=index.js.map
