/**
 * Copyright IBM Corp. 2026
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { defaults } from "../types/options.js";
import english from "../l10n/default.js";
import { formats, revFormat, tokenRegex } from "./formatting.js";
//#region ../../node_modules/flatpickr/dist/esm/utils/dates.js
var createDateFormatter = function(_a) {
	var _b = _a.config, config = _b === void 0 ? defaults : _b, _c = _a.l10n, l10n = _c === void 0 ? english : _c, _d = _a.isMobile, isMobile = _d === void 0 ? false : _d;
	return function(dateObj, frmt, overrideLocale) {
		var locale = overrideLocale || l10n;
		if (config.formatDate !== void 0 && !isMobile) return config.formatDate(dateObj, frmt, locale);
		return frmt.split("").map(function(c, i, arr) {
			return formats[c] && arr[i - 1] !== "\\" ? formats[c](dateObj, locale, config) : c !== "\\" ? c : "";
		}).join("");
	};
};
var createDateParser = function(_a) {
	var _b = _a.config, config = _b === void 0 ? defaults : _b, _c = _a.l10n, l10n = _c === void 0 ? english : _c;
	return function(date, givenFormat, timeless, customLocale) {
		if (date !== 0 && !date) return void 0;
		var locale = customLocale || l10n;
		var parsedDate;
		var dateOrig = date;
		if (date instanceof Date) parsedDate = new Date(date.getTime());
		else if (typeof date !== "string" && date.toFixed !== void 0) parsedDate = new Date(date);
		else if (typeof date === "string") {
			var format = givenFormat || (config || defaults).dateFormat;
			var datestr = String(date).trim();
			if (datestr === "today") {
				parsedDate = /* @__PURE__ */ new Date();
				timeless = true;
			} else if (config && config.parseDate) parsedDate = config.parseDate(date, format);
			else if (/Z$/.test(datestr) || /GMT$/.test(datestr)) parsedDate = new Date(date);
			else {
				var matched = void 0, ops = [];
				for (var i = 0, matchIndex = 0, regexStr = ""; i < format.length; i++) {
					var token = format[i];
					var isBackSlash = token === "\\";
					var escaped = format[i - 1] === "\\" || isBackSlash;
					if (tokenRegex[token] && !escaped) {
						regexStr += tokenRegex[token];
						var match = new RegExp(regexStr).exec(date);
						if (match && (matched = true)) ops[token !== "Y" ? "push" : "unshift"]({
							fn: revFormat[token],
							val: match[++matchIndex]
						});
					} else if (!isBackSlash) regexStr += ".";
				}
				parsedDate = !config || !config.noCalendar ? new Date((/* @__PURE__ */ new Date()).getFullYear(), 0, 1, 0, 0, 0, 0) : new Date((/* @__PURE__ */ new Date()).setHours(0, 0, 0, 0));
				ops.forEach(function(_a) {
					var fn = _a.fn, val = _a.val;
					return parsedDate = fn(parsedDate, val, locale) || parsedDate;
				});
				parsedDate = matched ? parsedDate : void 0;
			}
		}
		if (!(parsedDate instanceof Date && !isNaN(parsedDate.getTime()))) {
			config.errorHandler(/* @__PURE__ */ new Error("Invalid date provided: " + dateOrig));
			return;
		}
		if (timeless === true) parsedDate.setHours(0, 0, 0, 0);
		return parsedDate;
	};
};
function compareDates(date1, date2, timeless) {
	if (timeless === void 0) timeless = true;
	if (timeless !== false) return new Date(date1.getTime()).setHours(0, 0, 0, 0) - new Date(date2.getTime()).setHours(0, 0, 0, 0);
	return date1.getTime() - date2.getTime();
}
var isBetween = function(ts, ts1, ts2) {
	return ts > Math.min(ts1, ts2) && ts < Math.max(ts1, ts2);
};
var calculateSecondsSinceMidnight = function(hours, minutes, seconds) {
	return hours * 3600 + minutes * 60 + seconds;
};
var parseSeconds = function(secondsSinceMidnight) {
	var hours = Math.floor(secondsSinceMidnight / 3600), minutes = (secondsSinceMidnight - hours * 3600) / 60;
	return [
		hours,
		minutes,
		secondsSinceMidnight - hours * 3600 - minutes * 60
	];
};
var duration = { DAY: 864e5 };
function getDefaultHours(config) {
	var hours = config.defaultHour;
	var minutes = config.defaultMinute;
	var seconds = config.defaultSeconds;
	if (config.minDate !== void 0) {
		var minHour = config.minDate.getHours();
		var minMinutes = config.minDate.getMinutes();
		var minSeconds = config.minDate.getSeconds();
		if (hours < minHour) hours = minHour;
		if (hours === minHour && minutes < minMinutes) minutes = minMinutes;
		if (hours === minHour && minutes === minMinutes && seconds < minSeconds) seconds = config.minDate.getSeconds();
	}
	if (config.maxDate !== void 0) {
		var maxHr = config.maxDate.getHours();
		var maxMinutes = config.maxDate.getMinutes();
		hours = Math.min(hours, maxHr);
		if (hours === maxHr) minutes = Math.min(maxMinutes, minutes);
		if (hours === maxHr && minutes === maxMinutes) seconds = config.maxDate.getSeconds();
	}
	return {
		hours,
		minutes,
		seconds
	};
}
//#endregion
export { calculateSecondsSinceMidnight, compareDates, createDateFormatter, createDateParser, duration, getDefaultHours, isBetween, parseSeconds };

//# sourceMappingURL=dates.js.map