/**
 * tdesign v0.32.0
 * (c) 2021 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var config = require('../config.js');
var utils_classnames = require('../utils/classnames.js');
var avatar_props = require('./props.js');
var utils_renderTnode = require('../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');

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 name = "".concat(config.prefix, "-avatar");
var _Avatar = Vue__default["default"].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
      } : {};
    },
    customCharaSize: function customCharaSize() {
      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 avater = this.$refs.avatar;
      var avaterChild = this.$refs.avatarChild;
      var avaterWidth = avater === null || avater === void 0 ? void 0 : avater.offsetWidth;
      var avaterChildWidth = avaterChild === null || avaterChild === void 0 ? void 0 : avaterChild.offsetWidth;

      if (this.gap * 2 < avaterWidth) {
        this.scale = avaterChildWidth > avaterWidth - this.gap * 2 ? "scale(".concat((avaterWidth - this.gap * 2) / avaterChildWidth, ")") : "scale(1)";
      }
    },
    isCustomSize: function isCustomSize() {
      return this.sizeValue && !utils_classnames["default"].SIZE[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 = ["".concat(name), utils_classnames["default"].SIZE[this.sizeValue], (_ref = {}, _defineProperty__default["default"](_ref, "".concat(name, "--circle"), shape === "circle"), _defineProperty__default["default"](_ref, "".concat(name, "--round"), shape === "round"), _defineProperty__default["default"](_ref, "".concat(name, "__icon"), !!isIconOnly), _ref)];
    content = h("span", {
      "ref": "avatarChild",
      "style": _objectSpread({}, this.customCharaSize)
    }, [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
