"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 _session = _interopRequireDefault(require("../helpers/session"));

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

const transformDate = (input = {}) => {
  const output = {};
  output.dateDisplayDate = input.displayDate || null;
  output.scalarValuesComputed = input.scalarValuesComputed || null;
  ['earliestSingle', 'latest'].forEach(type => {
    const inputDate = input[`${type}Date`] || {};
    ['year', 'month', 'day', 'era', 'certainty', 'qualifier', 'qualifierValue', 'qualifierUnit'].forEach(field => {
      output[`date${(0, _upperFirst.default)(type)}${(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, _session.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;