/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @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/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 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 { 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 { useConfig } from '../../config-provider/useConfig.js';
import '../../config-provider/context.js';
import 'lodash';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../radio/index.js';
import '../../radio/radio.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import 'vue';
import '../../../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/camelCase';
import '../../../utils/render-node.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/kebabCase';
import '../../radio/props.js';
import '../../../utils/event.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../../utils/mixins.js';
import '../../radio/group.js';
import 'lodash/isString';
import 'lodash/isNumber';
import '../../radio/radio-button.js';
import '../../../utils/map-props.js';
import '../../../utils/with-install.js';
import '../../../config.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 '../../loading/icon/gradient.js';
import '../../../chunks/defineProperty-28395935.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '../../../utils/dom.js';
import 'raf';
import '../../../utils/easing.js';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../loading/style';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../button/props.js';
import '../../../utils/ripple.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 '../../input-number/props.js';
import '../../input-number/hooks/use-input-number.js';
import '../../../chunks/isObjectLike-2443c6ff.js';
import '../../../chunks/_commonjsHelpers-ea2bee21.js';
import '../../../chunks/isArray-b0f0e351.js';
import '../../../chunks/log.js';
import '../../../hooks/use-form-disabled.js';
import '../../../hooks/use-global-icon.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 '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import '../../../hooks/render-lnode.js';
import 'lodash/isObject';
import '../../select/props.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 '../../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/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';
import '@babel/runtime/helpers/classCallCheck';
import '@babel/runtime/helpers/createClass';

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);
        return;
      }

      color.value.update(color.value.rgba);
    };

    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
      }
    }) : 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]), h("div", {
      "class": ["".concat(baseClassName, "__sliders-preview"), "".concat(baseClassName, "--bg-alpha")]
    }, [h("span", {
      "class": "".concat(baseClassName, "__sliders-preview-inner"),
      "style": {
        background: previewColorStyle
      }
    })])]), 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
