/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var iconsVueNext = require('@ldesign/icons-vue-next');
var isArray = require('lodash/isArray');
var hooks_useVModel = require('../hooks/useVModel.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
var hooks_tnode = require('../hooks/tnode.js');
var tooltip_index = require('../tooltip/index.js');
var rate_props = require('./props.js');
require('lodash/kebabCase');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-f4db2590.js');
require('dayjs');
require('lodash/camelCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../utils/withInstall.js');
require('../tooltip/tooltip.js');
require('lodash/omit');
require('../popup/props.js');
require('../popup/index.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../hooks/useConfig.js');
require('../utils/dom.js');
require('../utils/easing.js');
require('../_common/js/utils/set-style.js');
require('../popup/container.js');
require('lodash/isUndefined');
require('../hooks/useResizeObserver.js');
require('../tooltip/util.js');
require('../tooltip/props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var isArray__default = /*#__PURE__*/_interopDefaultLegacy(isArray);

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__default["default"](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 = vue.defineComponent({
  name: "TRate",
  props: _objectSpread({}, rate_props["default"]),
  setup: function setup(props2, _ref) {
    var slots = _ref.slots;
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    var activeColor = isArray__default["default"](props2.color) ? props2.color[0] : props2.color;
    var defaultColor = isArray__default["default"](props2.color) ? props2.color[1] : "var(--td-bg-color-component)";
    var _toRefs = vue.toRefs(props2),
      inputValue = _toRefs.value,
      modelValue = _toRefs.modelValue;
    var _useVModel = hooks_useVModel["default"](inputValue, modelValue, props2.defaultValue, props2.onChange),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      starValue = _useVModel2[0],
      setStarValue = _useVModel2[1];
    var hoverValue = vue.ref(void 0);
    var root = vue.ref();
    var displayValue = vue.computed(function () {
      return Number(hoverValue.value || starValue.value);
    });
    var displayText = vue.computed(function () {
      return props2.texts.length === 0 ? ["\u6781\u5DEE", "\u5931\u671B", "\u4E00\u822C", "\u6EE1\u610F", "\u60CA\u559C"] : props2.texts;
    });
    var RateIcon = function RateIcon(iconProps) {
      if (slots.icon !== void 0) {
        return renderTNodeJSX("icon", {
          params: iconProps
        });
      }
      return vue.createVNode(iconsVueNext.StarFilledIcon, iconProps, null);
    };
    var getStarValue = function getStarValue(event, index) {
      if (props2.allowHalf) {
        var _root$value$getBoundi = root.value.getBoundingClientRect(),
          left = _root$value$getBoundi.left;
        var firstStar = root.value.firstChild.nextSibling;
        var _firstStar$getBoundin = firstStar.getBoundingClientRect(),
          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 _useConfig = configProvider_useConfig.useConfig("classPrefix"),
      classPrefix = _useConfig.classPrefix;
    return function () {
      return vue.createVNode("div", {
        "class": "".concat(classPrefix.value, "-rate"),
        "onMouseleave": mouseLeaveHandler
      }, [vue.createVNode("ul", {
        "class": "".concat(classPrefix.value, "-rate__list"),
        "style": {
          gap: "".concat(props2.gap, "px")
        },
        "ref": root
      }, [_toConsumableArray__default["default"](Array(Number(props2.count))).map(function (_, index) {
        return vue.createVNode("li", {
          "key": index,
          "class": ["".concat(classPrefix.value, "-rate__item"), getStarCls(index)],
          "onClick": function onClick(event) {
            return clickHandler(event, index + 1);
          },
          "onMousemove": function onMousemove(event) {
            return mouseEnterHandler(event, index + 1);
          }
        }, [props2.showText ? vue.createVNode(tooltip_index.Tooltip, {
          "key": index,
          "content": displayText.value[displayValue.value - 1]
        }, {
          "default": function _default() {
            return [vue.createVNode("div", {
              "class": "".concat(classPrefix.value, "-rate__star-top")
            }, [vue.createVNode(RateIcon, {
              "size": props2.size,
              "color": activeColor
            }, null)]), vue.createVNode("div", {
              "class": "".concat(classPrefix.value, "-rate__star-bottom")
            }, [vue.createVNode(RateIcon, {
              "size": props2.size,
              "color": defaultColor
            }, null)])];
          }
        }) : vue.createVNode(vue.Fragment, null, [vue.createVNode("div", {
          "class": "".concat(classPrefix.value, "-rate__star-top")
        }, [vue.createVNode(RateIcon, {
          "size": props2.size,
          "color": activeColor
        }, null)]), vue.createVNode("div", {
          "class": "".concat(classPrefix.value, "-rate__star-bottom")
        }, [vue.createVNode(RateIcon, {
          "size": props2.size,
          "color": defaultColor
        }, null)])])]);
      })]), props2.showText && vue.createVNode("div", {
        "class": "".concat(classPrefix.value, "-rate__text")
      }, [displayText.value[displayValue.value - 1]])]);
    };
  }
});

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