"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = 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 _default = function _default(value) {
  var format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "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 = "";
    }
  });
  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.default = _default;