"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const aria = require("../../../constants/aria.js");
const event = require("../../../constants/event.js");
const event$1 = require("../../../utils/dom/event.js");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const index = require("../../../hooks/use-namespace/index.js");
const index$1 = require("../../icon/index.js");
const constants = require("../../form/src/constants.js");
const useFormCommonProps = require("../../form/src/hooks/use-form-common-props.js");
const useFormItem = require("../../form/src/hooks/use-form-item.js");
const rate = require("./rate.js");
const Vue = require("vue");
const clamp = require("../../../../../lodash-es/clamp.js");
const _hoisted_1 = [
  "id",
  "aria-label",
  "aria-labelledby",
  "aria-valuenow",
  "aria-valuetext",
  "aria-valuemax",
  "tabindex",
  "aria-disabled"
];
const _hoisted_2 = ["onMousemove", "onClick"];
var rate_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ Vue.defineComponent({
  name: "ElRate",
  __name: "rate",
  props: rate.rateProps,
  emits: rate.rateEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    function getValueFromMap(value, map) {
      const isExcludedObject = (val) => shared_esmBundler.isObject(val);
      const matchedValue = map[Object.keys(map).map((key) => +key).filter((key) => {
        const val = map[key];
        return (isExcludedObject(val) ? val.excluded : false) ? value < key : value <= key;
      }).sort((a, b) => a - b)[0]];
      return isExcludedObject(matchedValue) && matchedValue.value || matchedValue;
    }
    const props = __props;
    const emit = __emit;
    const formItemContext = Vue.inject(constants.formItemContextKey, void 0);
    const rateSize = useFormCommonProps.useFormSize();
    const ns = index.useNamespace("rate");
    const { inputId, isLabeledByFormItem } = useFormItem.useFormItemInputId(props, { formItemContext });
    const currentValue = Vue.ref(clamp.default(props.modelValue, 0, props.max));
    const hoverIndex = Vue.ref(-1);
    const pointerAtLeftHalf = Vue.ref(true);
    const iconRefs = Vue.ref([]);
    const iconClientWidths = Vue.computed(() => iconRefs.value.map((icon) => icon.$el.clientWidth));
    const rateClasses = Vue.computed(() => [ns.b(), ns.m(rateSize.value)]);
    const rateDisabled = useFormCommonProps.useFormDisabled();
    const rateStyles = Vue.computed(() => {
      return ns.cssVarBlock({
        "void-color": props.voidColor,
        "disabled-void-color": props.disabledVoidColor,
        "fill-color": activeColor.value
      });
    });
    const text = Vue.computed(() => {
      let result = "";
      if (props.showScore) result = props.scoreTemplate.replace(/\{\s*value\s*\}/, rateDisabled.value ? `${props.modelValue}` : `${currentValue.value}`);
      else if (props.showText) result = props.texts[Math.ceil(currentValue.value) - 1];
      return result;
    });
    const valueDecimal = Vue.computed(() => props.modelValue * 100 - Math.floor(props.modelValue) * 100);
    const colorMap = Vue.computed(() => shared_esmBundler.isArray(props.colors) ? {
      [props.lowThreshold]: props.colors[0],
      [props.highThreshold]: {
        value: props.colors[1],
        excluded: true
      },
      [props.max]: props.colors[2]
    } : props.colors);
    const activeColor = Vue.computed(() => {
      const color = getValueFromMap(currentValue.value, colorMap.value);
      return shared_esmBundler.isObject(color) ? "" : color;
    });
    const decimalStyle = Vue.computed(() => {
      let width = "";
      if (rateDisabled.value) width = `${valueDecimal.value}%`;
      else if (props.allowHalf) width = "50%";
      return {
        color: activeColor.value,
        width
      };
    });
    const componentMap = Vue.computed(() => {
      let icons = shared_esmBundler.isArray(props.icons) ? [...props.icons] : { ...props.icons };
      icons = Vue.markRaw(icons);
      return shared_esmBundler.isArray(icons) ? {
        [props.lowThreshold]: icons[0],
        [props.highThreshold]: {
          value: icons[1],
          excluded: true
        },
        [props.max]: icons[2]
      } : icons;
    });
    const decimalIconComponent = Vue.computed(() => getValueFromMap(props.modelValue, componentMap.value));
    const voidComponent = Vue.computed(() => rateDisabled.value ? shared_esmBundler.isString(props.disabledVoidIcon) ? props.disabledVoidIcon : Vue.markRaw(props.disabledVoidIcon) : shared_esmBundler.isString(props.voidIcon) ? props.voidIcon : Vue.markRaw(props.voidIcon));
    const activeComponent = Vue.computed(() => getValueFromMap(currentValue.value, componentMap.value));
    function showDecimalIcon(item) {
      const showWhenDisabled = rateDisabled.value && valueDecimal.value > 0 && item - 1 < props.modelValue && item > props.modelValue;
      const showWhenAllowHalf = props.allowHalf && pointerAtLeftHalf.value && item - 0.5 <= currentValue.value && item > currentValue.value;
      return showWhenDisabled || showWhenAllowHalf;
    }
    function emitValue(value) {
      if (props.clearable && value === props.modelValue) value = 0;
      emit(event.UPDATE_MODEL_EVENT, value);
      if (props.modelValue !== value) emit(event.CHANGE_EVENT, value);
    }
    function selectValue(value) {
      if (rateDisabled.value) return;
      if (props.allowHalf && pointerAtLeftHalf.value) emitValue(currentValue.value);
      else emitValue(value);
    }
    function handleKey(e) {
      if (rateDisabled.value) return;
      const code = event$1.getEventCode(e);
      const step = props.allowHalf ? 0.5 : 1;
      let _currentValue = currentValue.value;
      switch (code) {
        case aria.EVENT_CODE.up:
        case aria.EVENT_CODE.right:
          _currentValue += step;
          break;
        case aria.EVENT_CODE.left:
        case aria.EVENT_CODE.down:
          _currentValue -= step;
          break;
      }
      _currentValue = clamp.default(_currentValue, 0, props.max);
      if (_currentValue === currentValue.value) return;
      e.stopPropagation();
      e.preventDefault();
      emit(event.UPDATE_MODEL_EVENT, _currentValue);
      emit(event.CHANGE_EVENT, _currentValue);
      return _currentValue;
    }
    function setCurrentValue(value, event2) {
      if (rateDisabled.value) return;
      if (props.allowHalf && event2) {
        pointerAtLeftHalf.value = event2.offsetX * 2 <= iconClientWidths.value[value - 1];
        currentValue.value = pointerAtLeftHalf.value ? value - 0.5 : value;
      } else currentValue.value = value;
      hoverIndex.value = value;
    }
    function resetCurrentValue() {
      if (rateDisabled.value) return;
      if (props.allowHalf) pointerAtLeftHalf.value = props.modelValue !== Math.floor(props.modelValue);
      currentValue.value = clamp.default(props.modelValue, 0, props.max);
      hoverIndex.value = -1;
    }
    Vue.watch(() => props.modelValue, (val) => {
      currentValue.value = clamp.default(val, 0, props.max);
      pointerAtLeftHalf.value = props.modelValue !== Math.floor(props.modelValue);
    });
    if (!props.modelValue) emit(event.UPDATE_MODEL_EVENT, 0);
    __expose({
      setCurrentValue,
      resetCurrentValue
    });
    return (_ctx, _cache) => {
      var _a;
      return Vue.openBlock(), Vue.createElementBlock("div", {
        id: Vue.unref(inputId),
        class: Vue.normalizeClass([rateClasses.value, Vue.unref(ns).is("disabled", Vue.unref(rateDisabled))]),
        role: "slider",
        "aria-label": !Vue.unref(isLabeledByFormItem) ? __props.ariaLabel || "rating" : void 0,
        "aria-labelledby": Vue.unref(isLabeledByFormItem) ? (_a = Vue.unref(formItemContext)) == null ? void 0 : _a.labelId : void 0,
        "aria-valuenow": currentValue.value,
        "aria-valuetext": text.value || void 0,
        "aria-valuemin": "0",
        "aria-valuemax": __props.max,
        style: Vue.normalizeStyle(rateStyles.value),
        tabindex: Vue.unref(rateDisabled) ? void 0 : 0,
        "aria-disabled": Vue.unref(rateDisabled),
        onKeydown: handleKey
      }, [(Vue.openBlock(true), Vue.createElementBlock(Vue.Fragment, null, Vue.renderList(__props.max, (item, key) => {
        return Vue.openBlock(), Vue.createElementBlock("span", {
          key,
          class: Vue.normalizeClass(Vue.unref(ns).e("item")),
          onMousemove: ($event) => setCurrentValue(item, $event),
          onMouseleave: resetCurrentValue,
          onClick: ($event) => selectValue(item)
        }, [Vue.createVNode(Vue.unref(index$1.ElIcon), {
          ref_for: true,
          ref_key: "iconRefs",
          ref: iconRefs,
          class: Vue.normalizeClass([
            Vue.unref(ns).e("icon"),
            { hover: hoverIndex.value === item },
            Vue.unref(ns).is("active", item <= currentValue.value),
            Vue.unref(ns).is("focus-visible", item === Math.ceil(currentValue.value || 1))
          ])
        }, {
          default: Vue.withCtx(() => [
            Vue.withDirectives((Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(activeComponent.value), null, null, 512)), [[Vue.vShow, !showDecimalIcon(item) && item <= currentValue.value]]),
            Vue.withDirectives((Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(voidComponent.value), null, null, 512)), [[Vue.vShow, !showDecimalIcon(item) && item > currentValue.value]]),
            Vue.withDirectives((Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(voidComponent.value), { class: Vue.normalizeClass([Vue.unref(ns).em("decimal", "box")]) }, null, 8, ["class"])), [[Vue.vShow, showDecimalIcon(item)]]),
            Vue.withDirectives(Vue.createVNode(Vue.unref(index$1.ElIcon), {
              style: Vue.normalizeStyle(decimalStyle.value),
              class: Vue.normalizeClass([Vue.unref(ns).e("icon"), Vue.unref(ns).e("decimal")])
            }, {
              default: Vue.withCtx(() => [(Vue.openBlock(), Vue.createBlock(Vue.resolveDynamicComponent(decimalIconComponent.value)))]),
              _: 1
            }, 8, ["style", "class"]), [[Vue.vShow, showDecimalIcon(item)]])
          ]),
          _: 2
        }, 1032, ["class"])], 42, _hoisted_2);
      }), 128)), __props.showText || __props.showScore ? (Vue.openBlock(), Vue.createElementBlock("span", {
        key: 0,
        class: Vue.normalizeClass(Vue.unref(ns).e("text")),
        style: Vue.normalizeStyle({ color: __props.textColor })
      }, Vue.toDisplayString(text.value), 7)) : Vue.createCommentVNode("v-if", true)], 46, _hoisted_1);
    };
  }
});
exports.default = rate_vue_vue_type_script_setup_true_lang_default;
