"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 avatarConfig () {
        return avatarConfig;
    },
    get avatarRoot () {
        return avatarRoot;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _styled = /*#__PURE__*/ _interop_require_default(require("@emotion/styled"));
const _engines = require("../../engines");
const _utils = require("../../utils");
const _Indicator = require("../Indicator");
const _Avatartokens = require("./Avatar.tokens");
const _Avatarstyles = require("./Avatar.styles");
const _base = require("./variations/_size/base");
const _base1 = require("./variations/_focused/base");
const _base2 = require("./variations/_shape/base");
const _utils1 = require("./utils");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
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 StatusLabelsDefault = {
    active: 'Активен',
    inactive: 'Неактивен'
};
const getAvatarContent = ({ customText, customBorderRadius, url, initials, name })=>{
    if (customText) {
        return /*#__PURE__*/ _react.default.createElement(_Avatarstyles.Text, null, customText);
    }
    if (url) {
        return /*#__PURE__*/ _react.default.createElement(_Avatarstyles.Image, {
            src: url,
            alt: name,
            borderRadius: customBorderRadius
        });
    }
    return /*#__PURE__*/ _react.default.createElement(_Avatarstyles.Text, null, initials);
};
const getAriaLabel = ({ url, name, status, 'aria-label': ariaLabelProp, statusLabels })=>{
    if (!url) {
        return;
    }
    // INFO: включаем aria-label чтобы озвучить что на изображении
    const ariaLabel = !ariaLabelProp || ariaLabelProp.trim() === '' ? name : ariaLabelProp;
    return status ? `${ariaLabel}. ${statusLabels[status]}` : ariaLabel;
};
const mergedConfig = (0, _engines.mergeConfig)(_Indicator.indicatorConfig);
const Indicator = (0, _engines.component)(mergedConfig);
const StyledIndicator = /*#__PURE__*/ (0, _styled.default)(Indicator, {
    target: "e1lfw7a50",
    label: "StyledIndicator"
})(_Indicator.indicatorTokens.size, ":var(", _Avatartokens.tokens.statusIconSize, ");", _Indicator.indicatorTokens.color, ":", ({ status })=>status === 'active' ? `var(${_Avatartokens.tokens.statusOnlineColor})` : `var(${_Avatartokens.tokens.statusOfflineColor})`);
const avatarRoot = (Root)=>{
    return /*#__PURE__*/ (0, _react.forwardRef)((props, ref)=>{
        const { size: avatarSize, name, url, customText, status, className, focused = true, isScalable, shape, statusLabels = StatusLabelsDefault, hasExtra, extraPlacement, type, counterView, count, maxCount, badgeView, text, customColor, customBackgroundColor, customBorderRadius, contentLeft, contentRight, pilled, ...rest } = props;
        const counterValue = count || 1;
        const initials = (0, _react.useMemo)(()=>(0, _utils1.getInitialsForName)(name), [
            name
        ]);
        const ariaLabel = getAriaLabel({
            ...props,
            statusLabels
        });
        const extraViewProp = type === 'badge' ? {
            badgeView
        } : {
            counterView
        };
        const borderRadius = avatarSize === 'fit' ? customBorderRadius : undefined;
        return /*#__PURE__*/ _react.default.createElement(Root, {
            ref: ref,
            size: avatarSize,
            shape: shape,
            className: (0, _utils.cx)(_Avatartokens.classes.avatarItem, className),
            "aria-label": ariaLabel,
            focused: focused,
            ...extraViewProp,
            ...rest
        }, /*#__PURE__*/ _react.default.createElement(_Avatarstyles.Wrapper, {
            isScalable: isScalable,
            borderRadius: borderRadius
        }, getAvatarContent({
            customText,
            url,
            initials,
            name,
            customBorderRadius: borderRadius
        })), status && /*#__PURE__*/ _react.default.createElement(_Avatarstyles.StatusIcon, null, /*#__PURE__*/ _react.default.createElement(StyledIndicator, {
            "aria-label": statusLabels[status],
            status: status
        })), hasExtra && avatarSize !== 'fit' && /*#__PURE__*/ _react.default.createElement(_Avatarstyles.ExtraContent, {
            className: (0, _utils1.extraPlacementMap)(extraPlacement)
        }, type === 'counter' ? /*#__PURE__*/ _react.default.createElement(_Avatarstyles.ExtraCounter, {
            view: counterView,
            count: counterValue,
            maxCount: maxCount
        }) : avatarSize !== 's' && /*#__PURE__*/ _react.default.createElement(_Avatarstyles.ExtraBadge, {
            view: badgeView,
            text: text,
            customColor: customColor,
            customBackgroundColor: customBackgroundColor,
            pilled: pilled,
            maxWidth: "100%",
            ...contentLeft ? {
                contentLeft
            } : {
                contentRight
            }
        })));
    });
};
const avatarConfig = {
    name: 'Avatar',
    tag: 'div',
    layout: avatarRoot,
    base: _Avatarstyles.base,
    variations: {
        size: {
            css: _base.base
        },
        focused: {
            css: _base1.base
        },
        shape: {
            css: _base2.base
        }
    },
    defaults: {
        size: 'm',
        shape: 'circled'
    }
};
