import { defineComponent, ref, toRefs, openBlock, createElementBlock, normalizeStyle, unref, Fragment, normalizeClass, createCommentVNode } from "vue";
import "../../../adapter-vue.mjs";
import { isUniFrameWork } from "../../../utils/env.mjs";
const _hoisted_1 = ["src"];
const _hoisted_2 = ["src"];
const _hoisted_3 = ["src"];
const _sfc_main = /* @__PURE__ */ defineComponent({
  __name: "index",
  props: {
    url: { default: "https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png" },
    size: { default: "36px" },
    borderRadius: { default: "5px" },
    useSkeletonAnimation: { type: Boolean, default: false }
  },
  emits: ["onLoad", "onError"],
  setup(__props, { emit: __emit }) {
    const defaultAvatarUrl = ref("https://web.sdk.qcloud.com/component/TUIKit/assets/avatar_21.png");
    const emits = __emit;
    const props = __props;
    const {
      size: avatarSize,
      url: avatarImageUrl,
      borderRadius: avatarBorderRadius,
      useSkeletonAnimation: useAvatarSkeletonAnimation
    } = toRefs(props);
    let reloadAvatarTime = 0;
    const isImgLoaded = ref(false);
    const loadErrorInUniapp = ref(false);
    function avatarLoadSuccess(e) {
      isImgLoaded.value = true;
      emits("onLoad", e);
    }
    function avatarLoadFailed(e) {
      reloadAvatarTime += 1;
      if (reloadAvatarTime > 3) {
        return;
      }
      if (isUniFrameWork) {
        loadErrorInUniapp.value = true;
      } else {
        e.currentTarget.src = defaultAvatarUrl.value;
      }
      emits("onError", e);
    }
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: "avatar-container",
        style: normalizeStyle({
          width: unref(avatarSize),
          height: unref(avatarSize),
          borderRadius: unref(avatarBorderRadius)
        })
      }, [
        unref(isUniFrameWork) ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [
          !unref(loadErrorInUniapp) ? (openBlock(), createElementBlock("image", {
            key: 0,
            class: "avatar-image",
            src: unref(avatarImageUrl) || unref(defaultAvatarUrl),
            onLoad: avatarLoadSuccess,
            onError: avatarLoadFailed
          }, null, 40, _hoisted_1)) : (openBlock(), createElementBlock("image", {
            key: 1,
            class: "avatar-image",
            src: unref(defaultAvatarUrl),
            onLoad: avatarLoadSuccess,
            onError: avatarLoadFailed
          }, null, 40, _hoisted_2))
        ], 64)) : (openBlock(), createElementBlock("img", {
          key: 1,
          class: "avatar-image",
          src: unref(avatarImageUrl) || unref(defaultAvatarUrl),
          onLoad: avatarLoadSuccess,
          onError: avatarLoadFailed
        }, null, 40, _hoisted_3)),
        unref(useAvatarSkeletonAnimation) && !unref(isImgLoaded) ? (openBlock(), createElementBlock("div", {
          key: 2,
          class: normalizeClass({
            placeholder: true,
            hidden: unref(isImgLoaded),
            "skeleton-animation": unref(useAvatarSkeletonAnimation)
          })
        }, null, 2)) : createCommentVNode("", true)
      ], 4);
    };
  }
});
export {
  _sfc_main as default
};
