/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-c89aafbb.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import { Image } from '../image/index.js';
import mixins from '../utils/mixins.js';
import props from './props.js';
import '@babel/runtime/helpers/typeof';
import '@vue/composition-api';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../_chunks/dep-be599ec8.js';
import 'dayjs';
import '../_chunks/dep-67ff4010.js';
import '../config-provider/type.js';
import '../utils/map-props.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import '../image/image.js';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/omit';
import 'lodash/isFunction';
import '@ldesign/icons-vue';
import '../_common/js/utils/observe.js';
import '../config-provider/useConfig.js';
import '../space/index.js';
import '../space/space.js';
import '../hooks/useConfig.js';
import '../space/props.js';
import '../space/style/css.js';
import '../space/type.js';
import '../image/props.js';
import '../image/style/css.js';
import '../image/type.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var classPrefixMixins = getClassPrefixMixins("avatar");
var _Avatar = mixins(Vue, classPrefixMixins).extend({
  name: "TAvatar",
  props: _objectSpread({}, props),
  data: function data() {
    return {
      isImgExist: true,
      gap: 4,
      scale: ""
    };
  },
  inject: {
    avatarGroup: {
      "default": void 0
    }
  },
  computed: {
    sizeValue: function sizeValue() {
      var _this$avatarGroup;
      return this.size || ((_this$avatarGroup = this.avatarGroup) === null || _this$avatarGroup === void 0 ? void 0 : _this$avatarGroup.size);
    },
    customAvatarSize: function customAvatarSize() {
      return this.isCustomSize() ? {
        "width": this.sizeValue,
        "height": this.sizeValue,
        "font-size": "".concat(Number.parseInt(this.sizeValue, 10) / 2, "px")
      } : {};
    },
    customImageSize: function customImageSize() {
      return this.isCustomSize() ? {
        height: this.sizeValue,
        width: this.sizeValue
      } : {};
    },
    customCharacterSize: function customCharacterSize() {
      return {
        transform: this.scale
      };
    }
  },
  mounted: function mounted() {
    var _this = this;
    this.$nextTick(function () {
      _this.setScaleParams();
    });
  },
  methods: {
    handleImgLoadError: function handleImgLoadError(context) {
      var onError = this.onError,
        hideOnLoadFailed = this.hideOnLoadFailed;
      this.isImgExist = !hideOnLoadFailed;
      this.$emit("error", context);
      onError && onError(context);
    },
    setScaleParams: function setScaleParams() {
      var avatar = this.$refs.avatar;
      var avatarChild = this.$refs.avatarChild;
      var avatarWidth = avatar === null || avatar === void 0 ? void 0 : avatar.offsetWidth;
      var avatarChildWidth = avatarChild === null || avatarChild === void 0 ? void 0 : avatarChild.offsetWidth;
      if (this.gap * 2 < avatarWidth) {
        this.scale = avatarChildWidth > avatarWidth - this.gap * 2 ? "scale(".concat((avatarWidth - this.gap * 2) / avatarChildWidth, ")") : "scale(1)";
      }
    },
    isCustomSize: function isCustomSize() {
      return this.sizeValue && !this.commonSizeClassName[this.sizeValue];
    }
  },
  updated: function updated() {
    var _this2 = this;
    this.$nextTick(function () {
      _this2.setScaleParams();
    });
  },
  render: function render() {
    var _ref;
    var h = arguments[0];
    var content = renderContent(this, "default", "content");
    var icon = renderTNodeJSX(this, "icon");
    var isIconOnly = icon && !content;
    var _this$$props = this.$props,
      shape = _this$$props.shape,
      image = _this$$props.image,
      alt = _this$$props.alt;
    var avatarClass = [this.componentName, this.commonSizeClassName[this.sizeValue], (_ref = {}, _defineProperty(_ref, "".concat(this.componentName, "--circle"), shape === "circle"), _defineProperty(_ref, "".concat(this.componentName, "--round"), shape === "round"), _defineProperty(_ref, "".concat(this.componentName, "__icon"), !!isIconOnly), _ref)];
    content = h("span", {
      "ref": "avatarChild",
      "style": _objectSpread({}, this.customCharacterSize)
    }, [content]);
    if (icon) content = [icon, !isIconOnly ? content : ""];
    if (image && this.isImgExist) {
      var _this$imageProps, _this$imageProps2;
      content = h(Image, helper([{
        "style": _objectSpread({}, this.customImageSize),
        "attrs": {
          "src": image,
          "alt": alt
        }
      }, {
        "props": this.imageProps
      }, {
        "attrs": {
          "error": !this.$scopedSlots.error && !((_this$imageProps = this.imageProps) !== null && _this$imageProps !== void 0 && _this$imageProps.error) ? " " : void 0,
          "loading": !this.$scopedSlots.loading && !((_this$imageProps2 = this.imageProps) !== null && _this$imageProps2 !== void 0 && _this$imageProps2.loading) ? " " : void 0
        },
        "scopedSlots": this.$scopedSlots
      }, {
        "on": {
          error: this.handleImgLoadError
        }
      }]));
    }
    return h("div", {
      "ref": "avatar",
      "class": avatarClass,
      "style": this.customAvatarSize
    }, [content]);
  }
});

export { _Avatar as default };
//# sourceMappingURL=avatar.js.map
