/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { defineComponent, toRefs, ref, watch, computed } from '@vue/composition-api';
import '../../../chunks/index.js';
import '../../../hooks/use-config.js';
import useCommonClassName from '../../../hooks/use-common-classname.js';
import props from '../props.js';
import { DEFAULT_COLOR, TD_COLOR_USED_COLORS_MAX_SIZE, DEFAULT_LINEAR_GRADIENT, DEFAULT_SYSTEM_SWATCH_COLORS } from '../const.js';
import { Color, getColorObject } from '../utils/index.js';
import { useBaseClassName } from '../hooks/index.js';
import useVModel from '../../../hooks/use-vmodel.js';
import useDefaultValue from '../../../hooks/use-default-value.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 { l as log } from '../../../chunks/log.js';
import '../../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../config-provider/context.js';
import 'lodash';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../../chunks/defineProperty-838e4192.js';
import 'lodash/kebabCase';
import '../../radio/index.js';
import '../../../utils/map-props.js';
import 'vue';
import '../../../utils/with-install.js';
import '../../../config.js';
import '../../radio/radio.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import '../../../utils/helper.js';
import 'lodash/camelCase';
import '../../../utils/render-node.js';
import '../../../utils/event.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../../utils/mixins.js';
import '../../radio/props.js';
import '../../radio/group.js';
import 'lodash/isString';
import 'lodash/isNumber';
import '../../radio/radio-button.js';
import '../../radio/style';
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/loading.js';
import '../../../utils/dom.js';
import 'raf';
import '../../../utils/easing.js';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/icon/gradient.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '../../loading/style';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../../utils/ripple.js';
import '../../button/props.js';
import '../../button/style';
import '../../button/type.js';
import '../../input/index.js';
import '../../input/input.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style';
import '../../input/type.js';
import '../../../hooks/use-global-icon.js';
import '../../input-number/props.js';
import '../../input-number/hooks/use-input-number.js';
import '../../../chunks/isObjectLike-c22c7e3b.js';
import '../../../chunks/isArray-cd7357b9.js';
import '../../../chunks/isNumber-c4eb560e.js';
import '../../../hooks/use-form-disabled.js';
import '../../input-number/style';
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 'lodash/isArray';
import 'lodash/isObject';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import '../../../hooks/render-lnode.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../popup/style';
import '../../popup/type.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/style';
import '../../tag/type.js';
import '../../select-input/index.js';
import '../../select-input/select-input.js';
import '../../select-input/props.js';
import '../../select-input/hooks/use-single.js';
import 'lodash/pick';
import '../../select-input/hooks/use-multiple.js';
import '../../tag-input/index.js';
import '../../tag-input/tag-input.js';
import '../../tag-input/props.js';
import '../../tag-input/hooks/use-tag-scroll.js';
import '../../tag-input/hooks/use-tag-list.js';
import '../../tag-input/hooks/use-hover.js';
import '../../tag-input/hooks/use-drag-sorter.js';
import '../../tag-input/style';
import '../../tag-input/type.js';
import '../../select-input/hooks/use-overlay-innerstyle.js';
import '../../select-input/style';
import '../../select-input/type.js';
import '../../common/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/style';
import '../../checkbox/type.js';
import '../../../hooks/use-lazy-load.js';
import '../../../chunks/observe.js';
import '../../select/utils.js';
import '../../select/props.js';
import '../../select/select-panel.js';
import '../../../hooks/use-virtual-scroll.js';
import '../../select/hooks/use-select-options.js';
import '../../select/option-group.js';
import '../../select/style';
import '../../select/type.js';
import './slider.js';
import 'lodash/upperCase';
import './format/inputs.js';
import 'lodash/throttle';
import './format/config.js';

var ColorPanel = defineComponent({
  name: "ColorPanel",
  components: {
    PanelHeader,
    LinearGradient,
    SaturationPanel,
    HueSlider,
    AlphaSlider,
    FormatPanel,
    SwatchesPanel
  },
  props: {
    ...props,
    togglePopup: {
      type: Function
    }
  },
  setup(props2) {
    const baseClassName = useBaseClassName();
    const {
      statusClassNames
    } = useCommonClassName();
    const {
      t,
      global
    } = useConfig("colorPicker");
    const {
      value: inputValue,
      recentColors
    } = toRefs(props2);
    const [innerValue, setInnerValue] = useVModel(inputValue, props2.defaultValue, props2.onChange, "change");
    const color = ref(new Color(innerValue.value || DEFAULT_COLOR));
    const updateColor = () => color.value.update(innerValue.value || DEFAULT_COLOR);
    const mode = ref(color.value.isGradient ? "linear-gradient" : "monochrome");
    const formatModel = ref(color.value.isGradient ? "CSS" : "RGB");
    const [recentlyUsedColors, setRecentlyUsedColors] = useDefaultValue(recentColors, props2.defaultRecentColors, props2.onRecentColorsChange, "recentColors", "recentColorsChange");
    if (props2.colorModes.length === 1) {
      const m = props2.colorModes[0];
      mode.value = m;
    }
    const formatValue = () => {
      if (mode.value === "linear-gradient") return color.value.linearGradient;
      return color.value.getFormatsColorMap()[props2.format] || color.value.css;
    };
    const addRecentlyUsedColor = () => {
      if (recentlyUsedColors.value === null || recentlyUsedColors.value === false) return;
      const colors = recentlyUsedColors.value || [];
      const currentColor = color.value.isGradient ? color.value.linearGradient : color.value.rgba;
      const 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);
    };
    const handleRecentlyUsedColorsChange = colors => {
      recentlyUsedColors.value = colors;
      setRecentlyUsedColors(colors);
    };
    const emitColorChange = trigger => {
      setInnerValue(formatValue(), {
        color: getColorObject(color.value),
        trigger: trigger || "palette-saturation-brightness"
      });
    };
    watch(() => [props2.defaultValue, props2.enableAlpha], updateColor);
    watch(() => innerValue.value, newColor => {
      if (newColor !== formatValue()) {
        updateColor();
        mode.value = color.value.isGradient ? "linear-gradient" : "monochrome";
      }
    });
    const handleModeChange = value => {
      mode.value = value;
      if (value === "linear-gradient") {
        color.value.update(color.value.gradientColors.length > 0 ? color.value.linearGradient : DEFAULT_LINEAR_GRADIENT);
        return;
      }
      color.value.update(color.value.rgba);
    };
    const handleFormatModeChange = format => formatModel.value = format;
    const handleSatAndValueChange = _ref => {
      let {
        saturation,
        value
      } = _ref;
      const {
        saturation: sat,
        value: val
      } = color.value;
      let 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);
    };
    const 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)
      });
    };
    const handleAlphaChange = alpha => {
      color.value.alpha = alpha;
      emitColorChange("palette-alpha-bar");
    };
    const handleInputChange = (input, alpha) => {
      color.value.update(input);
      color.value.alpha = alpha;
      emitColorChange("input");
    };
    const handleGradientChange = _ref2 => {
      let {
        key,
        payload
      } = _ref2;
      let 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);
    };
    const handleSetColor = (type, value) => {
      const isGradientValue = Color.isGradientColor(value);
      if (isGradientValue) {
        if (props2.colorModes.includes("linear-gradient")) {
          mode.value = "linear-gradient";
          color.value.update(value);
          color.value.updateCurrentGradientColor();
        } else {
          log.warn("DateRangePickerPanel", "\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();
    };
    const previewColorStyle = computed(() => mode.value === "linear-gradient" ? color.value.linearGradient : color.value.rgba);
    return {
      baseClassName,
      statusClassNames,
      t,
      global,
      color,
      mode,
      formatModel,
      recentlyUsedColors,
      previewColorStyle,
      addRecentlyUsedColor,
      handleModeChange,
      handleSatAndValueChange,
      handleHueChange,
      handleAlphaChange,
      handleGradientChange,
      handleSetColor,
      handleFormatModeChange,
      handleInputChange,
      handleRecentlyUsedColorsChange
    };
  },
  render() {
    var _systemColors;
    const h = arguments[0];
    const {
      baseClassName,
      statusClassNames,
      t,
      global,
      recentColors,
      swatchColors,
      previewColorStyle
    } = this;
    const showUsedColors = recentColors !== null && recentColors !== false;
    let systemColors = swatchColors;
    if (systemColors === void 0) systemColors = [...DEFAULT_SYSTEM_SWATCH_COLORS];
    const showSystemColors = ((_systemColors = systemColors) === null || _systemColors === void 0 ? void 0 : _systemColors.length) > 0;
    const renderSwatches = () => {
      if (!showSystemColors && !showUsedColors) return null;
      return h("div", {
        "class": `${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": 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": color => this.handleSetColor("system", color)
        }
      }) : null]);
    };
    const isGradient = this.mode === "linear-gradient";
    return h("div", {
      "class": [`${baseClassName}__panel`, this.disabled ? statusClassNames.disabled : false]
    }, [h("panel-header", {
      "props": {
        ...{
          ...this.$props
        }
      },
      "attrs": {
        "mode": this.mode,
        "handleModeChange": this.handleModeChange
      }
    }), h("div", {
      "class": [`${baseClassName}__body`]
    }, [isGradient ? h("linear-gradient", {
      "attrs": {
        "color": this.color,
        "disabled": this.disabled,
        "handleChange": this.handleGradientChange
      }
    }) : null, h("saturation-panel", {
      "attrs": {
        "color": this.color,
        "disabled": this.disabled,
        "handleChange": this.handleSatAndValueChange
      }
    }), h("div", {
      "class": [`${baseClassName}__sliders-wrapper`]
    }, [h("div", {
      "class": [`${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]), h("div", {
      "class": [`${baseClassName}__sliders-preview`, `${baseClassName}--bg-alpha`]
    }, [h("span", {
      "class": `${baseClassName}__sliders-preview-inner`,
      "style": {
        background: previewColorStyle
      }
    })])]), h("format-panel", {
      "props": {
        ...{
          ...this.$props,
          format: this.formatModel
        }
      },
      "attrs": {
        "color": this.color,
        "handleFormatModeChange": this.handleFormatModeChange,
        "handleFormatInputChange": this.handleInputChange
      }
    }), renderSwatches()])]);
  }
});

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