"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _vue = _interopRequireWildcard(require("vue"));

var _configProvider = require("../config-provider");

var _propsUtil = require("../_util/props-util");

var _vueTypes = _interopRequireDefault(require("../_util/vue-types"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var _default = {
  name: 'AAvatar',
  props: {
    prefixCls: {
      type: String,
      default: undefined
    },
    shape: {
      validator: function validator(val) {
        return ['circle', 'square'].includes(val);
      },
      default: 'circle'
    },
    size: {
      validator: function validator(val) {
        return typeof val === 'number' || ['small', 'large', 'default'].includes(val);
      },
      default: 'default'
    },
    src: String,

    /** Srcset of image avatar */
    srcSet: String,
    icon: _vueTypes.default.any,
    alt: String,
    loadError: Function
  },
  setup: function setup() {
    return {
      configProvider: (0, _vue.inject)('configProvider', _configProvider.ConfigConsumerProps)
    };
  },
  data: function data() {
    return {
      isImgExist: true,
      isMounted: false,
      scale: 1
    };
  },
  watch: {
    src: function src() {
      var _this = this;

      this.$nextTick(function () {
        _this.isImgExist = true;
        _this.scale = 1; // force uodate for position

        _this.$forceUpdate();
      });
    }
  },
  mounted: function mounted() {
    var _this2 = this;

    this.$nextTick(function () {
      _this2.setScale();

      _this2.isMounted = true;
    });
  },
  updated: function updated() {
    var _this3 = this;

    this.$nextTick(function () {
      _this3.setScale();
    });
  },
  methods: {
    setScale: function setScale() {
      if (!this.$refs.avatarChildren || !this.$refs.avatarNode) {
        return;
      }

      var childrenWidth = this.$refs.avatarChildren.offsetWidth; // offsetWidth avoid affecting be transform scale

      var nodeWidth = this.$refs.avatarNode.offsetWidth; // denominator is 0 is no meaning

      if (childrenWidth === 0 || nodeWidth === 0 || this.lastChildrenWidth === childrenWidth && this.lastNodeWidth === nodeWidth) {
        return;
      }

      this.lastChildrenWidth = childrenWidth;
      this.lastNodeWidth = nodeWidth; // add 4px gap for each side to get better performance

      this.scale = nodeWidth - 8 < childrenWidth ? (nodeWidth - 8) / childrenWidth : 1;
    },
    handleImgLoadError: function handleImgLoadError() {
      var loadError = this.$props.loadError;
      var errorFlag = loadError ? loadError() : undefined;

      if (errorFlag !== false) {
        this.isImgExist = false;
      }
    }
  },
  render: function render() {
    var _sizeCls, _objectSpread3;

    var _this$$props = this.$props,
        customizePrefixCls = _this$$props.prefixCls,
        shape = _this$$props.shape,
        size = _this$$props.size,
        src = _this$$props.src,
        alt = _this$$props.alt,
        srcSet = _this$$props.srcSet;
    var icon = (0, _propsUtil.getComponent)(this, 'icon');
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('avatar', customizePrefixCls);
    var _this$$data = this.$data,
        isImgExist = _this$$data.isImgExist,
        scale = _this$$data.scale,
        isMounted = _this$$data.isMounted;
    var sizeCls = (_sizeCls = {}, _defineProperty(_sizeCls, "".concat(prefixCls, "-lg"), size === 'large'), _defineProperty(_sizeCls, "".concat(prefixCls, "-sm"), size === 'small'), _sizeCls);

    var classString = _objectSpread(_objectSpread(_defineProperty({}, prefixCls, true), sizeCls), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, "".concat(prefixCls, "-").concat(shape), shape), _defineProperty(_objectSpread3, "".concat(prefixCls, "-image"), src && isImgExist), _defineProperty(_objectSpread3, "".concat(prefixCls, "-icon"), icon), _objectSpread3));

    var sizeStyle = typeof size === 'number' ? {
      width: "".concat(size, "px"),
      height: "".concat(size, "px"),
      lineHeight: "".concat(size, "px"),
      fontSize: icon ? "".concat(size / 2, "px") : '18px'
    } : {};
    var children = this.$slots.default && this.$slots.default();

    if (src && isImgExist) {
      children = _vue.createVNode("img", {
        "src": src,
        "srcSet": srcSet,
        "onError": this.handleImgLoadError,
        "alt": alt
      }, null);
    } else if (icon) {
      children = icon;
    } else {
      var childrenNode = this.$refs.avatarChildren;

      if (childrenNode || scale !== 1) {
        var transformString = "scale(".concat(scale, ") translateX(-50%)");
        var childrenStyle = {
          msTransform: transformString,
          WebkitTransform: transformString,
          transform: transformString
        };
        var sizeChildrenStyle = typeof size === 'number' ? {
          lineHeight: "".concat(size, "px")
        } : {};
        children = _vue.createVNode("span", {
          "class": "".concat(prefixCls, "-string"),
          "ref": "avatarChildren",
          "style": _objectSpread(_objectSpread({}, sizeChildrenStyle), childrenStyle)
        }, [children]);
      } else {
        var _childrenStyle = {};

        if (!isMounted) {
          _childrenStyle.opacity = 0;
        }

        children = _vue.createVNode("span", {
          "class": "".concat(prefixCls, "-string"),
          "ref": "avatarChildren",
          "style": {
            opacity: 0
          }
        }, [children]);
      }
    }

    return _vue.createVNode("span", {
      "ref": "avatarNode",
      "class": classString,
      "style": sizeStyle
    }, [children]);
  }
};
exports.default = _default;