"use strict";

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

var _parseDob = _interopRequireDefault(require("parse-dob"));

var _dobToAge = _interopRequireDefault(require("dob-to-age"));

var _function = require("../utils/function");

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

const parseDobMemoized = (0, _function.singleArgMemoize)(dob => (0, _parseDob.default)(dob, 'en-US'));
const formatBirthDate = (0, _function.singleArgMemoize)(str => {
  const [year, month, date] = str.split('-');
  const d = new Date();
  const opts = {
    year: 'numeric'
  };
  d.setFullYear(Number(year));
  d.setHours(12);

  if (month) {
    d.setMonth(Number(month) - 1);
    opts.month = 'long';
  }

  if (date) {
    d.setDate(Number(date));
    opts.day = 'numeric';
  }

  return d.toLocaleDateString(undefined, opts);
});
exports.formatBirthDate = formatBirthDate;
const birthDateField = {
  label: 'Birth Date',
  name: 'birthDate',
  onInput: true,
  subLabel: item => {
    const parsed = parseDobMemoized(item.value || '');

    if (!parsed) {
      return "You can use MM/DD/YYYY or \"dec 23 83\". Interpreted result will appear here. ";
    }

    return "Interpreted as: ".concat(formatBirthDate(parsed), " (").concat((0, _dobToAge.default)(parsed), " years old).");
  },
  completenessTest: parseDobMemoized,
  test: val => {
    const parsed = parseDobMemoized(val);

    if (val && !parsed) {
      return 'Could not interpret birth date yet. ';
    }
  },
  tourVal: '12/01/1984',
  inputWidth: '180px',
  getFinalValue: val => parseDobMemoized(val)
};
exports.birthDateField = birthDateField;