/**
 * ldesign v0.4.6
 * (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 VueCompositionAPI = require('@vue/composition-api');
var iconsVue = require('@ldesign/icons-vue');
var hooks_useVModel = require('../hooks/useVModel.js');
var configProvider_useConfig = require('../config-provider/useConfig.js');
var tooltip_index = require('../tooltip/index.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var rate_props = require('./props.js');
require('@babel/runtime/helpers/typeof');
require('lodash/kebabCase');
require('../config-provider/context.js');
require('lodash/merge');
require('lodash/mergeWith');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../_chunks/dep-7c884501.js');
require('dayjs');
require('../utils/map-props.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('vue');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../tooltip/tooltip.js');
require('../_chunks/dep-c3e1ffac.js');
require('lodash/isFunction');
require('../popup/props.js');
require('../popup/popup.js');
require('@popperjs/core');
require('../utils/dom.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('../utils/helper.js');
require('lodash/camelCase');
require('../_common/js/utils/set-style.js');
require('../config-provider/config-receiver.js');
require('../utils/mixins.js');
require('../utils/event.js');
require('../popup/container.js');
require('../popup/utils.js');
require('../popup/trigger.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);

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 = VueCompositionAPI.defineComponent({
  name: "TRate",
  props: _objectSpread({}, rate_props["default"]),
  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 = VueCompositionAPI.toRefs(props2),
      inputValue = _toRefs.value;
    var _useVModel = hooks_useVModel["default"](inputValue, props2.defaultValue, props2.onChange, "change"),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      starValue = _useVModel2[0],
      setStarValue = _useVModel2[1];
    var hoverValue = VueCompositionAPI.ref(void 0);
    var rootRef = VueCompositionAPI.ref();
    var _useConfig = configProvider_useConfig.useConfig("classPrefix"),
      classPrefix = _useConfig.classPrefix;
    var displayValue = VueCompositionAPI.computed(function () {
      return Number(hoverValue.value || starValue.value);
    });
    var displayText = VueCompositionAPI.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 = VueCompositionAPI.computed(function () {
      return {
        fontSize: props2.size,
        color: activeColor
      };
    });
    var inactiveIconStyle = VueCompositionAPI.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 utils_renderTnode.renderTNodeJSXDefault(this, "icon", h(iconsVue.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__default["default"](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_index.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]])]);
  }
});

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