import { getMatches } from "../file-matching.js";
import { formatPath } from "../utilities/formatting.js";
import { defineSource } from "../source.js";
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { z } from "zod";
import { parse } from "yaml";
//#region src/lib/sources/github-actions.ts
/**
* Source for GitHub Actions workflow files.
*
* Discovers `.github/workflows/*.yml` and `*.yaml` files and extracts the
* workflow name and file path from each.
*/
const githubActionSchema = z.object({
	file: z.string(),
	name: z.string()
});
const githubActionsSource = defineSource({
	async discover(context) {
		return getMatches(context.options, [".github/workflows/*.{yml,yaml}"]);
	},
	key: "githubActions",
	async parse(input, context) {
		const parsed = parse(await readFile(resolve(context.options.path, input), "utf8"));
		if (typeof parsed !== "object" || parsed === null || !("name" in parsed)) return;
		const { name } = parsed;
		if (typeof name !== "string") return;
		const file = formatPath(resolve(context.options.path, input), context.options.path, context.options.absolute);
		return {
			data: githubActionSchema.parse({
				file,
				name
			}),
			source: input
		};
	},
	phase: 1
});
//#endregion
export { githubActionsSource };
