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

'use strict';

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

var VueCompositionAPI = require('@vue/composition-api');
var log = require('../../../chunks/log.js');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
var hooks_useCommonClassname = require('../../../hooks/use-common-classname.js');
var components_colorPicker_props = require('../props.js');
var constants = require('../../../chunks/constants.js');
var color = require('../../../chunks/color.js');
var components_colorPicker_hooks_index = require('../hooks/index.js');
var hooks_useVmodel = require('../../../hooks/use-vmodel.js');
var hooks_useDefaultValue = require('../../../hooks/use-default-value.js');
var components_colorPicker_panel_header = require('./header.js');
var components_colorPicker_panel_linearGradient = require('./linear-gradient.js');
var components_colorPicker_panel_saturation = require('./saturation.js');
var components_colorPicker_panel_hue = require('./hue.js');
var components_colorPicker_panel_alpha = require('./alpha.js');
var components_colorPicker_panel_format_index = require('./format/index.js');
var components_colorPicker_panel_swatches = require('./swatches.js');
require('../../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../config-provider/context.js');
require('lodash');
require('../../../chunks/zh_CN.js');
require('../../../chunks/defineProperty-838e4192.js');
require('../../../hooks/use-config.js');
require('lodash/kebabCase');
require('../../radio/index.js');
require('../../../utils/map-props.js');
require('vue');
require('../../../utils/with-install.js');
require('../../../config.js');
require('../../radio/radio.js');
require('../../../chunks/helper.js');
require('../../../utils/helper.js');
require('lodash/camelCase');
require('../../../utils/render-node.js');
require('../../../utils/event.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../../utils/mixins.js');
require('../../radio/props.js');
require('../../radio/group.js');
require('lodash/isString');
require('lodash/isNumber');
require('../../radio/radio-button.js');
require('lodash/cloneDeep');
require('../../input-number/index.js');
require('../../input-number/input-number.js');
require('@ldesign/icons-vue');
require('../../button/index.js');
require('../../button/button.js');
require('../../loading/index.js');
require('../../loading/loading.js');
require('../../../utils/dom.js');
require('raf');
require('../../../utils/easing.js');
require('../../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../../chunks/set-style.js');
require('../../../chunks/helper2.js');
require('../../loading/plugin.js');
require('../../../utils/ripple.js');
require('../../button/props.js');
require('../../input/index.js');
require('../../input/input.js');
require('../../input/props.js');
require('../../input/group.js');
require('../../../hooks/use-global-icon.js');
require('../../input-number/props.js');
require('../../input-number/hooks/use-input-number.js');
require('../../../chunks/isObjectLike-c22c7e3b.js');
require('../../../chunks/isArray-cd7357b9.js');
require('../../../chunks/isNumber-c4eb560e.js');
require('../../../hooks/use-form-disabled.js');
require('./base-props.js');
require('../../select/index.js');
require('../../select/select.js');
require('lodash/isFunction');
require('lodash/debounce');
require('lodash/get');
require('lodash/isArray');
require('lodash/isObject');
require('../../../hooks/lnode.js');
require('lodash/isEmpty');
require('../../../hooks/render-lnode.js');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../popup/props.js');
require('../../popup/container.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../select-input/index.js');
require('../../select-input/select-input.js');
require('../../select-input/props.js');
require('../../select-input/hooks/use-single.js');
require('lodash/pick');
require('../../select-input/hooks/use-multiple.js');
require('../../tag-input/index.js');
require('../../tag-input/tag-input.js');
require('../../tag-input/props.js');
require('../../tag-input/hooks/use-tag-scroll.js');
require('../../tag-input/hooks/use-tag-list.js');
require('../../tag-input/hooks/use-hover.js');
require('../../tag-input/hooks/use-drag-sorter.js');
require('../../select-input/hooks/use-overlay-innerstyle.js');
require('../../common/fake-arrow.js');
require('../../select/option.js');
require('../../checkbox/index.js');
require('../../checkbox/group.js');
require('lodash/intersection');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../../hooks/use-lazy-load.js');
require('../../../chunks/observe.js');
require('../../select/utils.js');
require('../../select/props.js');
require('../../select/select-panel.js');
require('../../../hooks/use-virtual-scroll.js');
require('../../select/hooks/use-select-options.js');
require('../../select/option-group.js');
require('../../../chunks/draggable.js');
require('./slider.js');
require('lodash/upperCase');
require('../const.js');
require('./format/inputs.js');
require('lodash/throttle');
require('./format/config.js');

var ColorPanel = VueCompositionAPI.defineComponent({
  name: "ColorPanel",
  components: {
    PanelHeader: components_colorPicker_panel_header["default"],
    LinearGradient: components_colorPicker_panel_linearGradient["default"],
    SaturationPanel: components_colorPicker_panel_saturation["default"],
    HueSlider: components_colorPicker_panel_hue["default"],
    AlphaSlider: components_colorPicker_panel_alpha["default"],
    FormatPanel: components_colorPicker_panel_format_index["default"],
    SwatchesPanel: components_colorPicker_panel_swatches["default"]
  },
  props: {
    ...components_colorPicker_props["default"],
    togglePopup: {
      type: Function
    }
  },
  setup(props2) {
    const baseClassName = components_colorPicker_hooks_index.useBaseClassName();
    const {
      statusClassNames
    } = hooks_useCommonClassname["default"]();
    const {
      t,
      global
    } = components_configProvider_useConfig.useConfig("colorPicker");
    const {
      value: inputValue,
      recentColors
    } = VueCompositionAPI.toRefs(props2);
    const [innerValue, setInnerValue] = hooks_useVmodel["default"](inputValue, props2.defaultValue, props2.onChange, "change");
    const color$1 = VueCompositionAPI.ref(new color.Color(innerValue.value || constants.DEFAULT_COLOR));
    const updateColor = () => color$1.value.update(innerValue.value || constants.DEFAULT_COLOR);
    const mode = VueCompositionAPI.ref(color$1.value.isGradient ? "linear-gradient" : "monochrome");
    const formatModel = VueCompositionAPI.ref(color$1.value.isGradient ? "CSS" : "RGB");
    const [recentlyUsedColors, setRecentlyUsedColors] = hooks_useDefaultValue["default"](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$1.value.linearGradient;
      return color$1.value.getFormatsColorMap()[props2.format] || color$1.value.css;
    };
    const addRecentlyUsedColor = () => {
      if (recentlyUsedColors.value === null || recentlyUsedColors.value === false) return;
      const colors = recentlyUsedColors.value || [];
      const currentColor = color$1.value.isGradient ? color$1.value.linearGradient : color$1.value.rgba;
      const index = colors.indexOf(currentColor);
      if (index > -1) colors.splice(index, 1);
      colors.unshift(currentColor);
      if (colors.length > constants.TD_COLOR_USED_COLORS_MAX_SIZE) colors.length = constants.TD_COLOR_USED_COLORS_MAX_SIZE;
      handleRecentlyUsedColorsChange(colors);
    };
    const handleRecentlyUsedColorsChange = colors => {
      recentlyUsedColors.value = colors;
      setRecentlyUsedColors(colors);
    };
    const emitColorChange = trigger => {
      setInnerValue(formatValue(), {
        color: color.getColorObject(color$1.value),
        trigger: trigger || "palette-saturation-brightness"
      });
    };
    VueCompositionAPI.watch(() => [props2.defaultValue, props2.enableAlpha], updateColor);
    VueCompositionAPI.watch(() => innerValue.value, newColor => {
      if (newColor !== formatValue()) {
        updateColor();
        mode.value = color$1.value.isGradient ? "linear-gradient" : "monochrome";
      }
    });
    const handleModeChange = value => {
      mode.value = value;
      if (value === "linear-gradient") {
        color$1.value.update(color$1.value.gradientColors.length > 0 ? color$1.value.linearGradient : constants.DEFAULT_LINEAR_GRADIENT);
        return;
      }
      color$1.value.update(color$1.value.rgba);
    };
    const handleFormatModeChange = format => formatModel.value = format;
    const handleSatAndValueChange = _ref => {
      let {
        saturation,
        value
      } = _ref;
      const {
        saturation: sat,
        value: val
      } = color$1.value;
      let changeTrigger = "palette-saturation-brightness";
      if (value !== val && saturation !== sat) {
        color$1.value.saturation = saturation;
        color$1.value.value = value;
        changeTrigger = "palette-saturation-brightness";
      } else if (saturation !== sat) {
        color$1.value.saturation = saturation;
        changeTrigger = "palette-saturation";
      } else if (value !== val) {
        color$1.value.value = value;
        changeTrigger = "palette-brightness";
      } else {
        return;
      }
      emitColorChange(changeTrigger);
    };
    const handleHueChange = hue => {
      var _props2$onPaletteBarC;
      color$1.value.hue = hue;
      emitColorChange("palette-hue-bar");
      (_props2$onPaletteBarC = props2.onPaletteBarChange) === null || _props2$onPaletteBarC === void 0 ? void 0 : _props2$onPaletteBarC.call(props2, {
        color: color.getColorObject(color$1.value)
      });
    };
    const handleAlphaChange = alpha => {
      color$1.value.alpha = alpha;
      emitColorChange("palette-alpha-bar");
    };
    const handleInputChange = (input, alpha) => {
      color$1.value.update(input);
      color$1.value.alpha = alpha;
      emitColorChange("input");
    };
    const handleGradientChange = _ref2 => {
      let {
        key,
        payload
      } = _ref2;
      let trigger = "palette-saturation-brightness";
      switch (key) {
        case "degree":
          color$1.value.gradientDegree = payload;
          trigger = "input";
          break;
        case "selectedId":
          color$1.value.gradientSelectedId = payload;
          break;
        case "colors":
          color$1.value.gradientColors = payload;
          break;
      }
      emitColorChange(trigger);
    };
    const handleSetColor = (type, value) => {
      const isGradientValue = color.Color.isGradientColor(value);
      if (isGradientValue) {
        if (props2.colorModes.includes("linear-gradient")) {
          mode.value = "linear-gradient";
          color$1.value.update(value);
          color$1.value.updateCurrentGradientColor();
        } else {
          log.log.warn("DateRangePickerPanel", "\u8BE5\u6A21\u5F0F\u4E0D\u652F\u6301\u6E10\u53D8\u8272");
        }
      } else if (mode.value === "linear-gradient") {
        color$1.value.updateStates(value);
        color$1.value.updateCurrentGradientColor();
      } else {
        color$1.value.update(value);
      }
      emitColorChange();
    };
    const previewColorStyle = VueCompositionAPI.computed(() => mode.value === "linear-gradient" ? color$1.value.linearGradient : color$1.value.rgba);
    return {
      baseClassName,
      statusClassNames,
      t,
      global,
      color: color$1,
      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 = [...constants.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()])]);
  }
});

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