import { log } from "../log.js";
import { getWorkspaces } from "../file-matching.js";
import { defineSource } from "../source.js";
import { tokei } from "@kitschpatrol/tokei";
//#region src/lib/sources/code-stats.ts
async function getStatistics(directory, options) {
	const perLanguage = (await tokei({
		include: [directory],
		noIgnore: !options.respectIgnored,
		noIgnoreDot: !options.respectIgnored,
		noIgnoreVcs: !options.respectIgnored
	})).toSorted((a, b) => b.code - a.code);
	return {
		data: {
			perLanguage,
			total: {
				blanks: perLanguage.reduce((sum, entry) => sum + entry.blanks, 0),
				code: perLanguage.reduce((sum, entry) => sum + entry.code, 0),
				comments: perLanguage.reduce((sum, entry) => sum + entry.comments, 0),
				files: perLanguage.reduce((sum, entry) => sum + entry.files, 0),
				languages: perLanguage.map((entry) => entry.language),
				lines: perLanguage.reduce((sum, entry) => sum + entry.lines, 0)
			}
		},
		source: directory
	};
}
const codeStatsSource = defineSource({
	async discover(context) {
		return [context.options.path, ...getWorkspaces(context.options.path, context.options.workspaces)];
	},
	key: "codeStats",
	async parse(input, context) {
		log.debug("Extracting lines of code via tokei...");
		return getStatistics(input, context.options);
	},
	phase: 1
});
//#endregion
export { codeStatsSource };
