'use strict';

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

var vue = require('vue');
var CloseOutlined = require('@ant-design/icons-vue');
var classNames = require('../_util/classNames.js');
var index$1 = require('../_util/vue-types/index.js');
var KeyCode = require('../_util/KeyCode.js');
var type = require('../_util/type.js');
var index$3 = require('../_util/props-util/index.js');
var index$4 = require('../tooltip/index.js');
require('../hooks/index.js');
var FormItemContext = require('../form/FormItemContext.js');
var index$2 = require('./style/index.js');
var Star = require('./Star.js');
var util = require('./util.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');
var useRefs = require('../hooks/useRefs.js');

function _isSlot(s) {
  return typeof s === "function" || Object.prototype.toString.call(s) === "[object Object]" && !vue.isVNode(s);
}
const rateProps = () => ({
  "prefixCls": String,
  "count": {
    type: Number,
    default: 5
  },
  "value": {
    type: Number,
    default: 0
  },
  "allowHalf": {
    type: Boolean,
    default: false
  },
  "allowClear": {
    type: Boolean,
    default: true
  },
  "tooltips": Array,
  "disabled": {
    type: Boolean,
    default: void 0
  },
  "character": index$1.default.any,
  "autofocus": {
    type: Boolean,
    default: void 0
  },
  "tabindex": {
    type: [Number, String],
    default: 0
  },
  "direction": {
    type: String,
    default: "ltr"
  },
  "id": String,
  "onChange": Function,
  "onHoverChange": Function,
  "onUpdate:value": Function,
  "onFocus": Function,
  "onBlur": Function,
  "onKeydown": Function
});
const Rate = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "ARate",
  inheritAttrs: false,
  props: rateProps(),
  // emits: ['hoverChange', 'update:value', 'change', 'focus', 'blur', 'keydown'],
  setup(props, {
    slots,
    attrs,
    emit,
    expose
  }) {
    const {
      prefixCls,
      direction
    } = useConfigInject.default("rate", props);
    const [wrapSSR, hashId] = index$2.default(prefixCls);
    const formItemContext = FormItemContext.useInjectFormItemContext();
    const rateRef = vue.ref();
    const [setRef, starRefs] = useRefs.useRefs();
    const state = vue.reactive({
      value: props.value,
      focused: false,
      cleanedValue: null,
      hoverValue: void 0
    });
    vue.watch(() => props.value, () => {
      state.value = props.value;
    });
    const getStarDOM = (index) => {
      return index$3.findDOMNode(starRefs.value.get(index));
    };
    const getStarValue = (index, x) => {
      const reverse = direction.value === "rtl";
      let value = index + 1;
      if (props.allowHalf) {
        const starEle = getStarDOM(index);
        const leftDis = util.getOffsetLeft(starEle);
        const width = starEle.clientWidth;
        if (reverse && x - leftDis > width / 2)
          value -= 0.5;
        else if (!reverse && x - leftDis < width / 2)
          value -= 0.5;
      }
      return value;
    };
    const changeValue = (value) => {
      if (props.value === void 0)
        state.value = value;
      emit("update:value", value);
      emit("change", value);
      formItemContext.onFieldChange();
    };
    const onHover = (e, index) => {
      const hoverValue = getStarValue(index, e.pageX);
      if (hoverValue !== state.cleanedValue) {
        state.hoverValue = hoverValue;
        state.cleanedValue = null;
      }
      emit("hoverChange", hoverValue);
    };
    const onMouseLeave = () => {
      state.hoverValue = void 0;
      state.cleanedValue = null;
      emit("hoverChange", void 0);
    };
    const onClick = (event, index) => {
      const {
        allowClear
      } = props;
      const newValue = getStarValue(index, event.pageX);
      let isReset = false;
      if (allowClear)
        isReset = newValue === state.value;
      onMouseLeave();
      changeValue(isReset ? 0 : newValue);
      state.cleanedValue = isReset ? newValue : null;
    };
    const onFocus = (e) => {
      state.focused = true;
      emit("focus", e);
    };
    const onBlur = (e) => {
      state.focused = false;
      emit("blur", e);
      formItemContext.onFieldBlur();
    };
    const onKeyDown = (event) => {
      const {
        keyCode
      } = event;
      const {
        count,
        allowHalf
      } = props;
      const reverse = direction.value === "rtl";
      if (keyCode === KeyCode.default.RIGHT && state.value < count && !reverse) {
        if (allowHalf)
          state.value += 0.5;
        else
          state.value += 1;
        changeValue(state.value);
        event.preventDefault();
      } else if (keyCode === KeyCode.default.LEFT && state.value > 0 && !reverse) {
        if (allowHalf)
          state.value -= 0.5;
        else
          state.value -= 1;
        changeValue(state.value);
        event.preventDefault();
      } else if (keyCode === KeyCode.default.RIGHT && state.value > 0 && reverse) {
        if (allowHalf)
          state.value -= 0.5;
        else
          state.value -= 1;
        changeValue(state.value);
        event.preventDefault();
      } else if (keyCode === KeyCode.default.LEFT && state.value < count && reverse) {
        if (allowHalf)
          state.value += 0.5;
        else
          state.value += 1;
        changeValue(state.value);
        event.preventDefault();
      }
      emit("keydown", event);
    };
    const focus = () => {
      if (!props.disabled)
        rateRef.value.focus();
    };
    const blur = () => {
      if (!props.disabled)
        rateRef.value.blur();
    };
    expose({
      focus,
      blur
    });
    vue.onMounted(() => {
      const {
        autofocus,
        disabled
      } = props;
      if (autofocus && !disabled)
        focus();
    });
    const characterRender = (node, {
      index
    }) => {
      const {
        tooltips
      } = props;
      if (!tooltips)
        return node;
      return vue.createVNode(index$4.default, {
        "title": tooltips[index]
      }, _isSlot(node) ? node : {
        default: () => [node]
      });
    };
    return () => {
      const {
        count,
        allowHalf,
        disabled,
        tabindex,
        id = formItemContext.id.value
      } = props;
      const {
        class: className,
        style
      } = attrs;
      const stars = [];
      const disabledClass = disabled ? `${prefixCls.value}-disabled` : "";
      const character = props.character || slots.character || (() => vue.createVNode(CloseOutlined.StarFilled, null, null));
      for (let index = 0; index < count; index++) {
        stars.push(vue.createVNode(Star.default, {
          "ref": setRef(index),
          "key": index,
          "index": index,
          "count": count,
          "disabled": disabled,
          "prefixCls": `${prefixCls.value}-star`,
          "allowHalf": allowHalf,
          "value": state.hoverValue === void 0 ? state.value : state.hoverValue,
          "onClick": onClick,
          "onHover": onHover,
          "character": character,
          "characterRender": characterRender,
          "focused": state.focused
        }, null));
      }
      const rateClassName = classNames.default(prefixCls.value, disabledClass, className, {
        [hashId.value]: true,
        [`${prefixCls.value}-rtl`]: direction.value === "rtl"
      });
      return wrapSSR(vue.createVNode("ul", vue.mergeProps(attrs, {
        "id": id,
        "class": rateClassName,
        "style": style,
        "onMouseleave": disabled ? null : onMouseLeave,
        "tabindex": disabled ? -1 : tabindex,
        "onFocus": disabled ? null : onFocus,
        "onBlur": disabled ? null : onBlur,
        "onKeydown": disabled ? null : onKeyDown,
        "ref": rateRef,
        "role": "radiogroup"
      }), [stars]));
    };
  }
});
var index = type.withInstall(Rate);

exports.default = index;
exports.rateProps = rateProps;
//# sourceMappingURL=index.js.map
