const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
const require_logger = require('../../../config/dist/esm/logger.cjs');
const require_getConfiguration = require('../../../config/dist/esm/configFile/getConfiguration.cjs');
const require_handleAdditionalContentDeclarationFile = require('./handleAdditionalContentDeclarationFile.cjs');
const require_handleContentDeclarationFileChange = require('./handleContentDeclarationFileChange.cjs');
const require_handleUnlinkedContentDeclarationFile = require('./handleUnlinkedContentDeclarationFile.cjs');
const require_prepareIntlayer = require('./prepareIntlayer.cjs');
let node_path = require("node:path");
let chokidar = require("chokidar");

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

//#endregion
exports.buildAndWatchIntlayer = buildAndWatchIntlayer;
exports.watch = watch;
//# sourceMappingURL=watcher.cjs.map