import { getMatches } from "../file-matching.js";
import { defineSource } from "../source.js";
import { nonEmptyString, optionalUrl, stringArray } from "../utilities/schema-primitives.js";
import { parseSetupPy } from "../parsers/setup-py-parser.js";
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { z } from "zod";
//#region src/lib/sources/python-setup-py.ts
/** Parsed setup.py metadata */
const setupPyDataSchema = z.object({
	author: nonEmptyString,
	author_email: nonEmptyString,
	classifiers: stringArray,
	description: nonEmptyString,
	download_url: optionalUrl,
	extras_require: z.record(z.string(), z.array(z.string())),
	install_requires: stringArray,
	keywords: z.array(z.string()).optional(),
	license: nonEmptyString,
	long_description: nonEmptyString,
	maintainer: nonEmptyString,
	maintainer_email: nonEmptyString,
	name: nonEmptyString,
	project_urls: z.record(z.string(), z.string()),
	python_requires: nonEmptyString,
	url: optionalUrl,
	version: nonEmptyString
});
/** Parse setup.py content and validate through the Zod schema. */
async function parse(content) {
	const data = await parseSetupPy(content);
	return setupPyDataSchema.parse(data);
}
const pythonSetupPySource = defineSource({
	async discover(context) {
		return getMatches(context.options, ["setup.py"]);
	},
	key: "pythonSetupPy",
	async parse(input, context) {
		return {
			data: await parse(await readFile(resolve(context.options.path, input), "utf8")),
			source: input
		};
	},
	phase: 1
});
//#endregion
export { pythonSetupPySource };
