"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.mergeInputDate = exports.getBlocks = exports.formatInputDate = void 0;

require("core-js/modules/es.regexp.exec.js");

require("core-js/modules/es.string.split.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/web.dom-collections.for-each.js");

require("core-js/modules/es.array.includes.js");

require("core-js/modules/es.string.replace.js");

require("core-js/modules/es.array.slice.js");

var getBlocks = function getBlocks() {
  var format = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "YYYY-MM-DD";
  var blocks = [];
  var datePatterns = [];
  var delimiters = [];
  var formatTags = ["Y", "M", "D", "H", "m", "s", "y", "d", "h"];
  var formatArr = format.split("");
  var number = 0;
  var formatItemStr = "";
  formatArr.forEach(function (key, index) {
    if (formatTags.includes(key)) {
      number++;
      formatItemStr = formatItemStr + key;

      if (index + 1 === formatArr.length) {
        blocks.push(number);
        number = 0;
        datePatterns.push(formatItemStr);
        formatItemStr = "";
      }
    } else {
      blocks.push(number);
      number = 0;
      delimiters.push(key);
      datePatterns.push(formatItemStr);
      formatItemStr = "";
    }
  });
  return {
    blocks: blocks,
    datePatterns: datePatterns,
    delimiters: delimiters
  };
};

exports.getBlocks = getBlocks;

var formatInputDate = function formatInputDate(value, blocksData) {
  var blocks = blocksData.blocks,
      datePatterns = blocksData.datePatterns,
      delimiters = blocksData.delimiters;
  var result = "";
  value = value.replace(/[^\d]/g, "");
  blocks.forEach(function (length, index) {
    if (value.length > 0) {
      var sub = value.slice(0, length);
      var rest = value.slice(length);
      var datePattern = datePatterns[index];
      var delimiter = "";

      if (sub.length === length) {
        delimiter = delimiters[index] ? delimiters[index] : "";
      }

      if (datePattern == "DD") {
        if (sub === "00") {
          sub = "01";
        } else if (parseInt(sub, 10) > 31) {
          sub = "31";
        }
      } else if (datePattern == "MM") {
        if (sub === "00") {
          sub = "01";
        } else if (parseInt(sub, 10) > 12) {
          sub = "12";
        }
      } else if (datePattern == "HH") {
        if (parseInt(sub, 10) > 23) {
          sub = "23";
        }
      } else if (datePattern == "h") {
        if (parseInt(sub, 10) > 12) {
          sub = "12";
        }
      } else if (["mm", "ss", "m", "s"].includes(datePattern)) {
        if (parseInt(sub, 10) > 59) {
          sub = "59";
        }
      }

      result += sub + delimiter;
      value = rest;
    }
  });
  return result;
};

exports.formatInputDate = formatInputDate;

var mergeInputDate = function mergeInputDate(date1, date2, format) {
  if (date1 === date2) {
    return date1;
  }

  var _getBlocks = getBlocks(format),
      blocks = _getBlocks.blocks,
      delimiters = _getBlocks.delimiters;

  var result = "";
  var value1 = date1.replace(/[^\d]/g, "");
  var value2 = date2.replace(/[^\d]/g, "");
  blocks.forEach(function (length, index) {
    var date1Sub = value1.slice(0, length);
    value1 = value1.slice(length);
    var date2Sub = value2.slice(0, length);
    value2 = value2.slice(length);
    var val = "";
    var delimiter = delimiters[index] ? delimiters[index] : "";

    if (date1Sub.length === length) {
      val = date1Sub;
    } else {
      val = date2Sub;
    }

    if (val) {
      result += val + delimiter;
    }
  });
  return result;
};

exports.mergeInputDate = mergeInputDate;