"use strict";

exports.__esModule = true;
exports.default = void 0;
var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");
var _classnames = _interopRequireDefault(require("classnames"));
var _react = _interopRequireWildcard(require("react"));
var _view = _interopRequireDefault(require("../view"));
var _icon = _interopRequireDefault(require("../icon"));
var _form = _interopRequireDefault(require("../form"));
var _hooks = require("../utils/hooks");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var Rating = function (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 = (0, _hooks.useValue)(props, 0),
    value = _useValue[0],
    setValue = _useValue[1],
    isControlled = _useValue[2];
  var _useContext = (0, _react.useContext)(_form.default.ItemContext),
    contentAlign = _useContext.contentAlign,
    contextSize = _useContext.size;
  var size = sizeProp || contextSize;
  var gradeType = value > count * 0.4 ? 'high' : 'low';
  var iconClassName = (0, _classnames.default)(prefix + "rating--" + size + "-icon", prefix + "rating-icon");
  var classnames = {
    rating: (0, _classnames.default)(prefix + "rating", prefix + "rating--" + size, prefix + "rating--" + contentAlign, prefix + "rating--grade-" + gradeType, (_classNames = {}, _classNames[prefix + "rating--readonly"] = readOnly, _classNames[prefix + "rating--disabled"] = disabled, _classNames), className),
    iconWrapper: (0, _classnames.default)(prefix + "rating--" + size + "-icon-wrapper", prefix + "rating-icon-wrapper"),
    icon: iconClassName,
    activeIcon: (0, _classnames.default)(iconClassName, prefix + "rating-icon--active", prefix + "rating-icon--active-" + gradeType)
  };
  var handleClick = function (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__*/(0, _react.createElement)(_view.default, {
      key: i + "-overlay",
      className: (0, _classnames.default)(classnames.iconWrapper, classnames.iconWrapper + "--overlay"),
      onClick: handleClick.bind(null, i + 1)
    }, /*#__PURE__*/(0, _react.createElement)(_icon.default, {
      name: iconType,
      size: size,
      className: classnames.activeIcon,
      style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)({
        width: 100 * (value - i) + "%"
      })
    })) : null;
    stars.push( /*#__PURE__*/(0, _react.createElement)(_view.default, {
      key: i,
      className: (0, _classnames.default)(classnames.iconWrapper, (_classNames2 = {}, _classNames2[classnames.iconWrapper + "--first"] = i === 0, _classNames2[classnames.iconWrapper + "--last"] = i === count - 1, _classNames2)),
      onClick: handleClick.bind(null, i + 1)
    }, /*#__PURE__*/(0, _react.createElement)(_icon.default, {
      name: iconType,
      size: size,
      className: isActive ? classnames.activeIcon : classnames.icon
    }), half));
  }
  return /*#__PURE__*/(0, _react.createElement)(_view.default, {
    ref: ref,
    className: classnames.rating,
    style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)(style)
  }, stars);
};
Rating.displayName = 'Rating';
var _default = /*#__PURE__*/(0, _react.forwardRef)(Rating);
exports.default = _default;