import { formatPath } from "./utils/formatter.mjs";
import { buildDictionary } from "./buildIntlayerDictionary/buildIntlayerDictionary.mjs";
import { getBuiltDictionariesPath } from "./createDictionaryEntryPoint/getBuiltDictionariesPath.mjs";
import { createDictionaryEntryPoint } from "./createDictionaryEntryPoint/createDictionaryEntryPoint.mjs";
import { createModuleAugmentation } from "./createType/createModuleAugmentation.mjs";
import { createTypes } from "./createType/createType.mjs";
import { loadLocalDictionaries } from "./loadDictionaries/loadLocalDictionaries.mjs";
import { getAppLogger } from "@intlayer/config";

//#region ../@intlayer/webpack/dist/esm/chokidar/dist/esm/handleContentDeclarationFileChange.mjs
const handleContentDeclarationFileChange = async (filePath, config) => {
	const appLogger = getAppLogger(config);
	appLogger(`Change detected: ${formatPath(filePath)}`, { isVerbose: true });
	const dictionariesOutput = await buildDictionary(await loadLocalDictionaries(filePath, config), config);
	const updatedDictionariesPaths = Object.values(dictionariesOutput?.mergedDictionaries ?? {}).map((dictionary) => dictionary.dictionaryPath);
	const allDictionariesPaths = await getBuiltDictionariesPath(config);
	createTypes(updatedDictionariesPaths, config);
	appLogger("TypeScript types built", { isVerbose: true });
	if (updatedDictionariesPaths.some((updatedDictionaryPath) => !allDictionariesPaths.includes(updatedDictionaryPath))) {
		await createDictionaryEntryPoint(config);
		appLogger("Dictionary list built", { isVerbose: true });
	}
	createModuleAugmentation(config);
	appLogger("Module augmentation built", { isVerbose: true });
	for await (const plugin of config.plugins ?? []) {
		const { unmergedDictionaries, mergedDictionaries } = dictionariesOutput;
		await plugin.afterBuild?.({
			dictionaries: {
				unmergedDictionaries,
				mergedDictionaries
			},
			configuration: config
		});
	}
};

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