"use strict";

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); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.Rating = void 0;
var _react = _interopRequireWildcard(require("react"));
var _ = require("../..");
var _utils = require("../../common/utils");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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; }
var restrictRange = function restrictRange(rating) {
  var outOf = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
  if (!(0, _utils.isNumber)(rating) || rating < 0) return 0;
  if (rating > outOf) return outOf;
  return rating;
};
var getIcon = function getIcon(size, active, activeIcon, inactiveIcon) {
  if ((0, _react.isValidElement)(activeIcon) && (0, _react.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.default.createElement(_.Icon.StarFill12, {
        color: fillColor
      });
    case 'm':
    case 4:
      return _react.default.createElement(_.Icon.StarFill16, {
        color: fillColor
      });
    case 'l':
    case 6:
      return _react.default.createElement(_.Icon.StarFill24, {
        color: fillColor
      });
    case 'xl':
    case 8:
      return _react.default.createElement(_.Icon.StarFill32, {
        color: fillColor
      });
    default:
      return _react.default.createElement(_.Icon.StarFill16, {
        color: fillColor
      });
  }
};
var Rating = exports.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.default.createElement(_.Flexbox, {
    container: true,
    gap: "2px",
    height: height
  }, _react.default.createElement(_.VisuallyHidden, null, screenReaderText), (_ref2 = _toConsumableArray(Array(outOf))) === null || _ref2 === void 0 ? void 0 : _ref2.map(function (star, i) {
    return _react.default.createElement(_.Flexbox, {
      item: true,
      key: star,
      container: true,
      alignItems: "center"
    }, getIcon(size, i < finalRating, activeIcon, inactiveIcon));
  }));
};
var _default = exports.default = Rating;