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

import { defineComponent, toRefs, ref, computed } from '@vue/composition-api';
import { StarFilledIcon } from '@ldesign/icons-vue';
import useVModel from '../../hooks/use-vmodel.js';
import { useConfig } from '../config-provider/useConfig.js';
import { Tooltip } from '../tooltip/index.js';
import { renderLNodeJSXDefault } from '../../utils/render-node.js';
import props from './props.js';
import 'lodash/kebabCase';
import '../config-provider/context.js';
import 'lodash';
import '../../_chunks/dep-def4c2ce.js';
import '../../utils/map-props.js';
import 'vue';
import '../../utils/with-install.js';
import '../../config.js';
import '../tooltip/tooltip.js';
import '../../_chunks/dep-29915b8e.js';
import 'lodash/isFunction';
import '../popup/props.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../../_chunks/dep-cf772670.js';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../_chunks/dep-70c789e5.js';
import '../../_chunks/dep-5210251b.js';
import '../../utils/easing.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../utils/mixins.js';
import '../popup/container.js';
import '../tooltip/props.js';

var _Rate = defineComponent({
  name: "LRate",
  props: {
    ...props
  },
  setup(props2) {
    const activeColor = Array.isArray(props2.color) ? props2.color[0] : props2.color;
    const defaultColor = Array.isArray(props2.color) ? props2.color[1] : "var(--td-bg-color-component)";
    const {
      value: inputValue
    } = toRefs(props2);
    const [starValue, setStarValue] = useVModel(inputValue, props2.defaultValue, props2.onChange, "change");
    const hoverValue = ref(void 0);
    const rootRef = ref();
    const {
      classPrefix
    } = useConfig("classPrefix");
    const displayValue = computed(() => Number(hoverValue.value || starValue.value));
    const displayText = computed(() => props2.texts.length === 0 ? ["\u6781\u5DEE", "\u5931\u671B", "\u4E00\u822C", "\u6EE1\u610F", "\u60CA\u559C"] : props2.texts);
    const getStarValue = (event, index) => {
      if (props2.allowHalf) {
        var _rootRef$value, _rootRef$value$getBou, _firstStar$getBoundin;
        const {
          left
        } = (_rootRef$value = rootRef.value) === null || _rootRef$value === void 0 ? void 0 : (_rootRef$value$getBou = _rootRef$value.getBoundingClientRect) === null || _rootRef$value$getBou === void 0 ? void 0 : _rootRef$value$getBou.call(_rootRef$value);
        const firstStar = rootRef.value.firstChild.nextSibling;
        const {
          width
        } = firstStar === null || firstStar === void 0 ? void 0 : (_firstStar$getBoundin = firstStar.getBoundingClientRect) === null || _firstStar$getBoundin === void 0 ? void 0 : _firstStar$getBoundin.call(firstStar);
        const {
          clientX
        } = event;
        const starMiddle = width * (index - 0.5) + props2.gap * (index - 1);
        if (clientX - left >= starMiddle) return index;
        if (clientX - left < starMiddle) return index - 0.5;
      }
      return index;
    };
    const mouseEnterHandler = (event, index) => {
      if (props2.disabled) return;
      hoverValue.value = getStarValue(event, index);
    };
    const mouseLeaveHandler = () => {
      if (props2.disabled) return;
      hoverValue.value = void 0;
    };
    const clickHandler = (event, index) => {
      if (props2.disabled) return;
      setStarValue(getStarValue(event, index));
    };
    const getStarCls = index => {
      if (props2.allowHalf && index + 0.5 === displayValue.value) return `${classPrefix.value}-rate__item--half`;
      if (index >= displayValue.value) return "";
      if (index < displayValue.value) return `${classPrefix.value}-rate__item--full`;
    };
    const activeIconStyle = computed(() => ({
      fontSize: props2.size,
      color: activeColor
    }));
    const inactiveIconStyle = computed(() => ({
      fontSize: props2.size,
      color: defaultColor
    }));
    return {
      classPrefix,
      mouseLeaveHandler,
      getStarCls,
      clickHandler,
      mouseEnterHandler,
      activeColor,
      defaultColor,
      displayText,
      displayValue,
      rootRef,
      activeIconStyle,
      inactiveIconStyle
    };
  },
  methods: {
    renderRateIcon() {
      const h = this.$createElement;
      return renderLNodeJSXDefault(this, "icon", h(StarFilledIcon));
    }
  },
  render() {
    const h = arguments[0];
    const {
      classPrefix,
      mouseLeaveHandler,
      getStarCls,
      clickHandler,
      mouseEnterHandler,
      displayText,
      displayValue
    } = this;
    return h("div", {
      "class": `${classPrefix}-rate`,
      "on": {
        "mouseleave": mouseLeaveHandler
      }
    }, [h("ul", {
      "class": `${classPrefix}-rate__list`,
      "style": {
        gap: `${this.gap}px`
      },
      "ref": "rootRef"
    }, [[...Array(Number(this.count))].map((_, index) => h("li", {
      "key": index,
      "class": [`${classPrefix}-rate__item`, getStarCls(index)],
      "on": {
        "click": event => clickHandler(event, index + 1),
        "mousemove": event => mouseEnterHandler(event, index + 1)
      }
    }, [this.showText ? h(Tooltip, {
      "key": index,
      "attrs": {
        "content": displayText[displayValue - 1]
      }
    }, [h("div", {
      "class": `${classPrefix}-rate__star-top`,
      "style": {
        ...this.activeIconStyle
      }
    }, [this.renderRateIcon()]), h("div", {
      "class": `${classPrefix}-rate__star-bottom`,
      "style": {
        ...this.inactiveIconStyle
      }
    }, [this.renderRateIcon()])]) : h("div", [h("div", {
      "class": `${classPrefix}-rate__star-top`,
      "style": {
        ...this.activeIconStyle
      }
    }, [this.renderRateIcon()]), h("div", {
      "class": `${classPrefix}-rate__star-bottom`,
      "style": {
        ...this.inactiveIconStyle
      }
    }, [this.renderRateIcon()])])]))]), this.showText && h("div", {
      "class": `${classPrefix}-rate__text`
    }, [displayText[displayValue - 1]])]);
  }
});

export { _Rate as default };
//# sourceMappingURL=rate.js.map
