"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ratingConfig () {
        return ratingConfig;
    },
    get ratingRoot () {
        return ratingRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _utils = require("../../utils");
const _Ratingstyles = require("./Rating.styles");
const _base = require("./variations/_view/base");
const _base1 = require("./variations/_size/base");
const _utils1 = require("./utils");
const _Ratingtokens = require("./Rating.tokens");
const _getIcons = require("./utils/getIcons");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const ratingRoot = (Root)=>/*#__PURE__*/ (0, _react.forwardRef)(({ value, hasValue = true, precision = 1, decimalSeparator = '.', valuePlacement, iconSlot, iconSlotOutline, iconSlotHalf, hasIcons = true, iconQuantity = 5, helperText, helperTextStretching = 'filled', size, view, ...rest }, ref)=>{
        const innerValue = (0, _utils1.getInnerValue)(value, precision, iconQuantity);
        const displayValue = (0, _utils1.getDisplayValue)(innerValue, decimalSeparator);
        const contentRef = (0, _react.useRef)(null);
        const starsWrapperRef = (0, _react.useRef)(null);
        const [helperTextMaxWidth, setHelperTextMaxWidth] = (0, _react.useState)('unset');
        (0, _react.useEffect)(()=>{
            if (!hasValue && !hasIcons) {
                // eslint-disable-next-line no-console
                console.error("Rating should have at least one of 'hasValue' or 'hasIcons' props");
            }
        }, [
            hasValue,
            hasIcons
        ]);
        (0, _react.useEffect)(()=>{
            if (helperTextStretching === 'filled') {
                setHelperTextMaxWidth('unset');
                return;
            }
            const helpertextPlacement = (0, _utils1.getHelperTextPlacement)(value, size, hasIcons, iconQuantity);
            if (helpertextPlacement === _utils1.helperTextPlacements.inner) {
                if (!starsWrapperRef?.current) {
                    setHelperTextMaxWidth('unset');
                    return;
                }
                const starsWrapperWidth = starsWrapperRef.current.offsetWidth;
                setHelperTextMaxWidth(`${starsWrapperWidth}px`);
            } else if (helpertextPlacement === _utils1.helperTextPlacements.outer) {
                if (!contentRef?.current) {
                    setHelperTextMaxWidth('unset');
                    return;
                }
                const starsWrapperWidth = contentRef.current.offsetWidth;
                setHelperTextMaxWidth(`${starsWrapperWidth}px`);
            }
        }, [
            helperTextStretching,
            helperText,
            hasValue,
            hasIcons,
            precision,
            value,
            iconQuantity
        ]);
        // INFO: Если выключено отображение иконок и значения, нет смысла показывать только helperText
        if (!hasValue && !hasIcons) {
            return null;
        }
        return /*#__PURE__*/ _react.default.createElement(Root, {
            ref: ref,
            view: view,
            size: size,
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(_Ratingstyles.Content, {
            ref: contentRef,
            className: (0, _utils.cx)(valuePlacement === 'after' && _Ratingtokens.classes.valueAfter, iconQuantity === 1 && _Ratingtokens.classes.singleIconContent)
        }, hasValue && innerValue && /*#__PURE__*/ _react.default.createElement(_Ratingstyles.Score, null, displayValue), hasIcons && /*#__PURE__*/ _react.default.createElement(_Ratingstyles.Wrapper, null, /*#__PURE__*/ _react.default.createElement(_Ratingstyles.StarsWrapper, {
            ref: starsWrapperRef
        }, (0, _getIcons.getIcons)({
            innerValue,
            size,
            iconQuantity,
            iconSlot,
            iconSlotOutline,
            iconSlotHalf
        })), helperText && (0, _utils1.getHelperTextPlacement)(value, size, hasIcons, iconQuantity) === _utils1.helperTextPlacements.inner && /*#__PURE__*/ _react.default.createElement(_Ratingstyles.HelperText, {
            style: {
                maxWidth: helperTextMaxWidth
            }
        }, helperText))), helperText && (0, _utils1.getHelperTextPlacement)(value, size, hasIcons, iconQuantity) === _utils1.helperTextPlacements.outer && /*#__PURE__*/ _react.default.createElement(_Ratingstyles.HelperText, {
            className: _Ratingtokens.classes.helperTextOuter,
            style: {
                maxWidth: helperTextMaxWidth
            }
        }, helperText));
    });
const ratingConfig = {
    name: 'Rating',
    tag: 'div',
    layout: ratingRoot,
    base: _Ratingstyles.base,
    variations: {
        view: {
            css: _base.base
        },
        size: {
            css: _base1.base
        }
    },
    defaults: {
        view: 'default',
        size: 'l'
    }
};
