function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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; }
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 _ref2;
  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), (_ref2 = _toConsumableArray(Array(outOf))) === null || _ref2 === void 0 ? void 0 : _ref2.map(function (star, i) {
    return React.createElement(Flexbox, {
      item: true,
      key: star,
      container: true,
      alignItems: "center"
    }, getIcon(size, i < finalRating, activeIcon, inactiveIcon));
  }));
};
export default Rating;