import { getMatches } from "../file-matching.js";
import { defineSource } from "../source.js";
import { nonEmptyString, optionalUrl, stringArray } from "../utilities/schema-primitives.js";
import { parseGemspec } from "../parsers/gemspec-parser.js";
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { z } from "zod";
//#region src/lib/sources/ruby-gemspec.ts
const gemSpecDependencySchema = z.object({
	name: z.string(),
	requirements: z.array(z.string()),
	type: z.enum(["development", "runtime"])
});
/** @public */
const gemSpecSchema = z.object({
	authors: stringArray,
	bindir: nonEmptyString,
	cert_chain: stringArray,
	dependencies: z.array(gemSpecDependencySchema),
	description: nonEmptyString,
	email: z.union([z.string(), z.array(z.string())]).optional(),
	executables: stringArray,
	extensions: stringArray,
	extra: z.record(z.string(), z.unknown()),
	extra_rdoc_files: stringArray,
	files: stringArray,
	homepage: optionalUrl,
	license: nonEmptyString,
	licenses: stringArray,
	metadata: z.record(z.string(), z.string()),
	name: nonEmptyString,
	platform: nonEmptyString,
	post_install_message: nonEmptyString,
	rdoc_options: stringArray,
	require_paths: stringArray,
	required_ruby_version: nonEmptyString,
	required_rubygems_version: nonEmptyString,
	signing_key: nonEmptyString,
	summary: nonEmptyString,
	test_files: stringArray,
	version: nonEmptyString
});
/** Parse gemspec content and validate through the Zod schema. */
async function parse(content) {
	const raw = await parseGemspec(content);
	return gemSpecSchema.parse(raw);
}
const rubyGemspecSource = defineSource({
	async discover(context) {
		return getMatches(context.options, ["*.gemspec"]);
	},
	key: "rubyGemspec",
	async parse(input, context) {
		return {
			data: await parse(await readFile(resolve(context.options.path, input), "utf8")),
			source: input
		};
	},
	phase: 1
});
//#endregion
export { rubyGemspecSource };
