import React__default, { forwardRef, useContext, useRef, useEffect, useImperativeHandle } from "react";
import cs from "../_util/classNames.js";
import { ConfigContext } from "../ConfigProvider/index.js";
import AvatarContext from "./context.js";
import { isNumber } from "../_util/is.js";
import { jsxs, jsx } from "../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js";
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 = forwardRef(function(props, ref) {
  var _a;
  var _b = useContext(ConfigContext), getPrefixCls = _b.getPrefixCls, componentConfig = _b.componentConfig, rtl = _b.rtl;
  var prefixCls = getPrefixCls("avatar");
  var contextProps = useContext(AvatarContext);
  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 = useRef(null);
  var avatarRef = useRef(null);
  useEffect(function() {
    if (autoFixFontSize) {
      autoFixFontSizeHandler();
    }
  }, [size, children]);
  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 = cs(prefixCls, prefixCls + "-" + shape, (_a = {}, _a[prefixCls + "-with-trigger-icon"] = triggerIcon, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var isImage = children && React__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__ */ jsxs("div", {
    ...__assign({
      ref: avatarRef,
      onClick
    }, rest, {
      style: __assign({
        width: size,
        height: size,
        fontSize: isNumber(size) ? size / 2 : ""
      }, style),
      className: classNames
    }),
    children: [isImage ? /* @__PURE__ */ jsx("span", {
      className: prefixCls + "-image",
      children
    }) : null, !isImage && /* @__PURE__ */ jsx("span", {
      ref: textRef,
      className: prefixCls + "-text",
      children
    }), triggerIcon && /* @__PURE__ */ jsx("div", {
      className: prefixCls + "-trigger-icon-" + triggerType,
      style: _triggerIconStyle,
      children: triggerIcon
    })]
  });
});
Avatar.displayName = "Avatar";
const Avatar$1 = Avatar;
export {
  Avatar$1 as default
};
//# sourceMappingURL=avatar.js.map
