import { prepareIntlayer } from "./prepareIntlayer.mjs";
import { handleAdditionalContentDeclarationFile } from "./handleAdditionalContentDeclarationFile.mjs";
import { handleContentDeclarationFileChange } from "./handleContentDeclarationFileChange.mjs";
import { handleUnlinkedContentDeclarationFile } from "./handleUnlinkedContentDeclarationFile.mjs";
import { basename } from "node:path";
import { getAppLogger, getConfiguration } from "@intlayer/config";
import { watch } from "chokidar";

//#region ../@intlayer/webpack/dist/esm/chokidar/dist/esm/watcher.mjs
/** @ts-ignore remove error Module '"chokidar"' has no exported member 'ChokidarOptions'. */
const recentlyAddedFiles = /* @__PURE__ */ new Set();
const watch$1 = (options) => {
	const configuration = options?.configuration ?? getConfiguration(options?.configOptions);
	const appLogger = getAppLogger(configuration);
	const { watch: isWatchMode, watchedFilesPatternWithPath } = configuration.content;
	/** @ts-ignore remove error Expected 0-1 arguments, but got 2. */
	return watch(watchedFilesPatternWithPath, {
		persistent: isWatchMode,
		ignoreInitial: true,
		awaitWriteFinish: {
			stabilityThreshold: 1e3,
			pollInterval: 100
		},
		ignored: [
			"**/node_modules/**",
			"**/dist/**",
			"**/build/**",
			"**/.intlayer/**"
		],
		...options
	}).on("add", async (filePath) => {
		const fileName = basename(filePath);
		recentlyAddedFiles.add(fileName);
		await handleAdditionalContentDeclarationFile(filePath, configuration);
		setTimeout(() => recentlyAddedFiles.delete(fileName), 1e3);
	}).on("change", async (filePath) => await handleContentDeclarationFileChange(filePath, configuration)).on("unlink", async (filePath) => {
		setTimeout(async () => {
			const fileName = basename(filePath);
			if (recentlyAddedFiles.has(fileName)) return;
			await handleUnlinkedContentDeclarationFile(filePath, configuration);
		}, 300);
	}).on("error", async (error) => {
		appLogger(`Watcher error: ${error}`, { level: "error" });
		appLogger("Restarting watcher");
		await prepareIntlayer(configuration);
	});
};

//#endregion
export { watch$1 };
//# sourceMappingURL=watcher.mjs.map