"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;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _styledcomponents = /*#__PURE__*/ _interop_require_default(require("styled-components"));
var _engines = require("../../engines");
var _utils = require("../../utils");
var _Indicator = require("../Indicator");
var _Avatartokens = require("./Avatar.tokens");
var _Avatarstyles = require("./Avatar.styles");
var _base = require("./variations/_size/base");
var _base1 = require("./variations/_focused/base");
var _base2 = require("./variations/_shape/base");
var _utils1 = require("./utils");
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
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;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function ownKeys(object, enumerableOnly) {
    var keys = Object.keys(object);
    if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);
        if (enumerableOnly) {
            symbols = symbols.filter(function(sym) {
                return Object.getOwnPropertyDescriptor(object, sym).enumerable;
            });
        }
        keys.push.apply(keys, symbols);
    }
    return keys;
}
function _object_spread_props(target, source) {
    source = source != null ? source : {};
    if (Object.getOwnPropertyDescriptors) {
        Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
    } else {
        ownKeys(Object(source)).forEach(function(key) {
            Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
var StatusLabelsDefault = {
    active: 'Активен',
    inactive: 'Неактивен'
};
var getAvatarContent = function getAvatarContent(param) {
    var customText = param.customText, customBorderRadius = param.customBorderRadius, url = param.url, initials = param.initials, name = param.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);
};
var getAriaLabel = function getAriaLabel(param) {
    var url = param.url, name = param.name, status = param.status, ariaLabelProp = param['aria-label'], statusLabels = param.statusLabels;
    if (!url) {
        return;
    }
    // INFO: включаем aria-label чтобы озвучить что на изображении
    var ariaLabel = !ariaLabelProp || ariaLabelProp.trim() === '' ? name : ariaLabelProp;
    return status ? "".concat(ariaLabel, ". ").concat(statusLabels[status]) : ariaLabel;
};
var mergedConfig = (0, _engines.mergeConfig)(_Indicator.indicatorConfig);
var Indicator = (0, _engines.component)(mergedConfig);
var StyledIndicator = (0, _styledcomponents.default)(Indicator).withConfig({
    displayName: "Avatar__StyledIndicator",
    componentId: "sc-8add70e7-0"
})([
    "",
    ":var(",
    ");",
    ":",
    ""
], _Indicator.indicatorTokens.size, _Avatartokens.tokens.statusIconSize, _Indicator.indicatorTokens.color, function(param) {
    var status = param.status;
    return status === 'active' ? "var(".concat(_Avatartokens.tokens.statusOnlineColor, ")") : "var(".concat(_Avatartokens.tokens.statusOfflineColor, ")");
});
var avatarRoot = function avatarRoot(Root) {
    return /*#__PURE__*/ (0, _react.forwardRef)(function(props, ref) {
        var avatarSize = props.size, name = props.name, url = props.url, customText = props.customText, status = props.status, className = props.className, _props_focused = props.focused, focused = _props_focused === void 0 ? true : _props_focused, isScalable = props.isScalable, shape = props.shape, _props_statusLabels = props.statusLabels, statusLabels = _props_statusLabels === void 0 ? StatusLabelsDefault : _props_statusLabels, hasExtra = props.hasExtra, extraPlacement = props.extraPlacement, type = props.type, counterView = props.counterView, count = props.count, maxCount = props.maxCount, badgeView = props.badgeView, text = props.text, customColor = props.customColor, customBackgroundColor = props.customBackgroundColor, customBorderRadius = props.customBorderRadius, contentLeft = props.contentLeft, contentRight = props.contentRight, pilled = props.pilled, rest = _object_without_properties(props, [
            "size",
            "name",
            "url",
            "customText",
            "status",
            "className",
            "focused",
            "isScalable",
            "shape",
            "statusLabels",
            "hasExtra",
            "extraPlacement",
            "type",
            "counterView",
            "count",
            "maxCount",
            "badgeView",
            "text",
            "customColor",
            "customBackgroundColor",
            "customBorderRadius",
            "contentLeft",
            "contentRight",
            "pilled"
        ]);
        var counterValue = count || 1;
        var initials = (0, _react.useMemo)(function() {
            return (0, _utils1.getInitialsForName)(name);
        }, [
            name
        ]);
        var ariaLabel = getAriaLabel(_object_spread_props(_object_spread({}, props), {
            statusLabels: statusLabels
        }));
        var extraViewProp = type === 'badge' ? {
            badgeView: badgeView
        } : {
            counterView: counterView
        };
        var borderRadius = avatarSize === 'fit' ? customBorderRadius : undefined;
        return /*#__PURE__*/ _react.default.createElement(Root, _object_spread({
            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: customText,
            url: url,
            initials: initials,
            name: 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, _object_spread({
            view: badgeView,
            text: text,
            customColor: customColor,
            customBackgroundColor: customBackgroundColor,
            pilled: pilled,
            maxWidth: "100%"
        }, contentLeft ? {
            contentLeft: contentLeft
        } : {
            contentRight: contentRight
        }))));
    });
};
var 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'
    }
};
