"use strict";

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

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

var React = _interopRequireWildcard(require("react"));

var _avatar = require("../avatar");

var _button = require("../button");

var _overrides = require("../helpers/overrides");

var _chevronDown = _interopRequireDefault(require("../icon/chevron-down"));

var _chevronUp = _interopRequireDefault(require("../icon/chevron-up"));

var _list = require("../list");

var _menu = require("../menu");

var _popover = require("../popover");

var _styledComponents = require("./styled-components");

var _userProfileTile = _interopRequireDefault(require("./user-profile-tile"));

var _utils = require("./utils");

var _excluded = ["children"];

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var MENU_ITEM_WIDTH = '275px'; // eslint-disable-next-line react/display-name

var UserMenuListItem = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var item = props.item,
      _props$mapItemToNode = props.mapItemToNode,
      mapItemToNode = _props$mapItemToNode === void 0 ? _utils.defaultMapItemToNode : _props$mapItemToNode; // Replace with a user menu item renderer

  return /*#__PURE__*/React.createElement(_list.MenuAdapter, _extends({}, props, {
    ref: ref,
    artwork: item.icon || null,
    artworkSize: _list.ARTWORK_SIZES.LARGE
  }), /*#__PURE__*/React.createElement(_list.ListItemLabel, null, mapItemToNode(item)));
});

var svgStyleOverride = function svgStyleOverride(_ref) {
  var $theme = _ref.$theme;
  return {
    paddingLeft: $theme.sizing.scale200
  };
};

function UserMenuComponent(props) {
  // isOpen is used for displaying different arrow icons in open or closed state
  var _React$useState = React.useState(false),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      isOpen = _React$useState2[0],
      setIsOpen = _React$useState2[1];

  var _props$userItems = props.userItems,
      userItems = _props$userItems === void 0 ? [] : _props$userItems,
      username = props.username,
      userImgUrl = props.userImgUrl,
      _props$overrides = props.overrides,
      overrides = _props$overrides === void 0 ? {} : _props$overrides;

  var _getOverrides = (0, _overrides.getOverrides)(overrides.UserMenuProfileListItem, _styledComponents.StyledUserMenuProfileListItem),
      _getOverrides2 = _slicedToArray(_getOverrides, 2),
      UserMenuProfileListItem = _getOverrides2[0],
      userMenuProfileListItemProps = _getOverrides2[1];

  var _getOverrides3 = (0, _overrides.getOverrides)(overrides.UserMenuButton, _button.Button),
      _getOverrides4 = _slicedToArray(_getOverrides3, 2),
      UserMenuButton = _getOverrides4[0],
      userMenuButtonProps = _getOverrides4[1];

  userMenuButtonProps.overrides = (0, _overrides.mergeOverrides)({
    BaseButton: {
      component: _styledComponents.StyledUserMenuButton
    }
  }, userMenuButtonProps.overrides);

  var _getOverrides5 = (0, _overrides.getOverrides)(overrides.UserMenu, _menu.StatefulMenu),
      _getOverrides6 = _slicedToArray(_getOverrides5, 2),
      UserMenu = _getOverrides6[0],
      userMenuProps = _getOverrides6[1];

  userMenuProps.overrides = (0, _overrides.mergeOverrides)({
    List: {
      // eslint-disable-next-line react/display-name
      component: /*#__PURE__*/React.forwardRef(function (_ref2, ref) {
        var children = _ref2.children,
            restProps = _objectWithoutProperties(_ref2, _excluded);

        return /*#__PURE__*/React.createElement(_menu.StyledList, _extends({}, restProps, {
          ref: ref
        }), /*#__PURE__*/React.createElement(UserMenuProfileListItem, userMenuProfileListItemProps, /*#__PURE__*/React.createElement(_userProfileTile.default, {
          username: props.username,
          usernameSubtitle: props.usernameSubtitle,
          userImgUrl: props.userImgUrl,
          overrides: overrides
        })), children);
      }),
      style: {
        width: MENU_ITEM_WIDTH
      }
    },
    // eslint-disable-next-line react/display-name
    ListItem: /*#__PURE__*/React.forwardRef(function (listItemProps, ref) {
      return /*#__PURE__*/React.createElement(UserMenuListItem, _extends({
        ref: ref
      }, listItemProps, {
        mapItemToNode: props.mapItemToNode
      }));
    })
  }, userMenuProps.overrides);
  return /*#__PURE__*/React.createElement(_popover.StatefulPopover, {
    content: function content(_ref3) {
      var close = _ref3.close;
      return /*#__PURE__*/React.createElement(UserMenu, _extends({
        items: userItems,
        onItemSelect: function onItemSelect(_ref4) {
          var item = _ref4.item;
          props.onItemSelect(item);
          close();
        }
      }, userMenuProps));
    },
    autoFocus: false,
    dismissOnEsc: true,
    dismissOnClickOutside: true,
    onOpen: function onOpen() {
      return setIsOpen(true);
    },
    onClose: function onClose() {
      return setIsOpen(false);
    },
    placement: _popover.PLACEMENT.bottomRight,
    popperOptions: {
      modifiers: {
        flip: {
          enabled: false
        }
      }
    },
    triggerType: _popover.TRIGGER_TYPE.click
  }, /*#__PURE__*/React.createElement(UserMenuButton, userMenuButtonProps, /*#__PURE__*/React.createElement(_avatar.Avatar, {
    name: username || '',
    src: userImgUrl,
    size: '32px'
  }), isOpen ? /*#__PURE__*/React.createElement(_chevronUp.default, {
    size: 28,
    overrides: {
      Svg: {
        style: svgStyleOverride
      }
    }
  }) : /*#__PURE__*/React.createElement(_chevronDown.default, {
    size: 28,
    overrides: {
      Svg: {
        style: svgStyleOverride
      }
    }
  })));
}