"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.normalizeInvocationDescriptor = exports.createInvocationData = void 0;

var _immutable = _interopRequireDefault(require("immutable"));

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

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const prepareParams = params => {
  if (params) {
    const paramPairs = [];
    Object.keys(params).forEach(key => {
      const value = params[key];

      if (Array.isArray(value)) {
        value.forEach(v => paramPairs.push({
          key,
          value: v
        }));
      } else {
        paramPairs.push({
          key,
          value
        });
      }
    });

    if (paramPairs.length > 0) {
      return {
        param: paramPairs
      };
    }
  }

  return undefined;
};

const createInvocationData = (config, invocationDescriptor, params) => {
  const {
    mode,
    outputMIME,
    recordType: invocationRecordType,
    csid: invocationCsid
  } = invocationDescriptor.toJS();
  const invocationContext = {
    mode,
    outputMIME,
    params: prepareParams(params),
    docType: (0, _get.default)(config, ['recordTypes', invocationRecordType, 'serviceConfig', 'objectName'])
  };

  if (mode === 'single') {
    invocationContext.singleCSID = invocationCsid;
  } else if (mode === 'list') {
    invocationContext.listCSIDs = {
      csid: invocationCsid
    };
  } else if (mode === 'group') {
    invocationContext.groupCSID = invocationCsid;
  }

  return {
    'ns2:invocationContext': _objectSpread({
      '@xmlns:ns2': 'http://collectionspace.org/services/common/invocable'
    }, invocationContext)
  };
};

exports.createInvocationData = createInvocationData;

const normalizeInvocationDescriptor = (invocationDescriptor, invocationMetadata) => {
  let normalizedInvocationDescriptor = invocationDescriptor;

  if (!normalizedInvocationDescriptor) {
    normalizedInvocationDescriptor = _immutable.default.Map();
  }

  const outputMIME = normalizedInvocationDescriptor.get('outputMIME');

  if (!outputMIME && invocationMetadata) {
    const defaultOutputMIME = invocationMetadata.getIn(['document', 'ns2:reports_common', 'outputMIME']) || 'application/pdf';
    normalizedInvocationDescriptor = normalizedInvocationDescriptor.set('outputMIME', defaultOutputMIME);
  }

  return normalizedInvocationDescriptor;
};

exports.normalizeInvocationDescriptor = normalizeInvocationDescriptor;