/**
 * amis-ui v6.14.0-beta.1
 * Copyright 2018-2025 fex
 */

'use strict';

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

var tslib = require('tslib');
var React = require('react');
var amisCore = require('amis-core');
var icons = require('./icons.js');

function _interopNamespace(e) {
    if (e && e.__esModule) return e;
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () { return e[k]; }
                });
            }
        });
    }
    n["default"] = e;
    return Object.freeze(n);
}

var React__namespace = /*#__PURE__*/_interopNamespace(React);

var __react_jsx__ = require('react');
var _J$X_ = (__react_jsx__["default"] || __react_jsx__).createElement;
(__react_jsx__["default"] || __react_jsx__).Fragment;
var prefix = 'Avatar--';
var childPrefix = prefix + 'text';
var Avatar = /** @class */ (function (_super) {
    tslib.__extends(Avatar, _super);
    function Avatar(props) {
        var _this = _super.call(this, props) || this;
        _this.state = {
            scale: 1,
            hasImg: true
        };
        _this.avatarChildrenRef = React__namespace.createRef();
        _this.avatarRef = React__namespace.createRef();
        _this.handleImageLoadError = _this.handleImageLoadError.bind(_this);
        return _this;
    }
    Avatar.prototype.componentDidMount = function () {
        this.setScaleByGap();
    };
    Avatar.prototype.componentDidUpdate = function (prevProps, prevState) {
        var _a = this.props, src = _a.src, gap = _a.gap, text = _a.text, children = _a.children;
        var hasImg = this.state.hasImg;
        if (prevProps.src !== src) {
            this.setState({
                hasImg: !!src
            });
        }
        if ((prevState.hasImg && !hasImg) ||
            prevProps.text !== text ||
            prevProps.children !== children ||
            prevProps.gap !== gap) {
            this.setScaleByGap();
        }
    };
    Avatar.prototype.handleImageLoadError = function (event) {
        var onError = this.props.onError;
        this.setState({
            hasImg: onError ? !onError(event) : false
        });
    };
    Avatar.prototype.setScaleByGap = function () {
        var _a = this.props.gap, gap = _a === void 0 ? 4 : _a;
        if (!this.avatarChildrenRef.current || !this.avatarRef.current) {
            return;
        }
        var childrenWidth = this.avatarChildrenRef.current.offsetWidth;
        var nodeWidth = this.avatarRef.current.offsetWidth;
        if (childrenWidth && nodeWidth) {
            if (gap * 2 < nodeWidth) {
                var diff = nodeWidth - gap * 2;
                this.setState({
                    scale: diff < childrenWidth ? diff / childrenWidth : 1
                });
            }
        }
    };
    Avatar.prototype.render = function () {
        var _a = this.props, _b = _a.style, style = _b === void 0 ? {} : _b, className = _a.className, shape = _a.shape, size = _a.size, src = _a.src, icon = _a.icon, alt = _a.alt, draggable = _a.draggable, crossOrigin = _a.crossOrigin, fit = _a.fit, text = _a.text, children = _a.children, cx = _a.classnames, onClick = _a.onClick, onMouseEnter = _a.onMouseEnter, onMouseLeave = _a.onMouseLeave;
        var _c = this.state, scale = _c.scale, hasImg = _c.hasImg;
        var isImgRender = React__namespace.isValidElement(src);
        var isIconRender = React__namespace.isValidElement(icon);
        var childrenRender;
        var sizeStyle = {};
        var sizeClass = '';
        if (typeof size === 'number') {
            sizeStyle = {
                height: size,
                width: size,
                lineHeight: size + 'px'
            };
        }
        else if (typeof size === 'string') {
            sizeClass =
                size === 'large'
                    ? "".concat(prefix, "lg")
                    : size === 'small'
                        ? "".concat(prefix, "sm")
                        : '';
        }
        var scaleX = "scale(".concat(scale, ") translateX(-50%)");
        var scaleStyle = {
            msTransform: scaleX,
            WebkitTransform: scaleX,
            transform: scaleX
        };
        if (typeof src === 'string' && hasImg) {
            var imgStyle = fit ? { objectFit: fit } : {};
            childrenRender = (_J$X_("img", { style: imgStyle, src: src, alt: alt, draggable: draggable, onError: this.handleImageLoadError, crossOrigin: crossOrigin }));
        }
        else if (isImgRender) {
            childrenRender = src;
        }
        else if (typeof text === 'string' || typeof text === 'number') {
            childrenRender = (_J$X_("span", { className: cx(childPrefix), ref: this.avatarChildrenRef, style: scaleStyle }, text));
        }
        else if (['string', 'object'].includes(typeof icon)) {
            childrenRender = _J$X_(icons.Icon, { cx: cx, icon: icon });
        }
        else if (isIconRender) {
            childrenRender = icon;
        }
        else {
            childrenRender = (_J$X_("span", { className: cx(childPrefix), ref: this.avatarChildrenRef, style: scaleStyle }, typeof children === 'function' ? children() : children));
        }
        return (_J$X_("span", { className: cx("Avatar", className, prefix + shape, sizeClass), style: tslib.__assign(tslib.__assign({}, sizeStyle), style), onClick: onClick, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, ref: this.avatarRef }, childrenRender));
    };
    Avatar.defaultProps = {
        shape: 'circle',
        size: 'default',
        fit: 'cover',
        gap: 4
    };
    return Avatar;
}(React__namespace.Component));
var Avatar$1 = amisCore.themeable(Avatar);

exports.Avatar = Avatar;
exports["default"] = Avatar$1;
