/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import { prefix } from '../config.js';
import ClASSNAMES from '../utils/classnames.js';
import props from './props.js';
import { renderContent, renderTNodeJSX } from '../utils/render-tnode.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/isObject';

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 name = "".concat(prefix, "-avatar");
var _Avatar = Vue.extend({
  name: "TAvatar",
  props: _objectSpread({}, props),
  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 && !ClASSNAMES.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 = renderContent(this, "default", "content");
    var icon = 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), ClASSNAMES.SIZE[this.sizeValue], (_ref = {}, _defineProperty(_ref, "".concat(name, "--circle"), shape === "circle"), _defineProperty(_ref, "".concat(name, "--round"), shape === "round"), _defineProperty(_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]);
  }
});

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