import { checkLastUpdateTime } from "./checkLastUpdateTime.mjs";
import { formatTimeDiff } from "./formatTimeDiff.mjs";

//#region ../@intlayer/cli/dist/esm/utils/checkFileModifiedRange.mjs
/**
* Threshold that helps us differentiate between a numeric *timestamp* (ms from epoch)
* and a numeric *duration* (ms ago).
* 50 years expressed in milliseconds is far greater than any reasonable
* "relative" duration we expect the helper to receive (e.g. a couple of years).
*/
const TIMESTAMP_THRESHOLD_MS = 50 * 365 * 24 * 60 * 60 * 1e3;
/**
* Normalises the input date representation into a pair:
*   1. `relativeTime` – a Date instance whose epoch-time equals the duration
*      between `now` and the absolute date.
*   2. `absoluteTime` – the concrete point in time represented as a Date.
*
* Rules for interpreting the input:
*   • Date  => treated as an absolute time.
*   • string => parsed via the Date constructor => absolute time.
*   • number:
*       – if the value is larger than the TIMESTAMP_THRESHOLD_MS we assume it
*         is a unix timestamp (absolute time).
*       – otherwise we treat it as a *relative* duration expressed in
*         milliseconds.
*/
const normaliseInputDate = (date, now = /* @__PURE__ */ new Date()) => {
	if (date instanceof Date) return {
		absoluteTime: date,
		relativeTime: new Date(now.getTime() - date.getTime())
	};
	if (typeof date === "number") {
		if (date > TIMESTAMP_THRESHOLD_MS) {
			const absoluteTime = new Date(date);
			return {
				absoluteTime,
				relativeTime: new Date(now.getTime() - absoluteTime.getTime())
			};
		}
		const relativeMs = date;
		return {
			relativeTime: new Date(relativeMs),
			absoluteTime: new Date(now.getTime() - relativeMs)
		};
	}
	if (typeof date === "string") {
		const absoluteTime = new Date(date);
		if (Number.isNaN(absoluteTime.getTime())) throw new Error(`Invalid date string provided: ${date}`);
		return {
			absoluteTime,
			relativeTime: new Date(now.getTime() - absoluteTime.getTime())
		};
	}
	throw new Error(`Unsupported date format: ${date}`);
};
const checkFileModifiedRange = (filePath, options) => {
	const fileLastUpdateTime = checkLastUpdateTime(filePath);
	const { skipIfModifiedBefore, skipIfModifiedAfter } = options;
	const now = /* @__PURE__ */ new Date();
	const minDate = skipIfModifiedBefore ? normaliseInputDate(skipIfModifiedBefore, now).absoluteTime : void 0;
	const maxDate = skipIfModifiedAfter ? normaliseInputDate(skipIfModifiedAfter, now).absoluteTime : void 0;
	let shouldSkip = false;
	if (minDate instanceof Date && maxDate instanceof Date) shouldSkip = fileLastUpdateTime >= minDate && fileLastUpdateTime <= maxDate;
	else if (minDate instanceof Date) shouldSkip = fileLastUpdateTime >= minDate;
	else if (maxDate) shouldSkip = fileLastUpdateTime >= maxDate;
	if (shouldSkip) {
		const referenceDate = minDate && maxDate ? new Date(Math.abs(maxDate.getTime() - minDate.getTime())) : minDate ?? maxDate;
		return {
			isSkipped: true,
			message: `Skipping file because it has been modified within the last ${formatTimeDiff(new Date(now.getTime() - referenceDate.getTime()))} - ${filePath}`
		};
	}
	return {
		isSkipped: false,
		message: `File ${filePath} can be processed - ${filePath}`
	};
};

//#endregion
export { checkFileModifiedRange };
//# sourceMappingURL=checkFileModifiedRange.mjs.map