'use strict';

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

var vue = require('vue');
require('../../../utils/index.cjs');
require('../../../vue-components/index.cjs');
require('../../../constants/index.cjs');
require('../../../hooks/index.cjs');
var useConfigInject = require('../../config-provider/src/hooks/useConfigInject.cjs');
var index = require('../style/index.cjs');
var AvatarContext = require('./AvatarContext.cjs');
var props = require('./props.cjs');
var index$1 = require('../../../hooks/use-breakpoint/index.cjs');
var eagerComputed = require('../../../utils/vue/eagerComputed.cjs');
var size = require('../../../constants/size.cjs');
var props$1 = require('../../../utils/vue/props.cjs');
var index$2 = require('../../../vue-components/vc-resize-observer/src/index.cjs');

var AAvatar = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AAvatar",
  inheritAttrs: false,
  props: props.avatarProps(),
  slots: Object,
  setup(props, {
    slots,
    attrs
  }) {
    const isImgExist = vue.shallowRef(true);
    const isMounted = vue.shallowRef(false);
    const scale = vue.shallowRef(1);
    const avatarChildrenRef = vue.shallowRef(null);
    const avatarNodeRef = vue.shallowRef(null);
    const {
      prefixCls
    } = useConfigInject.default("avatar", props);
    const [wrapSSR, hashId] = index.default(prefixCls);
    const avatarCtx = AvatarContext.useAvatarInjectContext();
    const size$1 = vue.computed(() => {
      return props.size === "default" ? avatarCtx.size : props.size;
    });
    const screens = index$1.useBreakpoint();
    const responsiveSize = eagerComputed.eagerComputed(() => {
      if (typeof props.size !== "object")
        return void 0;
      const currentBreakpoint = size.responsiveArray.find((screen) => screens.value[screen]);
      const currentSize = props.size[currentBreakpoint];
      return currentSize;
    });
    const responsiveSizeStyle = (hasIcon) => {
      if (responsiveSize.value) {
        return {
          width: `${responsiveSize.value}px`,
          height: `${responsiveSize.value}px`,
          lineHeight: `${responsiveSize.value}px`,
          fontSize: `${hasIcon ? responsiveSize.value / 2 : 18}px`
        };
      }
      return {};
    };
    const setScaleParam = () => {
      if (!avatarChildrenRef.value || !avatarNodeRef.value)
        return;
      const childrenWidth = avatarChildrenRef.value.offsetWidth;
      const nodeWidth = avatarNodeRef.value.offsetWidth;
      if (childrenWidth !== 0 && nodeWidth !== 0) {
        const {
          gap = 4
        } = props;
        if (gap * 2 < nodeWidth) {
          scale.value = nodeWidth - gap * 2 < childrenWidth ? (nodeWidth - gap * 2) / childrenWidth : 1;
        }
      }
    };
    const handleImgLoadError = () => {
      const {
        loadError
      } = props;
      const errorFlag = loadError == null ? void 0 : loadError();
      if (errorFlag !== false)
        isImgExist.value = false;
    };
    vue.watch(() => props.src, () => {
      vue.nextTick(() => {
        isImgExist.value = true;
        scale.value = 1;
      });
    });
    vue.watch(() => props.gap, () => {
      vue.nextTick(() => {
        setScaleParam();
      });
    });
    vue.onMounted(() => {
      vue.nextTick(() => {
        setScaleParam();
        isMounted.value = true;
      });
    });
    return () => {
      var _a, _b;
      const {
        shape,
        src,
        alt,
        srcset,
        draggable,
        crossOrigin
      } = props;
      const mergeShape = (_a = avatarCtx.shape) != null ? _a : shape;
      const icon = props$1.getPropsSlot(slots, props, "icon");
      const pre = prefixCls.value;
      const classString = {
        [`${attrs.class}`]: !!attrs.class,
        [pre]: true,
        [`${pre}-lg`]: size$1.value === "large",
        [`${pre}-sm`]: size$1.value === "small",
        [`${pre}-${mergeShape}`]: true,
        [`${pre}-image`]: src && isImgExist.value,
        [`${pre}-icon`]: icon,
        [hashId.value]: true
      };
      const sizeStyle = typeof size$1.value === "number" ? {
        width: `${size$1.value}px`,
        height: `${size$1.value}px`,
        lineHeight: `${size$1.value}px`,
        fontSize: icon ? `${size$1.value / 2}px` : "18px"
      } : {};
      const children = (_b = slots.default) == null ? void 0 : _b.call(slots);
      let childrenToRender;
      if (src && isImgExist.value) {
        childrenToRender = vue.createVNode("img", {
          "draggable": draggable,
          "src": src,
          "srcset": srcset,
          "onError": handleImgLoadError,
          "alt": alt,
          "crossorigin": crossOrigin
        }, null);
      } else if (icon) {
        childrenToRender = icon;
      } else if (isMounted.value || scale.value !== 1) {
        const transformString = `scale(${scale.value}) translateX(-50%)`;
        const childrenStyle = {
          msTransform: transformString,
          WebkitTransform: transformString,
          transform: transformString
        };
        const sizeChildrenStyle = typeof size$1.value === "number" ? {
          lineHeight: `${size$1.value}px`
        } : {};
        childrenToRender = vue.createVNode(index$2.default, {
          "onResize": setScaleParam
        }, {
          default: () => [vue.createVNode("span", {
            "class": `${pre}-string`,
            "ref": avatarChildrenRef,
            "style": {
              ...sizeChildrenStyle,
              ...childrenStyle
            }
          }, [children])]
        });
      } else {
        childrenToRender = vue.createVNode("span", {
          "class": `${pre}-string`,
          "ref": avatarChildrenRef,
          "style": {
            opacity: 0
          }
        }, [children]);
      }
      return wrapSSR(vue.createVNode("span", {
        ...attrs,
        "ref": avatarNodeRef,
        "class": classString,
        "style": [sizeStyle, responsiveSizeStyle(!!icon), attrs.style]
      }, [childrenToRender]));
    };
  }
});

exports.default = AAvatar;
//# sourceMappingURL=Avatar.cjs.map
