'use strict';

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

var vue = require('vue');
var index$2 = require('../_util/props-util/index.js');
var index = require('../_util/vue-types/index.js');
require('../hooks/index.js');
var responsiveObserve = require('../_util/responsiveObserve.js');
var index$3 = require('../vc-resize-observer/index.js');
var eagerComputed = require('../_util/eagerComputed.js');
var SizeContext = require('./SizeContext.js');
var index$1 = require('./style/index.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');
var useBreakpoint = require('../hooks/useBreakpoint.js');

const avatarProps = () => ({
  prefixCls: String,
  shape: {
    type: String,
    default: "circle"
  },
  size: {
    type: [Number, String, Object],
    default: () => "default"
  },
  src: String,
  /** Srcset of image avatar */
  srcset: String,
  icon: index.default.any,
  alt: String,
  gap: Number,
  draggable: {
    type: Boolean,
    default: void 0
  },
  crossOrigin: String,
  loadError: {
    type: Function
  }
});
const Avatar = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AAvatar",
  inheritAttrs: false,
  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$1.default(prefixCls);
    const groupSize = SizeContext.useInjectSize();
    const size = vue.computed(() => {
      return props.size === "default" ? groupSize.value : props.size;
    });
    const screens = useBreakpoint.useBreakpoint();
    const responsiveSize = eagerComputed.default(() => {
      if (typeof props.size !== "object")
        return void 0;
      const currentBreakpoint = responsiveObserve.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;
      const {
        shape,
        src,
        alt,
        srcset,
        draggable,
        crossOrigin
      } = props;
      const icon = index$2.getPropsSlot(slots, props, "icon");
      const pre = prefixCls.value;
      const classString = {
        [`${attrs.class}`]: !!attrs.class,
        [pre]: true,
        [`${pre}-lg`]: size.value === "large",
        [`${pre}-sm`]: size.value === "small",
        [`${pre}-${shape}`]: shape,
        [`${pre}-image`]: src && isImgExist.value,
        [`${pre}-icon`]: icon,
        [hashId.value]: true
      };
      const sizeStyle = typeof size.value === "number" ? {
        width: `${size.value}px`,
        height: `${size.value}px`,
        lineHeight: `${size.value}px`,
        fontSize: icon ? `${size.value / 2}px` : "18px"
      } : {};
      const children = (_a = slots.default) == null ? void 0 : _a.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.value === "number" ? {
          lineHeight: `${size.value}px`
        } : {};
        childrenToRender = vue.createVNode(index$3.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", vue.mergeProps(attrs, {
        "ref": avatarNodeRef,
        "class": classString,
        "style": [sizeStyle, responsiveSizeStyle(!!icon), attrs.style]
      }), [childrenToRender]));
    };
  }
});

exports.avatarProps = avatarProps;
exports.default = Avatar;
//# sourceMappingURL=Avatar.js.map
