"use strict";var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;exports.__esModule = true;exports.InternalDateTransformer = void 0;var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));var _constants = require("./constants");












var _InternalDateValidator = require("./InternalDateValidator");








var _types = require("./types");var _InternalDateTransformer;var

InternalDateTransformer = exports.InternalDateTransformer = /*#__PURE__*/function () {function InternalDateTransformer() {}InternalDateTransformer.

























  dateToFragments = function dateToFragments(
  components,
  settings)
  {if (settings === void 0) {settings = {};}
    var _settings =





      settings,_settings$order = _settings.order,order = _settings$order === void 0 ? _constants.defaultDateComponentsOrder : _settings$order,_settings$separator = _settings.separator,separator = _settings$separator === void 0 ? _constants.defaultDateComponentsSeparator : _settings$separator,_settings$withSeparat = _settings.withSeparator,withSeparator = _settings$withSeparat === void 0 ? false : _settings$withSeparat,_settings$withPad = _settings.withPad,withPad = _settings$withPad === void 0 ? false : _settings$withPad,pad = _settings.pad;
    var year = {
      type: _types.InternalDateComponentType.Year,
      value: components.year,
      length: _constants.LENGTH_YEAR
    };
    var month = {
      type: _types.InternalDateComponentType.Month,
      value: components.month,
      length: _constants.LENGTH_MONTH
    };
    var date = {
      type: _types.InternalDateComponentType.Date,
      value: components.date,
      length: _constants.LENGTH_DATE
    };

    var fragments = [];
    if (order === _types.InternalDateOrder.YMD) {
      fragments.push(year, month, date);
    } else if (order === _types.InternalDateOrder.MDY) {
      fragments.push(month, date, year);
    } else if (order === _types.InternalDateOrder.DMY) {
      fragments.push(date, month, year);
    }

    if (withPad) {
      year.valueWithPad = InternalDateTransformer.padYear(year.value, pad);
      month.valueWithPad = InternalDateTransformer.padMonth(month.value, pad);
      date.valueWithPad = InternalDateTransformer.padDate(date.value, pad);
    }

    year.isValid = _InternalDateValidator.InternalDateValidator.testParseToNumber(year.value);
    month.isValid = _InternalDateValidator.InternalDateValidator.testParseToNumber(month.value);
    date.isValid = _InternalDateValidator.InternalDateValidator.testParseToNumber(date.value);

    if (withSeparator) {
      var separatorFragment = {
        type: _types.InternalDateComponentType.Separator,
        value: separator,
        length: _constants.LENGTH_SEPARATOR
      };
      fragments.splice(1, 0, separatorFragment);
      fragments.splice(3, 0, separatorFragment);
    }

    return fragments;
  };InternalDateTransformer.

  parseValueToDate = function parseValueToDate(
  value,
  order)
  {if (order === void 0) {order = _constants.defaultDateComponentsOrder;}
    if (!value) {
      return null;
    }

    var getRe = function getRe() {
      if (order === _types.InternalDateOrder.MDY) {
        return _constants.RE_ORDER_MDY;
      } else if (order === _types.InternalDateOrder.DMY) {
        return _constants.RE_ORDER_DMY;
      }

      return _constants.RE_ORDER_YMD;
    };

    var re = getRe();
    if (!re.test(value)) {
      return null;
    }
    var match = re.exec(value);
    var dateComponents = (0, _extends2.default)({}, _constants.emptyDateComponents);

    if (match) {
      var matchFinished = match.
      slice(1).
      map(function (item) {return _InternalDateValidator.InternalDateValidator.testParseToNumber(item) ? Number(item) : null;});
      if (order === _types.InternalDateOrder.YMD) {
        dateComponents.year = matchFinished[0];dateComponents.month = matchFinished[1];dateComponents.date = matchFinished[2];
      } else if (order === _types.InternalDateOrder.MDY) {
        dateComponents.year = matchFinished[2];dateComponents.month = matchFinished[0];dateComponents.date = matchFinished[1];
      } else if (order === _types.InternalDateOrder.DMY) {
        dateComponents.year = matchFinished[2];dateComponents.month = matchFinished[1];dateComponents.date = matchFinished[0];
      }
    }
    return dateComponents;
  };InternalDateTransformer.

  dateComponentsStringToNumber = function dateComponentsStringToNumber(
  componentsRaw)
  {
    if (componentsRaw === null) {
      return { year: 0, month: 0, date: 0 };
    }
    var year = componentsRaw.year,month = componentsRaw.month,date = componentsRaw.date;
    return { year: Number(year), month: Number(month), date: Number(date) };
  };InternalDateTransformer.

  dateToInternalString = function dateToInternalString(components) {
    return InternalDateTransformer.dateToFragments(components, {
      withPad: true,
      withSeparator: false,
      order: _types.InternalDateOrder.DMY
    }).
    filter(function (_ref) {var value = _ref.value;return value !== null;}).
    map(function (_ref2) {var valueWithPad = _ref2.valueWithPad;return valueWithPad;}).
    join(_types.InternalDateSeparator.Dot);
  };InternalDateTransformer.

  dateToHumanString = function dateToHumanString(_ref3) {var date = _ref3.date,month = _ref3.month,year = _ref3.year;
    return InternalDateTransformer.dateToInternalString({
      date: date,
      month: month !== null ? Number(month) + 1 : null,
      year: year
    });
  };InternalDateTransformer.

  componentsToNativeDate = function componentsToNativeDate(componentsRaw) {
    var _InternalDateTransfor = InternalDateTransformer.dateComponentsStringToNumber(componentsRaw),year = _InternalDateTransfor.year,month = _InternalDateTransfor.month,date = _InternalDateTransfor.date;
    return new Date(Date.UTC(year, month - 1, date));
  };InternalDateTransformer.

  componentsToA11YFormat = function componentsToA11YFormat(components, canonicalLocale) {
    var nativeDate = InternalDateTransformer.componentsToNativeDate(components);
    return nativeDate.toLocaleDateString(canonicalLocale, {
      weekday: 'long',
      year: 'numeric',
      month: 'long',
      day: 'numeric'
    });
  };return InternalDateTransformer;}();_InternalDateTransformer = InternalDateTransformer;InternalDateTransformer.padStart = function (value, length, pad) {if (pad === void 0) {pad = _constants.CHAR_PAD;}return String(value || '').padStart(length, pad);};InternalDateTransformer.padYear = function (year, pad) {return _InternalDateTransformer.padStart(year, _constants.LENGTH_YEAR, pad);};InternalDateTransformer.padMonth = function (month, pad) {return _InternalDateTransformer.padStart(month, _constants.LENGTH_MONTH, pad);};InternalDateTransformer.padDate = function (date, pad) {return _InternalDateTransformer.padStart(date, _constants.LENGTH_DATE, pad);};InternalDateTransformer.padDateComponent = function (type, value, pad) {if (type === _types.InternalDateComponentType.Year) {return _InternalDateTransformer.padYear(value, pad);} else if (type === _types.InternalDateComponentType.Month) {return _InternalDateTransformer.padMonth(value, pad);} else if (type === _types.InternalDateComponentType.Date) {return _InternalDateTransformer.padDate(value, pad);}return '';};