/**
 * 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 iconsVue = require('@ldesign/icons-vue');
var hooks_useVmodel = require('../../hooks/use-vmodel.js');
var components_configProvider_useConfig = require('../config-provider/useConfig.js');
var components_tooltip_index = require('../tooltip/index.js');
var utils_renderNode = require('../../utils/render-node.js');
var components_rate_props = require('./props.js');
require('lodash/kebabCase');
require('../config-provider/context.js');
require('lodash');
require('../../chunks/zh_CN.js');
require('../../utils/map-props.js');
require('vue');
require('../../utils/with-install.js');
require('../../config.js');
require('../tooltip/tooltip.js');
require('../../chunks/helper.js');
require('lodash/isFunction');
require('../popup/props.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../../chunks/set-style.js');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../chunks/log.js');
require('../../chunks/_commonjsHelpers-2e6815bd.js');
require('../../utils/easing.js');
require('../../utils/helper.js');
require('lodash/camelCase');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../utils/mixins.js');
require('../popup/container.js');
require('../tooltip/props.js');

var _Rate = VueCompositionAPI.defineComponent({
  name: "LRate",
  props: {
    ...components_rate_props["default"]
  },
  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
    } = VueCompositionAPI.toRefs(props2);
    const [starValue, setStarValue] = hooks_useVmodel["default"](inputValue, props2.defaultValue, props2.onChange, "change");
    const hoverValue = VueCompositionAPI.ref(void 0);
    const rootRef = VueCompositionAPI.ref();
    const {
      classPrefix
    } = components_configProvider_useConfig.useConfig("classPrefix");
    const displayValue = VueCompositionAPI.computed(() => Number(hoverValue.value || starValue.value));
    const displayText = VueCompositionAPI.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 = VueCompositionAPI.computed(() => ({
      fontSize: props2.size,
      color: activeColor
    }));
    const inactiveIconStyle = VueCompositionAPI.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 utils_renderNode.renderLNodeJSXDefault(this, "icon", h(iconsVue.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(components_tooltip_index.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]])]);
  }
});

exports["default"] = _Rate;
//# sourceMappingURL=rate.js.map
