/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @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, computed } from '@vue/composition-api';
import { StarFilledIcon } from '@ldesign/icons-vue';
import useVModel from '../hooks/useVModel.js';
import '../hooks/useConfig.js';
import { Tooltip } from '../tooltip/index.js';
import { renderTNodeJSXDefault } from '../utils/render-tnode.js';
import props from './props.js';
import { useConfig } from '../config-provider/useConfig.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/kebabCase';
import '../utils/map-props.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'vue';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import '../tooltip/tooltip.js';
import '../_chunks/dep-c89aafbb.js';
import 'lodash/isFunction';
import '../popup/props.js';
import '../popup/popup.js';
import '@popperjs/core';
import '../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../utils/easing.js';
import '../utils/helper.js';
import 'lodash/camelCase';
import '../_common/js/utils/set-style.js';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-be599ec8.js';
import 'dayjs';
import '../_chunks/dep-67ff4010.js';
import '../config-provider/type.js';
import '../utils/mixins.js';
import '../utils/event.js';
import '../popup/container.js';
import '../popup/utils.js';
import '../popup/trigger.js';
import '../tooltip/props.js';
import '../tooltip/style/css.js';
import '../tooltip/type.js';

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

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