import React, { isValidElement } from 'react';
import { Flexbox, Icon, VisuallyHidden } from '../..';
import { isNumber } from '../../common/utils';
var restrictRange = function restrictRange(rating) {
  var outOf = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
  if (!isNumber(rating) || rating < 0) return 0;
  if (rating > outOf) return outOf;
  return rating;
};
var getIcon = function getIcon(size, active, activeIcon, inactiveIcon) {
  if (isValidElement(activeIcon) && isValidElement(inactiveIcon)) {
    return active ? activeIcon : inactiveIcon;
  }
  var fillColor = function fillColor(t) {
    return active ? t.color.starRating : t.color.starRatingOff;
  };
  switch (size) {
    case 's':
    case 3:
      return React.createElement(Icon.StarFill12, {
        color: fillColor
      });
    case 'm':
    case 4:
      return React.createElement(Icon.StarFill16, {
        color: fillColor
      });
    case 'l':
    case 6:
      return React.createElement(Icon.StarFill24, {
        color: fillColor
      });
    case 'xl':
    case 8:
      return React.createElement(Icon.StarFill32, {
        color: fillColor
      });
    default:
      return React.createElement(Icon.StarFill16, {
        color: fillColor
      });
  }
};
export var Rating = function Rating(_ref) {
  var _ref$rating = _ref.rating,
    rating = _ref$rating === void 0 ? 0 : _ref$rating,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 'm' : _ref$size,
    _ref$outOf = _ref.outOf,
    outOf = _ref$outOf === void 0 ? 5 : _ref$outOf,
    _ref$height = _ref.height,
    height = _ref$height === void 0 ? 'auto' : _ref$height,
    activeIcon = _ref.activeIcon,
    inactiveIcon = _ref.inactiveIcon;
  var finalRating = restrictRange(rating, outOf);
  var screenReaderText = rating === 1 ? "".concat(rating, " star") : "".concat(rating, " stars");
  return React.createElement(Flexbox, {
    container: true,
    gap: "2px",
    height: height
  }, React.createElement(VisuallyHidden, null, screenReaderText), Array.from({
    length: outOf
  }).map(function (_, i) {
    return React.createElement(Flexbox, {
      item: true,
      key: "star_".concat(i + 1),
      container: true,
      alignItems: "center"
    }, getIcon(size, i < finalRating, activeIcon, inactiveIcon));
  }));
};
export default Rating;