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

'use strict';

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

var helper = require('../_chunks/dep-c3e1ffac.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var utils_renderTnode = require('../utils/render-tnode.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var image_index = require('../image/index.js');
var utils_mixins = require('../utils/mixins.js');
var avatar_props = require('./props.js');
require('@babel/runtime/helpers/typeof');
require('@vue/composition-api');
require('lodash/camelCase');
require('lodash/kebabCase');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../_chunks/dep-7c884501.js');
require('dayjs');
require('../utils/map-props.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../image/image.js');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/omit');
require('lodash/isFunction');
require('@ldesign/icons-vue');
require('../_common/js/utils/observe.js');
require('../config-provider/useConfig.js');
require('../space/index.js');
require('../space/space.js');
require('../hooks/useConfig.js');
require('../space/props.js');
require('../image/props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

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__default["default"](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 = configProvider_configReceiver.getClassPrefixMixins("avatar");
var _Avatar = utils_mixins["default"](Vue__default["default"], classPrefixMixins).extend({
  name: "TAvatar",
  props: _objectSpread({}, avatar_props["default"]),
  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 = utils_renderTnode.renderContent(this, "default", "content");
    var icon = utils_renderTnode.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__default["default"](_ref, "".concat(this.componentName, "--circle"), shape === "circle"), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--round"), shape === "round"), _defineProperty__default["default"](_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_index.Image, helper.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]);
  }
});

exports["default"] = _Avatar;
//# sourceMappingURL=avatar.js.map
