import { detectSchemaType, formatValidationErrors, validateManifest, validateTemplateManifest } from "./validate-manifest.js";
import fs from "node:fs";
import path from "node:path";
import { Command } from "commander";
import process from "node:process";

//#region src/cli/commands/plugin/validate/validate.ts
function resolveManifestPaths(paths, cwd) {
	const out = [];
	for (const p of paths) {
		const resolved = path.resolve(cwd, p);
		if (!fs.existsSync(resolved)) {
			console.error(`Path not found: ${p}`);
			continue;
		}
		if (fs.statSync(resolved).isDirectory()) {
			const pluginManifest = path.join(resolved, "manifest.json");
			const templateManifest = path.join(resolved, "appkit.plugins.json");
			let found = false;
			if (fs.existsSync(pluginManifest)) {
				out.push(pluginManifest);
				found = true;
			}
			if (fs.existsSync(templateManifest)) {
				out.push(templateManifest);
				found = true;
			}
			if (!found) console.error(`No manifest.json or appkit.plugins.json in directory: ${p}`);
		} else out.push(resolved);
	}
	return out;
}
function runPluginValidate(paths) {
	const cwd = process.cwd();
	const manifestPaths = resolveManifestPaths(paths.length > 0 ? paths : ["."], cwd);
	if (manifestPaths.length === 0) {
		console.error("No manifest files to validate.");
		process.exit(1);
	}
	let hasFailure = false;
	for (const manifestPath of manifestPaths) {
		let obj;
		try {
			const raw = fs.readFileSync(manifestPath, "utf-8");
			obj = JSON.parse(raw);
		} catch (err) {
			console.error(`✗ ${manifestPath}`);
			console.error(`  ${err instanceof Error ? err.message : String(err)}`);
			hasFailure = true;
			continue;
		}
		const result = detectSchemaType(obj) === "template-plugins" ? validateTemplateManifest(obj) : validateManifest(obj);
		const relativePath = path.relative(cwd, manifestPath);
		if (result.valid) console.log(`✓ ${relativePath}`);
		else {
			console.error(`✗ ${relativePath}`);
			if (result.errors?.length) console.error(formatValidationErrors(result.errors, obj));
			hasFailure = true;
		}
	}
	process.exit(hasFailure ? 1 : 0);
}
const pluginValidateCommand = new Command("validate").description("Validate plugin manifest(s) or template manifests against their JSON schema").argument("[paths...]", "Paths to manifest.json, appkit.plugins.json, or plugin directories (default: .)").action(runPluginValidate);

//#endregion
export { pluginValidateCommand };
//# sourceMappingURL=validate.js.map