import { getMatches } from "../file-matching.js";
import { defineSource } from "../source.js";
import { nonEmptyString, optionalUrl, stringArray } from "../utilities/schema-primitives.js";
import { parseGoMod } from "../parsers/go-mod-parser.js";
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { z } from "zod";
//#region src/lib/sources/go-go-mod.ts
const goModDependencySchema = z.object({
	module: z.string(),
	version: z.string()
});
const goModDataSchema = z.object({
	dependencies: z.array(goModDependencySchema),
	go_version: nonEmptyString,
	module: nonEmptyString,
	repository_url: optionalUrl,
	tool_dependencies: stringArray
});
/** Parse a go.mod file string and validate through the schema. */
function parse(content) {
	return goModDataSchema.parse(parseGoMod(content));
}
const goGoModSource = defineSource({
	async discover(context) {
		return getMatches(context.options, ["go.mod"]);
	},
	key: "goGoMod",
	async parse(input, context) {
		return {
			data: parse(await readFile(resolve(context.options.path, input), "utf8")),
			source: input
		};
	},
	phase: 1
});
//#endregion
export { goGoModSource };
