'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.checkForUses = exports.readAuthVocabs = exports.SET_AUTH_VOCAB_WORKFLOW_STATE = exports.AUTH_VOCABS_READ_REJECTED = exports.AUTH_VOCABS_READ_FULFILLED = exports.AUTH_VOCABS_READ_STARTED = undefined;

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _cspace = require('./cspace');

var _cspace2 = _interopRequireDefault(_cspace);

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

const AUTH_VOCABS_READ_STARTED = exports.AUTH_VOCABS_READ_STARTED = 'AUTH_VOCABS_READ_STARTED';
const AUTH_VOCABS_READ_FULFILLED = exports.AUTH_VOCABS_READ_FULFILLED = 'AUTH_VOCABS_READ_FULFILLED';
const AUTH_VOCABS_READ_REJECTED = exports.AUTH_VOCABS_READ_REJECTED = 'AUTH_VOCABS_READ_REJECTED';
const SET_AUTH_VOCAB_WORKFLOW_STATE = exports.SET_AUTH_VOCAB_WORKFLOW_STATE = 'SET_AUTH_VOCAB_WORKFLOW_STATE';

const readAuthVocabs = exports.readAuthVocabs = config => dispatch => {
  const recordTypes = (0, _get2.default)(config, 'recordTypes');

  if (!recordTypes) {
    // This is mostly to allow tests to run without having a config.

    return Promise.resolve();
  }

  dispatch({
    type: AUTH_VOCABS_READ_STARTED
  });

  const session = (0, _cspace2.default)();

  const readPromises = Object.values(config.recordTypes).filter(recordTypeConfig => recordTypeConfig.serviceConfig.serviceType === 'authority').map(recordTypeConfig => session.read(recordTypeConfig.serviceConfig.servicePath).catch(error =>
  // 403 Forbidden might happen if the user doesn't have any permissions on an authority.
  // Just swallow this, but reject other errors.

  (0, _get2.default)(error, ['response', 'status']) === 403 ? undefined : Promise.reject(error)));

  return Promise.all(readPromises).then(responses => dispatch({
    type: AUTH_VOCABS_READ_FULFILLED,
    payload: responses,
    meta: {
      config
    }
  })).catch(error => {
    dispatch({
      type: AUTH_VOCABS_READ_REJECTED,
      payload: error
    });

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

/**
 * Check if any uses exist for the given authority item. Resolves to true or false.
 */
const checkForUses = exports.checkForUses = (config, recordType, vocabulary, csid) => () => {
  const recordTypeConfig = (0, _get2.default)(config, ['recordTypes', recordType]);
  const vocabularyConfig = (0, _get2.default)(recordTypeConfig, ['vocabularies', vocabulary]);

  const recordServicePath = (0, _get2.default)(recordTypeConfig, ['serviceConfig', 'servicePath']);
  const vocabularyServicePath = (0, _get2.default)(vocabularyConfig, ['serviceConfig', 'servicePath']);
  const pathParts = [recordServicePath, vocabularyServicePath, 'items', csid, 'refObjs'];
  const path = pathParts.join('/');

  const requestConfig = {
    params: {
      wf_deleted: 'false',
      pgSz: '1'
    }
  };

  return (0, _cspace2.default)().read(path, requestConfig).then(response => {
    const totalItems = (0, _get2.default)(response, ['data', 'ns3:authority-ref-doc-list', 'totalItems']);

    return totalItems && parseInt(totalItems, 10) > 0;
  });
};