import { createLogger } from "../../utils/logger/index.js";
import { init_logger } from "../../utils/logger.js";
import { join } from "path";
import { watch } from "fs";
//#region src/agent/skills/watcher.ts
/**
* Skill Watcher
* 
* Watches skill directories for changes and triggers reloads.
*
*/
init_logger();
const log = createLogger("SkillWatcher");
/**
* Create a debounced reload function
*/
function createDebouncedReload(loader, debounceMs) {
	let timeout = null;
	let pendingReload = false;
	return () => {
		if (timeout) {
			pendingReload = true;
			return;
		}
		timeout = setTimeout(() => {
			timeout = null;
			if (pendingReload) {
				pendingReload = false;
				loader.reload();
				log.info("Skills reloaded after directory changes");
			}
		}, debounceMs);
	};
}
/**
* Create a skill watcher
*/
function createSkillWatcher(loader, options) {
	const { watchDirs, debounceMs = 1e3 } = options;
	const watchers = [];
	const debouncedReload = createDebouncedReload(loader, debounceMs);
	let isWatching = false;
	let lastRefreshTime = Date.now();
	function watchDirectory(dir) {
		try {
			const watcher = watch(dir, { recursive: true }, (eventType, filename) => {
				if (!filename) return;
				if (filename.toLowerCase() === "skill.md") {
					log.debug({
						eventType,
						filename,
						dir
					}, "Skill file changed");
					lastRefreshTime = Date.now();
					debouncedReload();
				}
				if (eventType === "rename") {
					log.debug({
						eventType,
						filename,
						dir
					}, "Directory changed, may be new skill");
					lastRefreshTime = Date.now();
					debouncedReload();
				}
			});
			watchers.push({
				watcher,
				dir
			});
			log.info({ dir }, "Watching skill directory");
		} catch (err) {
			log.warn({
				dir,
				error: err
			}, "Failed to watch skill directory");
		}
	}
	return {
		start: () => {
			if (isWatching) {
				log.warn("Watcher already started");
				return;
			}
			for (const dir of watchDirs) watchDirectory(dir);
			isWatching = true;
			log.info({ dirs: watchDirs }, "Skill watcher started");
		},
		stop: () => {
			if (!isWatching) return;
			for (const { watcher } of watchers) watcher.close();
			watchers.length = 0;
			isWatching = false;
			log.info("Skill watcher stopped");
		},
		isWatching: () => isWatching,
		getLastRefreshTime: () => lastRefreshTime
	};
}
/**
* Create a skill watcher from loader configuration
*/
function createWatcherFromLoader(loader, options) {
	const watchDirs = [];
	if (options.workspaceDir) {
		const workspaceSkillsDir = join(options.workspaceDir, "skills");
		watchDirs.push(workspaceSkillsDir);
	}
	if (options.builtinDir) watchDirs.push(options.builtinDir);
	if (options.globalDirs) watchDirs.push(...options.globalDirs);
	if (options.extraDirs) watchDirs.push(...options.extraDirs);
	if (watchDirs.length === 0) {
		log.warn("No directories to watch");
		return null;
	}
	return createSkillWatcher(loader, {
		watchDirs,
		debounceMs: options.debounceMs ?? 1e3
	});
}
//#endregion
export { createSkillWatcher, createWatcherFromLoader };

//# sourceMappingURL=watcher.js.map