'use strict';

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

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var index$1 = require('../../utils/index.js');
var Rating_styles = require('./Rating.styles.js');
var base = require('./variations/_view/base.js');
var base$1 = require('./variations/_size/base.js');
var index = require('./utils/index.js');
var Rating_tokens = require('./Rating.tokens.js');
var getIcons = require('./utils/getIcons.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var _excluded = ["value", "hasValue", "precision", "decimalSeparator", "valuePlacement", "iconSlot", "iconSlotOutline", "iconSlotHalf", "hasIcons", "iconQuantity", "helperText", "helperTextStretching", "size", "view"];
var ratingRoot = function ratingRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (_ref, ref) {
    var value = _ref.value,
      _ref$hasValue = _ref.hasValue,
      hasValue = _ref$hasValue === void 0 ? true : _ref$hasValue,
      _ref$precision = _ref.precision,
      precision = _ref$precision === void 0 ? 1 : _ref$precision,
      _ref$decimalSeparator = _ref.decimalSeparator,
      decimalSeparator = _ref$decimalSeparator === void 0 ? '.' : _ref$decimalSeparator,
      valuePlacement = _ref.valuePlacement,
      iconSlot = _ref.iconSlot,
      iconSlotOutline = _ref.iconSlotOutline,
      iconSlotHalf = _ref.iconSlotHalf,
      _ref$hasIcons = _ref.hasIcons,
      hasIcons = _ref$hasIcons === void 0 ? true : _ref$hasIcons,
      _ref$iconQuantity = _ref.iconQuantity,
      iconQuantity = _ref$iconQuantity === void 0 ? 5 : _ref$iconQuantity,
      helperText = _ref.helperText,
      _ref$helperTextStretc = _ref.helperTextStretching,
      helperTextStretching = _ref$helperTextStretc === void 0 ? 'filled' : _ref$helperTextStretc,
      size = _ref.size,
      view = _ref.view,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    var innerValue = index.getInnerValue(value, precision, iconQuantity);
    var displayValue = index.getDisplayValue(innerValue, decimalSeparator);
    var contentRef = React.useRef(null);
    var starsWrapperRef = React.useRef(null);
    var _useState = React.useState('unset'),
      _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
      helperTextMaxWidth = _useState2[0],
      setHelperTextMaxWidth = _useState2[1];
    React.useEffect(function () {
      if (!hasValue && !hasIcons) {
        // eslint-disable-next-line no-console
        console.error("Rating should have at least one of 'hasValue' or 'hasIcons' props");
      }
    }, [hasValue, hasIcons]);
    React.useEffect(function () {
      if (helperTextStretching === 'filled') {
        setHelperTextMaxWidth('unset');
        return;
      }
      var helpertextPlacement = index.getHelperTextPlacement(value, size, hasIcons, iconQuantity);
      if (helpertextPlacement === index.helperTextPlacements.inner) {
        if (!(starsWrapperRef !== null && starsWrapperRef !== void 0 && starsWrapperRef.current)) {
          setHelperTextMaxWidth('unset');
          return;
        }
        var starsWrapperWidth = starsWrapperRef.current.offsetWidth;
        setHelperTextMaxWidth("".concat(starsWrapperWidth, "px"));
      } else if (helpertextPlacement === index.helperTextPlacements.outer) {
        if (!(contentRef !== null && contentRef !== void 0 && contentRef.current)) {
          setHelperTextMaxWidth('unset');
          return;
        }
        var _starsWrapperWidth = contentRef.current.offsetWidth;
        setHelperTextMaxWidth("".concat(_starsWrapperWidth, "px"));
      }
    }, [helperTextStretching, helperText, hasValue, hasIcons, precision, value, iconQuantity]);

    // INFO: Если выключено отображение иконок и значения, нет смысла показывать только helperText
    if (!hasValue && !hasIcons) {
      return null;
    }
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      ref: ref,
      view: view,
      size: size
    }, rest), /*#__PURE__*/React__default.default.createElement(Rating_styles.Content, {
      ref: contentRef,
      className: index$1.cx(valuePlacement === 'after' && Rating_tokens.classes.valueAfter, iconQuantity === 1 && Rating_tokens.classes.singleIconContent)
    }, hasValue && innerValue && /*#__PURE__*/React__default.default.createElement(Rating_styles.Score, null, displayValue), hasIcons && /*#__PURE__*/React__default.default.createElement(Rating_styles.Wrapper, null, /*#__PURE__*/React__default.default.createElement(Rating_styles.StarsWrapper, {
      ref: starsWrapperRef
    }, getIcons.getIcons({
      innerValue: innerValue,
      size: size,
      iconQuantity: iconQuantity,
      iconSlot: iconSlot,
      iconSlotOutline: iconSlotOutline,
      iconSlotHalf: iconSlotHalf
    })), helperText && index.getHelperTextPlacement(value, size, hasIcons, iconQuantity) === index.helperTextPlacements.inner && /*#__PURE__*/React__default.default.createElement(Rating_styles.HelperText, {
      style: {
        maxWidth: helperTextMaxWidth
      }
    }, helperText))), helperText && index.getHelperTextPlacement(value, size, hasIcons, iconQuantity) === index.helperTextPlacements.outer && /*#__PURE__*/React__default.default.createElement(Rating_styles.HelperText, {
      className: Rating_tokens.classes.helperTextOuter,
      style: {
        maxWidth: helperTextMaxWidth
      }
    }, helperText));
  });
};
var ratingConfig = {
  name: 'Rating',
  tag: 'div',
  layout: ratingRoot,
  base: Rating_styles.base,
  variations: {
    view: {
      css: base.base
    },
    size: {
      css: base$1.base
    }
  },
  defaults: {
    view: 'default',
    size: 'l'
  }
};

exports.ratingConfig = ratingConfig;
exports.ratingRoot = ratingRoot;
