function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
import { rpx2vw4style as __rpx2vw__ } from "babel-runtime-jsx-style-transform";
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import classNames from 'classnames';
import React, { createElement, forwardRef, useContext } from "react";
import View from '../view';
import Icon from '../icon';
import Form from '../form';
import { useValue } from '../utils/hooks';
var Rating = function Rating(props, ref) {
  var _classNames;
  var _props$prefix = props.prefix,
    prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
    sizeProp = props.size,
    _props$disabled = props.disabled,
    disabled = _props$disabled === void 0 ? false : _props$disabled,
    _props$readOnly = props.readOnly,
    readOnly = _props$readOnly === void 0 ? false : _props$readOnly,
    _props$count = props.count,
    count = _props$count === void 0 ? 5 : _props$count,
    _props$iconType = props.iconType,
    iconType = _props$iconType === void 0 ? 'favorites-filling' : _props$iconType,
    onChange = props.onChange,
    className = props.className,
    style = props.style;
  var _useValue = useValue(props, 0),
    _useValue2 = _slicedToArray(_useValue, 3),
    value = _useValue2[0],
    setValue = _useValue2[1],
    isControlled = _useValue2[2];
  var _useContext = useContext(Form.ItemContext),
    contentAlign = _useContext.contentAlign,
    contextSize = _useContext.size;
  var size = sizeProp || contextSize;
  var gradeType = value > count * 0.4 ? 'high' : 'low';
  var iconClassName = classNames("".concat(prefix, "rating--").concat(size, "-icon"), "".concat(prefix, "rating-icon"));
  var classnames = {
    rating: classNames("".concat(prefix, "rating"), "".concat(prefix, "rating--").concat(size), "".concat(prefix, "rating--").concat(contentAlign), "".concat(prefix, "rating--grade-").concat(gradeType), (_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "rating--readonly"), readOnly), _defineProperty(_classNames, "".concat(prefix, "rating--disabled"), disabled), _classNames), className),
    iconWrapper: classNames("".concat(prefix, "rating--").concat(size, "-icon-wrapper"), "".concat(prefix, "rating-icon-wrapper")),
    icon: iconClassName,
    activeIcon: classNames(iconClassName, "".concat(prefix, "rating-icon--active"), "".concat(prefix, "rating-icon--active-").concat(gradeType))
  };
  var handleClick = function handleClick(v) {
    if (disabled || readOnly || v < 0) {
      return;
    }
    if (!isControlled) {
      setValue(v);
    }
    if (onChange) {
      onChange(v);
    }
  };
  var stars = [];
  for (var i = 0; i < count; i++) {
    var _classNames2;
    var isActive = Math.floor(value - 1) >= i;
    var half = value - i < 1 && value - i > 0 ? /*#__PURE__*/React.createElement(View, {
      key: "".concat(i, "-overlay"),
      className: classNames(classnames.iconWrapper, "".concat(classnames.iconWrapper, "--overlay")),
      onClick: handleClick.bind(null, i + 1)
    }, /*#__PURE__*/React.createElement(Icon, {
      name: iconType,
      size: size,
      className: classnames.activeIcon,
      style: __rpx2vw__({
        width: "".concat(100 * (value - i), "%")
      })
    })) : null;
    stars.push( /*#__PURE__*/React.createElement(View, {
      key: i,
      className: classNames(classnames.iconWrapper, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(classnames.iconWrapper, "--first"), i === 0), _defineProperty(_classNames2, "".concat(classnames.iconWrapper, "--last"), i === count - 1), _classNames2)),
      onClick: handleClick.bind(null, i + 1)
    }, /*#__PURE__*/React.createElement(Icon, {
      name: iconType,
      size: size,
      className: isActive ? classnames.activeIcon : classnames.icon
    }), half));
  }
  return /*#__PURE__*/React.createElement(View, {
    ref: ref,
    className: classnames.rating,
    style: __rpx2vw__(style)
  }, stars);
};
Rating.displayName = 'Rating';
export default /*#__PURE__*/forwardRef(Rating);