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

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var vue = require('vue');
var hooks_useConfig = require('../hooks/useConfig.js');
var hooks_tnode = require('../hooks/tnode.js');
var image_index = require('../image/index.js');
var avatar_props = require('./props.js');
require('../config-provider/useConfig.js');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
require('../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../_chunks/dep-f4db2590.js');
require('dayjs');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/isObject');
require('../utils/withInstall.js');
require('../image/image.js');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/omit');
require('@ldesign/icons-vue-next');
require('../_common/js/utils/observe.js');
require('../space/index.js');
require('../space/space.js');
require('lodash/isNumber');
require('../hooks/slot.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);

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 _Avatar = vue.defineComponent({
  name: "TAvatar",
  props: avatar_props["default"],
  setup: function setup(props2) {
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("avatar");
    var renderContent = hooks_tnode.useContent();
    var renderTNodeJSX = hooks_tnode.useTNodeJSX();
    var _useCommonClassName = hooks_useConfig.useCommonClassName(),
      SIZE = _useCommonClassName.SIZE;
    var avatarGroup = vue.inject("avatarGroup", void 0);
    var avatar = vue.ref(null);
    var avatarChild = vue.ref(null);
    var isImgExist = vue.ref(true);
    var gap = vue.ref(4);
    var scale = vue.ref("");
    var sizeValue = vue.computed(function () {
      return props2.size || (avatarGroup === null || avatarGroup === void 0 ? void 0 : avatarGroup.size);
    });
    var isCustomSize = vue.computed(function () {
      return sizeValue.value && !SIZE.value[sizeValue.value];
    });
    var customAvatarSize = vue.computed(function () {
      return isCustomSize.value ? {
        "width": sizeValue.value,
        "height": sizeValue.value,
        "font-size": "".concat(Number.parseInt(sizeValue.value, 10) / 2, "px")
      } : {};
    });
    var customImageSize = vue.computed(function () {
      return isCustomSize.value ? {
        height: sizeValue.value,
        width: sizeValue.value
      } : {};
    });
    var customCharacterSize = vue.computed(function () {
      return {
        transform: scale.value
      };
    });
    var handleImgLoadError = function handleImgLoadError(_ref) {
      var _props2$onError;
      var e = _ref.e;
      var hideOnLoadFailed = props2.hideOnLoadFailed;
      isImgExist.value = !hideOnLoadFailed;
      (_props2$onError = props2.onError) === null || _props2$onError === void 0 ? void 0 : _props2$onError.call(props2, {
        e: e
      });
    };
    var setScaleParams = function setScaleParams() {
      var $avatar = avatar.value;
      var $avatarChild = avatarChild.value;
      var avatarWidth = $avatar === null || $avatar === void 0 ? void 0 : $avatar.offsetWidth;
      var avatarChildWidth = $avatarChild === null || $avatarChild === void 0 ? void 0 : $avatarChild.offsetWidth;
      if (gap.value * 2 < avatarWidth) {
        scale.value = avatarChildWidth > avatarWidth - gap.value * 2 ? "scale(".concat((avatarWidth - gap.value * 2) / avatarChildWidth, ")") : "scale(1)";
      }
    };
    vue.onMounted(function () {
      vue.nextTick(function () {
        setScaleParams();
      });
    });
    vue.onUpdated(function () {
      vue.nextTick(function () {
        setScaleParams();
      });
    });
    return function () {
      var _ref2;
      var content = renderContent("default", "content");
      var icon = renderTNodeJSX("icon");
      var isIconOnly = icon && !content;
      var shape = props2.shape,
        image = props2.image,
        alt = props2.alt;
      var avatarClass = ["".concat(COMPONENT_NAME.value), SIZE.value[sizeValue.value], (_ref2 = {}, _defineProperty__default["default"](_ref2, "".concat(COMPONENT_NAME.value, "--circle"), shape === "circle"), _defineProperty__default["default"](_ref2, "".concat(COMPONENT_NAME.value, "--round"), shape === "round"), _defineProperty__default["default"](_ref2, "".concat(COMPONENT_NAME.value, "__icon"), !!isIconOnly), _ref2)];
      content = vue.createVNode("span", {
        "ref": avatarChild,
        "style": _objectSpread({}, customCharacterSize.value)
      }, [content]);
      if (icon) content = [icon, !isIconOnly ? content : ""];
      if (image && isImgExist.value) {
        content = vue.createVNode(image_index.Image, vue.mergeProps({
          "style": _objectSpread({}, customImageSize.value),
          "src": image,
          "alt": alt,
          "onError": handleImgLoadError
        }, props2.imageProps), null);
      }
      return vue.createVNode("div", {
        "ref": avatar,
        "class": avatarClass,
        "style": _objectSpread({}, customAvatarSize.value)
      }, [content]);
    };
  }
});

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