import { getMatches } from "../file-matching.js";
import { defineSource } from "../source.js";
import { nonEmptyString, optionalUrl, stringArray } from "../utilities/schema-primitives.js";
import { splitCommaSeparated } from "../utilities/template-helpers.js";
import { extractRfc822Body, parseRfc822Headers, splitMultiValues } from "../parsers/rfc822-header-parser.js";
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { z } from "zod";
//#region src/lib/sources/python-pkg-info.ts
/** Parsed PKG-INFO / METADATA metadata */
const pkgInfoDataSchema = z.object({
	author: nonEmptyString,
	author_email: nonEmptyString,
	classifiers: stringArray,
	description: nonEmptyString,
	description_content_type: nonEmptyString,
	download_url: optionalUrl,
	home_page: optionalUrl,
	keywords: z.array(z.string()).optional(),
	license: nonEmptyString,
	long_description: nonEmptyString,
	maintainer: nonEmptyString,
	maintainer_email: nonEmptyString,
	metadata_version: nonEmptyString,
	name: nonEmptyString,
	platforms: stringArray,
	project_urls: z.record(z.string(), z.string()),
	requires_dist: stringArray,
	requires_python: nonEmptyString,
	summary: nonEmptyString,
	version: nonEmptyString
});
/** Simple header-to-field mappings. */
const HEADER_MAP = {
	Author: "author",
	"Author-email": "author_email",
	"Description-Content-Type": "description_content_type",
	"Download-URL": "download_url",
	"Home-Page": "home_page",
	"Home-page": "home_page",
	License: "license",
	Maintainer: "maintainer",
	"Maintainer-email": "maintainer_email",
	"Metadata-Version": "metadata_version",
	Name: "name",
	"Requires-Python": "requires_python",
	Summary: "summary",
	Version: "version"
};
/**
* Parse a PKG-INFO or METADATA file and return structured metadata.
*
* Handles RFC 822-style headers with multi-value fields (Classifier,
* Requires-Dist, Project-URL, Platform), continuation lines, and
* the body section as long_description.
*/
function parse(source) {
	const headers = parseRfc822Headers(source);
	const data = {
		author: void 0,
		author_email: void 0,
		classifiers: [],
		description: void 0,
		description_content_type: void 0,
		download_url: void 0,
		home_page: void 0,
		keywords: void 0,
		license: void 0,
		long_description: void 0,
		maintainer: void 0,
		maintainer_email: void 0,
		metadata_version: void 0,
		name: void 0,
		platforms: [],
		project_urls: {},
		requires_dist: [],
		requires_python: void 0,
		summary: void 0,
		version: void 0
	};
	for (const [header, field] of Object.entries(HEADER_MAP)) {
		const value = headers[header];
		if (value && value !== "UNKNOWN") Object.assign(data, { [field]: value });
	}
	if (headers.Summary && headers.Summary !== "UNKNOWN") data.description = headers.Summary;
	if (headers.Keywords && headers.Keywords !== "UNKNOWN") data.keywords = splitCommaSeparated(headers.Keywords);
	data.classifiers = splitMultiValues(headers.Classifier);
	data.platforms = splitMultiValues(headers.Platform);
	data.requires_dist = splitMultiValues(headers["Requires-Dist"]);
	if (headers["Project-URL"]) for (const line of splitMultiValues(headers["Project-URL"])) {
		const commaIndex = line.indexOf(", ");
		if (commaIndex > 0) {
			const label = line.slice(0, commaIndex).trim();
			const url = line.slice(commaIndex + 2).trim();
			if (url) data.project_urls[label] = url;
		}
	}
	data.long_description = extractRfc822Body(source);
	return pkgInfoDataSchema.parse(data);
}
const pythonPkgInfoSource = defineSource({
	async discover(context) {
		return getMatches(context.options, ["PKG-INFO"]);
	},
	key: "pythonPkgInfo",
	async parse(input, context) {
		return {
			data: parse(await readFile(resolve(context.options.path, input), "utf8")),
			source: input
		};
	},
	phase: 1
});
//#endregion
export { pythonPkgInfoSource };
