'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var classNames = require('clsx');
var useId = require('@prom-ui/core/useId');
var useIsMounted = require('@prom-ui/core/useIsMounted');
var StarFill = require('@prom-ui/icons/StarFill');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

function _interopNamespaceCompat(e) {
  if (e && typeof e === 'object' && 'default' in e) return e;
  var n = Object.create(null);
  if (e) {
    Object.keys(e).forEach(function (k) {
      if (k !== 'default') {
        var d = Object.getOwnPropertyDescriptor(e, k);
        Object.defineProperty(n, k, d.get ? d : {
          enumerable: true,
          get: function () { return e[k]; }
        });
      }
    });
  }
  n.default = e;
  return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
var classNames__default = /*#__PURE__*/_interopDefaultCompat(classNames);

function _iterableToArrayLimit(arr, i) {
  var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
  if (null != _i) {
    var _s,
      _e,
      _x,
      _r,
      _arr = [],
      _n = !0,
      _d = !1;
    try {
      if (_x = (_i = _i.call(arr)).next, 0 === i) {
        if (Object(_i) !== _i) return;
        _n = !1;
      } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
    } catch (err) {
      _d = !0, _e = err;
    } finally {
      try {
        if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
      } finally {
        if (_d) throw _e;
      }
    }
    return _arr;
  }
}
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 _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _slicedToArray(arr, i) {
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}
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 _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 _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 _toPropertyKey(arg) {
  var key = _toPrimitive(arg, "string");
  return typeof key === "symbol" ? key : String(key);
}

var css = {"root":"RatingSelect__root___8RSGF","label":"RatingSelect__label___FF3m3","icon":"RatingSelect__icon___1Zpyt","input":"RatingSelect__input___25gdX","hovered":"RatingSelect__hovered___DscdS"};

var _excluded = ["value", "name", "onChange", "onHover", "size", "disabled"];
var ITEMS_LIST = [1, 2, 3, 4, 5];
var RatingSelect = function RatingSelect(_ref) {
  var value = _ref.value,
    name = _ref.name,
    onChange = _ref.onChange,
    onHover = _ref.onHover,
    _ref$size = _ref.size,
    size = _ref$size === void 0 ? 18 : _ref$size,
    disabled = _ref.disabled,
    props = _objectWithoutProperties(_ref, _excluded);
  var isMounted = useIsMounted.useIsMounted();
  var componentId = useId.useId();
  var _React$useState = React__namespace.useState(value),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    selectedValue = _React$useState2[0],
    setSelectedValue = _React$useState2[1];
  var _React$useState3 = React__namespace.useState(true),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    isHovered = _React$useState4[0],
    setIsHovered = _React$useState4[1];
  var inputName = name || componentId;
  React__namespace.useEffect(function () {
    if (isMounted && typeof onChange === 'function') {
      onChange(selectedValue);
    }
  }, [selectedValue]);
  React__namespace.useEffect(function () {
    if (value !== selectedValue) {
      setSelectedValue(value);
    }
  }, [value]);
  var onInputChange = function onInputChange(inputValue) {
    setIsHovered(false);
    setSelectedValue(inputValue);
  };
  var onLabelClick = function onLabelClick(inputValue, e) {
    setIsHovered(false);
    if (!disabled && selectedValue === inputValue) {
      e === null || e === void 0 ? void 0 : e.preventDefault();
      setSelectedValue(undefined);
    }
  };
  var onLabelMouseEnter = function onLabelMouseEnter(inputValue) {
    if (!isHovered) {
      setIsHovered(true);
    }
    if (typeof onHover === 'function') {
      onHover(inputValue);
    }
  };
  var onRootMouseEnter = function onRootMouseEnter() {
    if (typeof onHover === 'function') {
      onHover(undefined);
    }
  };
  return /*#__PURE__*/React__namespace.createElement("div", {
    className: classNames__default.default(css.root, _defineProperty({}, css.hovered, isHovered)),
    style: {
      fontSize: size
    },
    onMouseLeave: onRootMouseEnter,
    "data-qaid": props['data-qaid'] || 'rating_select',
    "data-testid": props['data-testid'] || 'rating_select'
  }, ITEMS_LIST.map(function (inputValue) {
    var inputId = "".concat(inputName, "-").concat(inputValue);
    return /*#__PURE__*/React__namespace.createElement("input", {
      key: inputValue,
      id: inputId,
      className: css.input,
      type: "radio",
      name: inputName,
      value: inputValue,
      checked: selectedValue === inputValue,
      onChange: function onChange() {
        return onInputChange(inputValue);
      },
      disabled: disabled,
      "data-qaid": "rating_select_input_".concat(inputValue),
      "data-testid": "rating_select_input_".concat(inputValue)
    });
  }), ITEMS_LIST.map(function (inputValue) {
    var inputId = "".concat(inputName, "-").concat(inputValue);
    return /*#__PURE__*/React__namespace.createElement("label", {
      key: inputValue,
      className: css.label,
      htmlFor: inputId,
      onClick: function onClick(e) {
        return onLabelClick(inputValue, e);
      },
      onMouseEnter: function onMouseEnter() {
        return onLabelMouseEnter(inputValue);
      },
      "data-qaid": "rating_select_label_".concat(inputValue),
      "data-testid": "rating_select_label_".concat(inputValue)
    }, /*#__PURE__*/React__namespace.createElement(StarFill.SvgStarFill, {
      className: css.icon,
      focusable: false,
      "aria-hidden": true
    }));
  }));
};
RatingSelect.displayName = 'RatingSelect';

exports.RatingSelect = RatingSelect;

require('./style.development.css');
