"use strict";

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

var _cspaceClient = _interopRequireDefault(require("cspace-client"));

var _get = _interopRequireDefault(require("lodash/get"));

var _login = require("./login");

var _prefs = require("./prefs");

var _account = require("./account");

var _authority = require("./authority");

var _notification = require("./notification");

var _LoginModal = _interopRequireDefault(require("../components/login/LoginModal"));

var _actionCodes = require("../constants/actionCodes");

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

let client;
let session;

const createSession = (username, password) => {
  if (typeof username === 'undefined' && typeof password === 'undefined') {
    return client.session();
  }

  return client.session({
    username,
    password
  });
};

exports.createSession = createSession;

const getSession = () => session;

const setSession = newSession => {
  session = newSession;
  return {
    type: _actionCodes.CSPACE_CONFIGURED,
    payload: session.config()
  };
};

exports.setSession = setSession;

const readSystemInfo = config => dispatch => getSession().read('systeminfo', {
  auth: false,
  params: {
    tid: (0, _get.default)(config, 'tenantId')
  }
}).then(response => dispatch({
  type: _actionCodes.SYSTEM_INFO_READ_FULFILLED,
  payload: response
})).catch(error => dispatch({
  type: _actionCodes.SYSTEM_INFO_READ_REJECTED,
  payload: error
}));

exports.readSystemInfo = readSystemInfo;

const configureCSpace = config => dispatch => {
  client = (0, _cspaceClient.default)({
    url: (0, _get.default)(config, 'serverUrl'),
    onError: error => {
      const status = (0, _get.default)(error, ['response', 'status']);

      if (status === 401) {
        const internalError = (0, _get.default)(error, ['response', 'data', 'error']);

        if (internalError === 'invalid_token') {
          // The stored token is no longer valid. Show the login modal.
          dispatch((0, _login.resetLogin)(session.config().username));
          dispatch((0, _notification.openModal)(_LoginModal.default.modalName));
        }
      }

      return Promise.reject(error);
    }
  });
  const newSession = createSession();
  dispatch(setSession(newSession));
  const {
    username
  } = newSession.config();

  if (!username) {
    return Promise.resolve();
  }

  return dispatch((0, _account.readAccountPerms)(config, username)).then(() => dispatch((0, _prefs.loadPrefs)(config, username))).then(() => dispatch((0, _authority.readAuthVocabs)(config))).catch(error => {
    // 401 is expected if the user's auth token has expired. The client onError handler will take
    // care of showing a notification, so the error can be swallowed here.
    const status = (0, _get.default)(error, ['response', 'status']);

    if (status === 401) {
      return Promise.resolve();
    }

    return Promise.reject(error);
  });
};

exports.configureCSpace = configureCSpace;
var _default = getSession;
exports.default = _default;