"use strict";
const React = require("react");
const classNames = require("../_util/classNames.js");
const index = require("../ConfigProvider/index.js");
const context = require("./context.js");
const is = require("../_util/is.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var defaultProps = {
  shape: "circle",
  autoFixFontSize: true,
  triggerType: "button"
};
var Avatar = React.forwardRef(function(props, ref) {
  var _a;
  var _b = React.useContext(index.ConfigContext), getPrefixCls = _b.getPrefixCls, componentConfig = _b.componentConfig, rtl = _b.rtl;
  var prefixCls = getPrefixCls("avatar");
  var contextProps = React.useContext(context);
  var mergedProps = __assign(__assign(__assign(__assign({}, defaultProps), componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.Avatar), contextProps), props);
  var className = mergedProps.className, shape = mergedProps.shape, size = mergedProps.size, children = mergedProps.children, autoFixFontSize = mergedProps.autoFixFontSize, triggerIcon = mergedProps.triggerIcon, triggerIconStyle = mergedProps.triggerIconStyle, triggerType = mergedProps.triggerType, onClick = mergedProps.onClick, rest = __rest(mergedProps, ["className", "shape", "size", "children", "autoFixFontSize", "triggerIcon", "triggerIconStyle", "triggerType", "onClick"]);
  var style = __assign(__assign({}, contextProps.style), props.style);
  var textRef = React.useRef(null);
  var avatarRef = React.useRef(null);
  React.useEffect(function() {
    if (autoFixFontSize) {
      autoFixFontSizeHandler();
    }
  }, [size, children]);
  React.useImperativeHandle(ref, function() {
    return avatarRef.current;
  });
  function autoFixFontSizeHandler() {
    if (textRef.current) {
      var textWidth = textRef.current.clientWidth;
      var size_1 = props.size || avatarRef.current.offsetWidth;
      var scale = size_1 / (textWidth + 8);
      if (size_1 && scale < 1) {
        textRef.current.style.transform = "scale(" + scale + ") translateX(-50%)";
      }
    }
  }
  var classNames$1 = classNames(prefixCls, prefixCls + "-" + shape, (_a = {}, _a[prefixCls + "-with-trigger-icon"] = triggerIcon, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var isImage = children && React__default.default.isValidElement(children) && (children.type === "img" || children.type === "picture");
  var _triggerIconStyle = triggerIconStyle || {};
  if (triggerType === "button" && (!triggerIconStyle || triggerIconStyle && !triggerIconStyle.color) && style && style.backgroundColor) {
    _triggerIconStyle.color = style.backgroundColor;
  }
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
    ...__assign({
      ref: avatarRef,
      onClick
    }, rest, {
      style: __assign({
        width: size,
        height: size,
        fontSize: is.isNumber(size) ? size / 2 : ""
      }, style),
      className: classNames$1
    }),
    children: [isImage ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
      className: prefixCls + "-image",
      children
    }) : null, !isImage && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
      ref: textRef,
      className: prefixCls + "-text",
      children
    }), triggerIcon && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-trigger-icon-" + triggerType,
      style: _triggerIconStyle,
      children: triggerIcon
    })]
  });
});
Avatar.displayName = "Avatar";
const Avatar$1 = Avatar;
module.exports = Avatar$1;
//# sourceMappingURL=avatar.js.map
