require('./Avatar_11r3oyw.css');
'use strict';

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

var _rollupPluginBabelHelpers = require('../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var react = require('@linaria/react');
var index = require('../../utils/index.js');
var Avatar_tokens = require('./Avatar.tokens.js');
var Avatar_styles = require('./Avatar.styles.js');
var base = require('./variations/_size/base.js');
var base$1 = require('./variations/_focused/base.js');
var base$2 = require('./variations/_shape/base.js');
var index$1 = require('./utils/index.js');
var getInitialsForName = require('./utils/getInitialsForName.js');
var common = require('../../engines/common.js');
var Indicator$1 = require('../Indicator/Indicator.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var _excluded = ["size", "name", "url", "customText", "status", "className", "focused", "isScalable", "shape", "statusLabels", "hasExtra", "extraPlacement", "type", "counterView", "count", "maxCount", "badgeView", "text", "customColor", "customBackgroundColor", "customBorderRadius", "contentLeft", "contentRight", "pilled"];
var StatusLabelsDefault = {
  active: 'Активен',
  inactive: 'Неактивен'
};
var getAvatarContent = function getAvatarContent(_ref) {
  var customText = _ref.customText,
    customBorderRadius = _ref.customBorderRadius,
    url = _ref.url,
    initials = _ref.initials,
    name = _ref.name;
  if (customText) {
    return /*#__PURE__*/React__default.default.createElement(Avatar_styles.Text, null, customText);
  }
  if (url) {
    return /*#__PURE__*/React__default.default.createElement(Avatar_styles.Image, {
      src: url,
      alt: name,
      borderRadius: customBorderRadius
    });
  }
  return /*#__PURE__*/React__default.default.createElement(Avatar_styles.Text, null, initials);
};
var getAriaLabel = function getAriaLabel(_ref2) {
  var url = _ref2.url,
    name = _ref2.name,
    status = _ref2.status,
    ariaLabelProp = _ref2['aria-label'],
    statusLabels = _ref2.statusLabels;
  if (!url) {
    return;
  }

  // INFO: включаем aria-label чтобы озвучить что на изображении
  var ariaLabel = !ariaLabelProp || ariaLabelProp.trim() === '' ? name : ariaLabelProp;
  return status ? "".concat(ariaLabel, ". ").concat(statusLabels[status]) : ariaLabel;
};
var mergedConfig = /*#__PURE__*/common.mergeConfig(Indicator$1.indicatorConfig);
var Indicator = /*#__PURE__*/common.component(mergedConfig);
var _exp = function _exp() {
  return Indicator;
};
var _exp5 = function _exp5() {
  return function (_ref3) {
    var status = _ref3.status;
    return status === 'active' ? "var(".concat(Avatar_tokens.tokens.statusOnlineColor, ")") : "var(".concat(Avatar_tokens.tokens.statusOfflineColor, ")");
  };
};
var StyledIndicator = /*#__PURE__*/react.styled(_exp())({
  name: "StyledIndicator",
  "class": "s1l3y6h4",
  propsAsIs: true,
  vars: {
    "s1l3y6h4-0": [/*#__PURE__*/_exp5()]
  }
});
var avatarRoot = function avatarRoot(Root) {
  return /*#__PURE__*/React.forwardRef(function (props, ref) {
    var avatarSize = props.size,
      name = props.name,
      url = props.url,
      customText = props.customText,
      status = props.status,
      className = props.className,
      _props$focused = props.focused,
      focused = _props$focused === void 0 ? true : _props$focused,
      isScalable = props.isScalable,
      shape = props.shape,
      _props$statusLabels = props.statusLabels,
      statusLabels = _props$statusLabels === void 0 ? StatusLabelsDefault : _props$statusLabels,
      hasExtra = props.hasExtra,
      extraPlacement = props.extraPlacement,
      type = props.type,
      counterView = props.counterView,
      count = props.count,
      maxCount = props.maxCount,
      badgeView = props.badgeView,
      text = props.text,
      customColor = props.customColor,
      customBackgroundColor = props.customBackgroundColor,
      customBorderRadius = props.customBorderRadius,
      contentLeft = props.contentLeft,
      contentRight = props.contentRight,
      pilled = props.pilled,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(props, _excluded);
    var counterValue = count || 1;
    var initials = React.useMemo(function () {
      return getInitialsForName.getInitialsForName(name);
    }, [name]);
    var ariaLabel = getAriaLabel(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, props), {}, {
      statusLabels: statusLabels
    }));
    var extraViewProp = type === 'badge' ? {
      badgeView: badgeView
    } : {
      counterView: counterView
    };
    var borderRadius = avatarSize === 'fit' ? customBorderRadius : undefined;
    return /*#__PURE__*/React__default.default.createElement(Root, _rollupPluginBabelHelpers.extends({
      ref: ref,
      size: avatarSize,
      shape: shape,
      className: index.cx(Avatar_tokens.classes.avatarItem, className),
      "aria-label": ariaLabel,
      focused: focused
    }, extraViewProp, rest), /*#__PURE__*/React__default.default.createElement(Avatar_styles.Wrapper, {
      isScalable: isScalable,
      borderRadius: borderRadius
    }, getAvatarContent({
      customText: customText,
      url: url,
      initials: initials,
      name: name,
      customBorderRadius: borderRadius
    })), status && /*#__PURE__*/React__default.default.createElement(Avatar_styles.StatusIcon, null, /*#__PURE__*/React__default.default.createElement(StyledIndicator, {
      "aria-label": statusLabels[status],
      status: status
    })), hasExtra && avatarSize !== 'fit' && /*#__PURE__*/React__default.default.createElement(Avatar_styles.ExtraContent, {
      className: index$1.extraPlacementMap(extraPlacement)
    }, type === 'counter' ? /*#__PURE__*/React__default.default.createElement(Avatar_styles.ExtraCounter, {
      view: counterView,
      count: counterValue,
      maxCount: maxCount
    }) : avatarSize !== 's' && /*#__PURE__*/React__default.default.createElement(Avatar_styles.ExtraBadge, _rollupPluginBabelHelpers.extends({
      view: badgeView,
      text: text,
      customColor: customColor,
      customBackgroundColor: customBackgroundColor,
      pilled: pilled,
      maxWidth: "100%"
    }, contentLeft ? {
      contentLeft: contentLeft
    } : {
      contentRight: contentRight
    }))));
  });
};
var avatarConfig = {
  name: 'Avatar',
  tag: 'div',
  layout: avatarRoot,
  base: Avatar_styles.base,
  variations: {
    size: {
      css: base.base
    },
    focused: {
      css: base$1.base
    },
    shape: {
      css: base$2.base
    }
  },
  defaults: {
    size: 'm',
    shape: 'circled'
  }
};

exports.avatarConfig = avatarConfig;
exports.avatarRoot = avatarRoot;
