/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var avatar_props = require('./props.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../utils/mixins.js');
require('@babel/runtime/helpers/typeof');
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');

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,
      sizeValue: "",
      scale: ""
    };
  },
  inject: {
    avatarGroup: {
      "default": void 0
    }
  },
  computed: {
    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;

    var avatarGroup = this.avatarGroup;
    this.sizeValue = this.size || (avatarGroup === null || avatarGroup === void 0 ? void 0 : avatarGroup.size);
    this.$nextTick(function () {
      _this.setScaleParams();
    });
  },
  methods: {
    handleImgLoadError: function handleImgLoadError() {
      var _this$$props = this.$props,
          onError = _this$$props.onError,
          hideOnLoadFailed = _this$$props.hideOnLoadFailed;
      this.isImgExist = !hideOnLoadFailed;
      onError && onError();
      this.$emit("error");
    },
    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$$props2 = this.$props,
        shape = _this$$props2.shape,
        image = _this$$props2.image,
        alt = _this$$props2.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) {
      content = h("img", {
        "style": _objectSpread({}, this.customImageSize),
        "attrs": {
          "src": image,
          "alt": alt
        },
        "on": {
          "error": this.handleImgLoadError
        }
      });
    }

    return h("div", {
      "ref": "avatar",
      "class": avatarClass,
      "style": _objectSpread({}, this.customAvatarSize)
    }, [content]);
  }
});

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