"use strict";

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

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

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

var _upperFirst = _interopRequireDefault(require("lodash/upperFirst"));

var _cspace = _interopRequireDefault(require("./cspace"));

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

const transformDate = function transformDate() {
  let input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  const output = {};
  output.dateDisplayDate = input.displayDate || null;
  output.scalarValuesComputed = input.scalarValuesComputed || null;
  ['earliestSingle', 'latest'].forEach(type => {
    const inputDate = input["".concat(type, "Date")] || {};
    ['year', 'month', 'day', 'era', 'certainty', 'qualifier', 'qualifierValue', 'qualifierUnit'].forEach(field => {
      output["date".concat((0, _upperFirst.default)(type)).concat((0, _upperFirst.default)(field))] = inputDate[field] || null;
    });
  });
  return _immutable.default.fromJS(output);
};

const parseDisplayDate = displayDate => () => {
  if (!displayDate) {
    return Promise.resolve({
      value: transformDate()
    });
  }

  const requestConfig = {
    params: {
      dateToParse: displayDate
    }
  };
  return (0, _cspace.default)().read('structureddates', requestConfig).then(response => ({
    value: transformDate((0, _get.default)(response, ['data', 'ns2:structureddate_common']))
  })).catch(() => ({
    isError: true
  }));
};

exports.parseDisplayDate = parseDisplayDate;
var _default = {};
exports.default = _default;