"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const aria = require("../../../constants/aria.js");
const event$1 = require("../../../constants/event.js");
const event = require("../../../utils/dom/event.js");
const error = require("../../../utils/error.js");
const index = require("../../../hooks/use-locale/index.js");
const index$1 = require("../../../hooks/use-namespace/index.js");
const index$3 = require("../../../hooks/use-focus-controller/index.js");
const index$2 = require("../../../hooks/use-empty-values/index.js");
const index$6 = require("../../icon/index.js");
const useFormCommonProps = require("../../form/src/hooks/use-form-common-props.js");
const useFormItem = require("../../form/src/hooks/use-form-item.js");
const index$5 = require("../../tooltip/index.js");
const index$9 = require("../../button/index.js");
const index$a = require("../../../directives/click-outside/index.js");
const colorPickerPanel = require("../../color-picker-panel/src/color-picker-panel.js");
const color = require("../../color-picker-panel/src/utils/color.js");
const useCommonColor = require("../../color-picker-panel/src/composables/use-common-color.js");
const index$8 = require("../../color-picker-panel/index.js");
const colorPicker = require("./color-picker.js");
const index$7 = require("../../../../../@element-plus/icons-vue/dist/index.js");
const vue = require("vue");
const index$4 = require("../../../../node_modules/@vueuse/shared/index.js");
const pick = require("../../../../../lodash-es/pick.js");
const debounce = require("../../../../../lodash-es/debounce.js");
const _hoisted_1 = [
  "id",
  "aria-label",
  "aria-labelledby",
  "aria-description",
  "aria-disabled",
  "tabindex"
];
var color_picker_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ vue.defineComponent({
  name: "ElColorPicker",
  __name: "color-picker",
  props: colorPicker.colorPickerProps,
  emits: colorPicker.colorPickerEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const { t } = index.useLocale();
    const ns = index$1.useNamespace("color");
    const { formItem } = useFormItem.useFormItem();
    const colorSize = useFormCommonProps.useFormSize();
    const colorDisabled = useFormCommonProps.useFormDisabled();
    const { valueOnClear, isEmptyValue } = index$2.useEmptyValues(props, null);
    const commonColor = useCommonColor.useCommonColor(props, emit);
    const { inputId: buttonId, isLabeledByFormItem } = useFormItem.useFormItemInputId(props, { formItemContext: formItem });
    const popper = vue.ref();
    const triggerRef = vue.ref();
    const pickerPanelRef = vue.ref();
    const showPicker = vue.ref(false);
    const showPanelColor = vue.ref(false);
    let shouldActiveChange = true;
    const { isFocused, handleFocus, handleBlur } = index$3.useFocusController(triggerRef, {
      disabled: colorDisabled,
      beforeBlur(event2) {
        var _a;
        return (_a = popper.value) == null ? void 0 : _a.isFocusInsideContent(event2);
      },
      afterBlur() {
        var _a;
        setShowPicker(false);
        resetColor();
        if (props.validateEvent) (_a = formItem == null ? void 0 : formItem.validate) == null ? void 0 : _a.call(formItem, "blur").catch((err) => error.debugWarn(err));
      }
    });
    const color$1 = index$4.reactiveComputed(() => {
      var _a;
      return ((_a = pickerPanelRef.value) == null ? void 0 : _a.color) ?? commonColor.color;
    });
    const panelProps = vue.computed(() => pick.default(props, Object.keys(colorPickerPanel.colorPickerPanelProps)));
    const displayedColor = vue.computed(() => {
      if (!props.modelValue && !showPanelColor.value) return "transparent";
      return displayedRgb(color$1, props.showAlpha);
    });
    const currentColor = vue.computed(() => {
      return !props.modelValue && !showPanelColor.value ? "" : color$1.value;
    });
    const buttonAriaLabel = vue.computed(() => {
      return !isLabeledByFormItem.value ? props.ariaLabel || t("el.colorpicker.defaultLabel") : void 0;
    });
    const buttonAriaLabelledby = vue.computed(() => {
      return isLabeledByFormItem.value ? formItem == null ? void 0 : formItem.labelId : void 0;
    });
    const btnKls = vue.computed(() => {
      return [
        ns.b("picker"),
        ns.is("disabled", colorDisabled.value),
        ns.bm("picker", colorSize.value),
        ns.is("focused", isFocused.value)
      ];
    });
    function displayedRgb(color2, showAlpha) {
      const { r, g, b, a } = color2.toRgb();
      return showAlpha ? `rgba(${r}, ${g}, ${b}, ${a})` : `rgb(${r}, ${g}, ${b})`;
    }
    function setShowPicker(value) {
      showPicker.value = value;
    }
    const debounceSetShowPicker = debounce.default(setShowPicker, 100, { leading: true });
    function show() {
      if (colorDisabled.value) return;
      setShowPicker(true);
    }
    function hide() {
      debounceSetShowPicker(false);
      resetColor();
    }
    function resetColor() {
      vue.nextTick(() => {
        if (props.modelValue) color$1.fromString(props.modelValue);
        else {
          color$1.value = "";
          vue.nextTick(() => {
            showPanelColor.value = false;
          });
        }
      });
    }
    function handleTrigger() {
      if (colorDisabled.value) return;
      if (showPicker.value) resetColor();
      debounceSetShowPicker(!showPicker.value);
    }
    function confirmValue() {
      const value = isEmptyValue(color$1.value) ? valueOnClear.value : color$1.value;
      emit(event$1.UPDATE_MODEL_EVENT, value);
      emit(event$1.CHANGE_EVENT, value);
      if (props.validateEvent) formItem == null ? void 0 : formItem.validate("change").catch((err) => error.debugWarn(err));
      debounceSetShowPicker(false);
      vue.nextTick(() => {
        const newColor = new color.default({
          enableAlpha: props.showAlpha,
          format: props.colorFormat || "",
          value: props.modelValue
        });
        if (!color$1.compare(newColor)) resetColor();
      });
    }
    function clear() {
      debounceSetShowPicker(false);
      emit(event$1.UPDATE_MODEL_EVENT, valueOnClear.value);
      emit(event$1.CHANGE_EVENT, valueOnClear.value);
      if (props.modelValue !== valueOnClear.value && props.validateEvent) formItem == null ? void 0 : formItem.validate("change").catch((err) => error.debugWarn(err));
      resetColor();
      emit("clear");
    }
    function handleShowTooltip() {
      var _a, _b;
      (_b = (_a = pickerPanelRef == null ? void 0 : pickerPanelRef.value) == null ? void 0 : _a.inputRef) == null ? void 0 : _b.focus();
    }
    function handleClickOutside() {
      if (!showPicker.value) return;
      hide();
      isFocused.value && focus();
    }
    function handleEsc(event2) {
      event2.preventDefault();
      event2.stopPropagation();
      setShowPicker(false);
      resetColor();
    }
    function handleKeyDown(event$12) {
      switch (event.getEventCode(event$12)) {
        case aria.EVENT_CODE.enter:
        case aria.EVENT_CODE.numpadEnter:
        case aria.EVENT_CODE.space:
          event$12.preventDefault();
          event$12.stopPropagation();
          show();
          break;
        case aria.EVENT_CODE.esc:
          handleEsc(event$12);
          break;
      }
    }
    function focus() {
      triggerRef.value.focus();
    }
    function blur() {
      triggerRef.value.blur();
    }
    vue.watch(() => currentColor.value, (val) => {
      shouldActiveChange && emit("activeChange", val);
      shouldActiveChange = true;
    });
    vue.watch(() => color$1.value, () => {
      if (!props.modelValue && !showPanelColor.value) showPanelColor.value = true;
    });
    vue.watch(() => props.modelValue, (newVal) => {
      if (!newVal) showPanelColor.value = false;
      else if (newVal && newVal !== color$1.value) {
        shouldActiveChange = false;
        color$1.fromString(newVal);
      }
    });
    vue.watch(() => showPicker.value, () => {
      pickerPanelRef.value && vue.nextTick(pickerPanelRef.value.update);
    });
    vue.provide(colorPickerPanel.ROOT_COMMON_COLOR_INJECTION_KEY, commonColor);
    __expose({
      color: color$1,
      show,
      hide,
      focus,
      blur
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createBlock(vue.unref(index$5.ElTooltip), {
        ref_key: "popper",
        ref: popper,
        visible: showPicker.value,
        "show-arrow": false,
        "fallback-placements": [
          "bottom",
          "top",
          "right",
          "left"
        ],
        offset: 0,
        "gpu-acceleration": false,
        "popper-class": [vue.unref(ns).be("picker", "panel"), __props.popperClass],
        "popper-style": __props.popperStyle,
        "stop-popper-mouse-event": false,
        pure: "",
        loop: "",
        role: "dialog",
        effect: "light",
        trigger: "click",
        teleported: __props.teleported,
        transition: `${vue.unref(ns).namespace.value}-zoom-in-top`,
        persistent: __props.persistent,
        "append-to": __props.appendTo,
        onShow: handleShowTooltip,
        onHide: _cache[2] || (_cache[2] = ($event) => setShowPicker(false))
      }, {
        content: vue.withCtx(() => [vue.withDirectives((vue.openBlock(), vue.createBlock(vue.unref(index$8.ElColorPickerPanel), vue.mergeProps({
          ref_key: "pickerPanelRef",
          ref: pickerPanelRef
        }, panelProps.value, {
          border: false,
          "validate-event": false,
          onKeydown: vue.withKeys(handleEsc, ["esc"])
        }), {
          footer: vue.withCtx(() => [vue.createElementVNode("div", null, [__props.clearable ? (vue.openBlock(), vue.createBlock(vue.unref(index$9.ElButton), {
            key: 0,
            class: vue.normalizeClass(vue.unref(ns).be("footer", "link-btn")),
            text: "",
            size: "small",
            onClick: clear
          }, {
            default: vue.withCtx(() => [vue.createTextVNode(vue.toDisplayString(vue.unref(t)("el.colorpicker.clear")), 1)]),
            _: 1
          }, 8, ["class"])) : vue.createCommentVNode("v-if", true), vue.createVNode(vue.unref(index$9.ElButton), {
            plain: "",
            size: "small",
            class: vue.normalizeClass(vue.unref(ns).be("footer", "btn")),
            onClick: confirmValue
          }, {
            default: vue.withCtx(() => [vue.createTextVNode(vue.toDisplayString(vue.unref(t)("el.colorpicker.confirm")), 1)]),
            _: 1
          }, 8, ["class"])])]),
          _: 1
        }, 16)), [[
          vue.unref(index$a.default),
          handleClickOutside,
          triggerRef.value
        ]])]),
        default: vue.withCtx(() => [vue.createElementVNode("div", vue.mergeProps({
          id: vue.unref(buttonId),
          ref_key: "triggerRef",
          ref: triggerRef
        }, _ctx.$attrs, {
          class: btnKls.value,
          role: "button",
          "aria-label": buttonAriaLabel.value,
          "aria-labelledby": buttonAriaLabelledby.value,
          "aria-description": vue.unref(t)("el.colorpicker.description", { color: __props.modelValue || "" }),
          "aria-disabled": vue.unref(colorDisabled),
          tabindex: vue.unref(colorDisabled) ? void 0 : __props.tabindex,
          onKeydown: handleKeyDown,
          onFocus: _cache[0] || (_cache[0] = (...args) => vue.unref(handleFocus) && vue.unref(handleFocus)(...args)),
          onBlur: _cache[1] || (_cache[1] = (...args) => vue.unref(handleBlur) && vue.unref(handleBlur)(...args))
        }), [vue.createElementVNode("div", {
          class: vue.normalizeClass(vue.unref(ns).be("picker", "trigger")),
          onClick: handleTrigger
        }, [vue.createElementVNode("span", { class: vue.normalizeClass([vue.unref(ns).be("picker", "color"), vue.unref(ns).is("alpha", __props.showAlpha)]) }, [vue.createElementVNode("span", {
          class: vue.normalizeClass(vue.unref(ns).be("picker", "color-inner")),
          style: vue.normalizeStyle({ backgroundColor: displayedColor.value })
        }, [vue.withDirectives(vue.createVNode(vue.unref(index$6.ElIcon), { class: vue.normalizeClass([vue.unref(ns).be("picker", "icon"), vue.unref(ns).is("icon-arrow-down")]) }, {
          default: vue.withCtx(() => [vue.createVNode(vue.unref(index$7.ArrowDown))]),
          _: 1
        }, 8, ["class"]), [[vue.vShow, __props.modelValue || showPanelColor.value]]), vue.withDirectives(vue.createVNode(vue.unref(index$6.ElIcon), { class: vue.normalizeClass([vue.unref(ns).be("picker", "empty"), vue.unref(ns).is("icon-close")]) }, {
          default: vue.withCtx(() => [vue.createVNode(vue.unref(index$7.Close))]),
          _: 1
        }, 8, ["class"]), [[vue.vShow, !__props.modelValue && !showPanelColor.value]])], 6)], 2)], 2)], 16, _hoisted_1)]),
        _: 1
      }, 8, [
        "visible",
        "popper-class",
        "popper-style",
        "teleported",
        "transition",
        "persistent",
        "append-to"
      ]);
    };
  }
});
exports.default = color_picker_vue_vue_type_script_setup_true_lang_default;
