import React, { forwardRef, useMemo } from "react";
import styled from "@emotion/styled";
import { component, mergeConfig } from "../../engines";
import { cx } from "../../utils";
import { indicatorConfig, indicatorTokens } from "../Indicator";
import { classes, tokens } from "./Avatar.tokens";
import { base, Wrapper, Image, StatusIcon, Text, ExtraContent, ExtraCounter, ExtraBadge } from "./Avatar.styles";
import { base as sizeCSS } from "./variations/_size/base";
import { base as focusedCSS } from "./variations/_focused/base";
import { base as shapeCSS } from "./variations/_shape/base";
import { extraPlacementMap, getInitialsForName } from "./utils";
const StatusLabelsDefault = {
    active: 'Активен',
    inactive: 'Неактивен'
};
const getAvatarContent = ({ customText, customBorderRadius, url, initials, name })=>{
    if (customText) {
        return /*#__PURE__*/ React.createElement(Text, null, customText);
    }
    if (url) {
        return /*#__PURE__*/ React.createElement(Image, {
            src: url,
            alt: name,
            borderRadius: customBorderRadius
        });
    }
    return /*#__PURE__*/ React.createElement(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 = mergeConfig(indicatorConfig);
const Indicator = component(mergedConfig);
const StyledIndicator = /*#__PURE__*/ styled(Indicator, {
    target: "e1lfw7a50",
    label: "StyledIndicator"
})(indicatorTokens.size, ":var(", tokens.statusIconSize, ");", indicatorTokens.color, ":", ({ status })=>status === 'active' ? `var(${tokens.statusOnlineColor})` : `var(${tokens.statusOfflineColor})`);
export const avatarRoot = (Root)=>{
    return /*#__PURE__*/ 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 = useMemo(()=>getInitialsForName(name), [
            name
        ]);
        const ariaLabel = getAriaLabel({
            ...props,
            statusLabels
        });
        const extraViewProp = type === 'badge' ? {
            badgeView
        } : {
            counterView
        };
        const borderRadius = avatarSize === 'fit' ? customBorderRadius : undefined;
        return /*#__PURE__*/ React.createElement(Root, {
            ref: ref,
            size: avatarSize,
            shape: shape,
            className: cx(classes.avatarItem, className),
            "aria-label": ariaLabel,
            focused: focused,
            ...extraViewProp,
            ...rest
        }, /*#__PURE__*/ React.createElement(Wrapper, {
            isScalable: isScalable,
            borderRadius: borderRadius
        }, getAvatarContent({
            customText,
            url,
            initials,
            name,
            customBorderRadius: borderRadius
        })), status && /*#__PURE__*/ React.createElement(StatusIcon, null, /*#__PURE__*/ React.createElement(StyledIndicator, {
            "aria-label": statusLabels[status],
            status: status
        })), hasExtra && avatarSize !== 'fit' && /*#__PURE__*/ React.createElement(ExtraContent, {
            className: extraPlacementMap(extraPlacement)
        }, type === 'counter' ? /*#__PURE__*/ React.createElement(ExtraCounter, {
            view: counterView,
            count: counterValue,
            maxCount: maxCount
        }) : avatarSize !== 's' && /*#__PURE__*/ React.createElement(ExtraBadge, {
            view: badgeView,
            text: text,
            customColor: customColor,
            customBackgroundColor: customBackgroundColor,
            pilled: pilled,
            maxWidth: "100%",
            ...contentLeft ? {
                contentLeft
            } : {
                contentRight
            }
        })));
    });
};
export const avatarConfig = {
    name: 'Avatar',
    tag: 'div',
    layout: avatarRoot,
    base,
    variations: {
        size: {
            css: sizeCSS
        },
        focused: {
            css: focusedCSS
        },
        shape: {
            css: shapeCSS
        }
    },
    defaults: {
        size: 'm',
        shape: 'circled'
    }
};
