'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _class, _temp2;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _Tray = require('@instructure/ui-overlays/lib/components/Tray');

var _Tray2 = _interopRequireDefault(_Tray);

var _Portal = require('@instructure/ui-portal/lib/components/Portal');

var _Portal2 = _interopRequireDefault(_Portal);

var _Dialog = require('@instructure/ui-a11y/lib/components/Dialog');

var _Dialog2 = _interopRequireDefault(_Dialog);

var _Mask = require('@instructure/ui-overlays/lib/components/Mask');

var _Mask2 = _interopRequireDefault(_Mask);

var _Button = require('@instructure/ui-buttons/lib/components/Button');

var _Button2 = _interopRequireDefault(_Button);

var _Alert = require('@instructure/ui-alerts/lib/components/Alert');

var _Alert2 = _interopRequireDefault(_Alert);

var _Text = require('@instructure/ui-elements/lib/components/Text');

var _Text2 = _interopRequireDefault(_Text);

var _formatMessage = require('../../../../common/format-message');

var _formatMessage2 = _interopRequireDefault(_formatMessage);

var _Banks = require('../../../../banks/components/pages/Banks');

var _Banks2 = _interopRequireDefault(_Banks);

var _BankEntry = require('../../../../banks/components/pages/BankEntry');

var _BankEntry2 = _interopRequireDefault(_BankEntry);

var _Bank = require('../../../../banks/components/pages/Bank');

var _Bank2 = _interopRequireDefault(_Bank);

var _constants = require('../../../../constants');

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

var _ref2 = _react2.default.createElement('br', null);

var _ref3 = _react2.default.createElement('br', null);

var BanksTray = (_temp2 = _class = function (_Component) {
  (0, _inherits3.default)(BanksTray, _Component);

  function BanksTray() {
    var _ref;

    var _temp, _this, _ret;

    (0, _classCallCheck3.default)(this, BanksTray);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = BanksTray.__proto__ || Object.getPrototypeOf(BanksTray)).call.apply(_ref, [this].concat(args))), _this), _this.closeTray = function () {
      if (_this.props.trayTrigger) {
        _this.props.trayTrigger.focus();
      }
      _this.props.setUI(_constants.BUILD_TRAY_OPEN, false);
      _this.props.setUI(_constants.BUILD_TRAY_POSITION, null);
      _this.props.setUI(_constants.BUILD_TRAY_STIMULUS_ID, null);
    }, _this.clearActiveBank = function () {
      _this.props.newActiveBank(void 0);
      _this.props.newActiveBankEntry(void 0);
    }, _this.navigateToBank = function (bankId) {
      _this.props.newActiveBankEntry(void 0);
      _this.props.newActiveBank(bankId);
    }, _this.handleCloseAlert = function () {
      _this.props.setUI(_constants.TRAY_ALERT_OPEN, false);
      _this.clearActiveBank();
      _this.props.clear(_constants.BANKS_BANK_PAGINATION);
    }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  }

  (0, _createClass3.default)(BanksTray, [{
    key: 'renderTrayContent',
    value: function renderTrayContent() {
      var _props = this.props,
          activeBankEntryId = _props.activeBankEntryId,
          activeBankId = _props.activeBankId;

      if (activeBankId) {
        if (activeBankEntryId) {
          return _react2.default.createElement(_BankEntry2.default, {
            isCompact: true,
            closeTray: this.closeTray,
            bankId: activeBankId,
            bankEntryId: activeBankEntryId,
            navigateToBanks: this.clearActiveBank,
            navigateToBank: this.navigateToBank
          });
        } else {
          return _react2.default.createElement(_Bank2.default, {
            isCompact: true,
            closeTray: this.closeTray,
            bankId: activeBankId,
            navigateToBanks: this.clearActiveBank,
            navigateToBankEntry: this.props.newActiveBankEntry
          });
        }
      } else {
        return _react2.default.createElement(_Banks2.default, {
          isCompact: true,
          closeTray: this.closeTray,
          navigateToBank: this.props.newActiveBank,
          getUserInfo: this.props.getUserInfo,
          getShareableUsers: this.props.getShareableUsers
        });
      }
    }
  }, {
    key: 'renderTrayAlert',
    value: function renderTrayAlert() {
      var alertTitle = (0, _formatMessage2.default)('Resource no longer available.');
      var alertBody = (0, _formatMessage2.default)('This item or your permissions to it may have been removed by another user.');
      return _react2.default.createElement(
        _Portal2.default,
        { open: this.props.trayAlertOpen },
        _react2.default.createElement(
          _Mask2.default,
          { fullscreen: true, placement: 'top' },
          _react2.default.createElement(
            _Dialog2.default,
            {
              open: this.props.trayAlertOpen,
              shouldReturnFocus: true,
              shouldContainFocus: true,
              onDismiss: this.handleCloseAlert,
              label: alertTitle + ' ' + alertBody
            },
            _react2.default.createElement(
              _Alert2.default,
              { variant: 'error', margin: 'small 0' },
              _react2.default.createElement(
                _Text2.default,
                { weight: 'bold' },
                alertTitle
              ),
              _ref2,
              _react2.default.createElement(
                _Text2.default,
                null,
                alertBody
              ),
              _ref3,
              _react2.default.createElement(
                _Button2.default,
                {
                  margin: 'small 0 0 0',
                  variant: 'primary',
                  size: 'small',
                  onClick: this.handleCloseAlert
                },
                (0, _formatMessage2.default)('Ok')
              )
            )
          )
        )
      );
    }
  }, {
    key: 'render',
    value: function render() {
      return _react2.default.createElement(
        'div',
        null,
        _react2.default.createElement(
          _Tray2.default,
          {
            label: (0, _formatMessage2.default)('Browse item banks'),
            open: this.props.trayOpen,
            placement: 'end',
            size: 'medium',
            shouldContainFocus: true,
            'data-automation': 'sdk-banks-tray'
          },
          this.renderTrayContent()
        ),
        this.renderTrayAlert()
      );
    }
  }]);
  BanksTray.displayName = 'BanksTray'
  ;
  return BanksTray;
}(_react.Component), _class.propTypes = {
  activeBankId: _propTypes2.default.string,
  activeBankEntryId: _propTypes2.default.string,
  clear: _propTypes2.default.func.isRequired,
  getUserInfo: _propTypes2.default.func,
  getShareableUsers: _propTypes2.default.func,
  trayOpen: _propTypes2.default.bool.isRequired,
  trayAlertOpen: _propTypes2.default.bool.isRequired,
  newActiveBank: _propTypes2.default.func.isRequired,
  setUI: _propTypes2.default.func.isRequired,
  trayTrigger: _propTypes2.default.instanceOf(Element),
  newActiveBankEntry: _propTypes2.default.func.isRequired
}, _class.defaultProps = {
  activeBankId: null,
  activeBankEntryId: null,
  getShareableUsers: null,
  getUserInfo: null,
  trayTrigger: null
}, _temp2);
exports.default = BanksTray;