import { r as ux } from "./cli-B86kvKcR.mjs";
import { n as checkBlockLockState } from "./block-upstreams-CV5bdDNL.mjs";
import path from "node:path";
import process from "node:process";
import { defineCommand } from "citty";
import { readFile, stat } from "node:fs/promises";
//#region src/lib/catalog-block-check.ts
async function checkCatalogBlock(blockDir) {
	const resolvedBlockDir = path.resolve(blockDir);
	const blockName = path.basename(resolvedBlockDir);
	const packagePath = path.join(resolvedBlockDir, "package.json");
	const blockPath = path.join(resolvedBlockDir, "block.yaml");
	const changelogPath = path.join(resolvedBlockDir, "CHANGELOG.md");
	const failures = [];
	let packageJson = null;
	try {
		packageJson = JSON.parse(await readFile(packagePath, "utf8"));
	} catch (error) {
		failures.push(`${blockName}: package.json is required and must be valid JSON`);
		if (!(error instanceof SyntaxError)) return {
			blockName,
			checked: false,
			failures
		};
	}
	let blockYaml = "";
	try {
		blockYaml = await readFile(blockPath, "utf8");
	} catch {
		failures.push(`${blockName}: block.yaml is required`);
	}
	try {
		await stat(changelogPath);
	} catch {
		failures.push(`${blockName}: CHANGELOG.md is required for versioned block packages`);
	}
	if (packageJson) {
		const expectedPackageName = `@repo/block-${blockName}`;
		if (packageJson.private !== true) failures.push(`${blockName}: package.json must set private: true`);
		if (packageJson.name !== expectedPackageName) failures.push(`${blockName}: package name ${JSON.stringify(packageJson.name)} does not match ${JSON.stringify(expectedPackageName)}`);
	}
	if (blockYaml) {
		const authoredBlockName = readYamlScalar(blockYaml, "name");
		const blockVersion = readYamlScalar(blockYaml, "version");
		if (authoredBlockName !== blockName) failures.push(`${blockName}: block.yaml name ${JSON.stringify(authoredBlockName)} does not match directory name ${JSON.stringify(blockName)}`);
		if (packageJson?.version !== blockVersion) failures.push(`${blockName}: package.json version ${JSON.stringify(packageJson?.version)} does not match block.yaml version ${JSON.stringify(blockVersion)}`);
	}
	if (blockYaml) try {
		const lockFailures = await checkBlockLockState(resolvedBlockDir);
		failures.push(...lockFailures.map((failure) => `${blockName}: ${failure.path}: ${failure.message}`));
	} catch (error) {
		failures.push(`${blockName}: failed to validate block-lock.yaml state: ${error instanceof Error ? error.message : "unknown error"}`);
	}
	return {
		blockName,
		checked: packageJson !== null,
		failures
	};
}
function formatCatalogBlockCheckFailures(failures) {
	return ["Catalog block check failed:", ...failures.map((failure) => `- ${failure}`)].join("\n");
}
function readYamlScalar(source, key) {
	const match = source.match(new RegExp(`^${key}:\\s*(.+)$`, "m"));
	return match ? match[1].trim() : null;
}
//#endregion
//#region src/commands/catalog/block/check.ts
async function runCatalogBlockCheckCommand(blockPath) {
	try {
		const result = await ux.running("Checking catalog block", async () => checkCatalogBlock(blockPath));
		if (result.failures.length > 0) throw new Error(formatCatalogBlockCheckFailures(result.failures));
		ux.log.success(`Catalog block ${result.blockName} passed.`);
	} catch (error) {
		ux.handleError(error);
	}
}
var check_default = defineCommand({
	meta: {
		name: "check",
		description: "Validate a catalog block package and lock state"
	},
	args: { path: {
		type: "positional",
		description: "Block package path",
		required: false
	} },
	async run({ args }) {
		await runCatalogBlockCheckCommand(args.path || ".");
		if (!process.exitCode) process.exit(0);
	}
});
//#endregion
export { check_default as default };

//# sourceMappingURL=check-D0ivl-LW.mjs.map