import { r as ux } from "./cli-B86kvKcR.mjs";
import { r as syncBlockUpstreams, t as applyBlockUpstreamItem } from "./block-upstreams-CV5bdDNL.mjs";
import process from "node:process";
import { defineCommand } from "citty";
//#region src/commands/catalog/block/upstream.ts
const blockItemKinds = [
	"agents",
	"commands",
	"plugins",
	"references",
	"rules",
	"skills"
];
async function runBlockUpstreamSyncCommand(blockPath, upstreamKey) {
	try {
		const result = await ux.running("Syncing block upstreams and lock state", async () => syncBlockUpstreams(blockPath, upstreamKey));
		ux.log.success(`Synced block ${result.blockName}; refreshed ${result.fetched.length} upstream snapshot(s).`);
	} catch (error) {
		ux.handleError(error);
	}
}
async function runBlockUpstreamApplyCommand(blockPath, kind, item) {
	try {
		if (!isBlockItemKind(kind)) throw new Error(`Invalid block item kind: ${kind}`);
		const result = await ux.running("Applying block upstream item", async () => applyBlockUpstreamItem(blockPath, kind, item));
		if (result.mode === "derived") {
			ux.log.info(`Derived item ${result.kind}.${result.item} was not overwritten. Review upstream snapshots: ${result.skipped.join(", ")}`);
			return;
		}
		ux.log.success(`Applied ${result.kind}.${result.item}: ${result.applied.join(", ")}`);
	} catch (error) {
		ux.handleError(error);
	}
}
const syncCommand = defineCommand({
	meta: {
		name: "sync",
		description: "Refresh block upstream snapshots and block-lock provenance"
	},
	args: {
		path: {
			type: "positional",
			description: "Block package path",
			required: false
		},
		upstream: {
			type: "positional",
			description: "Optional upstream key",
			required: false
		}
	},
	async run({ args }) {
		await runBlockUpstreamSyncCommand(args.path || ".", args.upstream);
		if (!process.exitCode) process.exit(0);
	}
});
const applyCommand = defineCommand({
	meta: {
		name: "apply",
		description: "Apply fetched upstream snapshots to a vendored block item"
	},
	args: {
		path: {
			type: "positional",
			description: "Block package path",
			required: true
		},
		kind: {
			type: "positional",
			description: "Block item kind",
			required: true
		},
		item: {
			type: "positional",
			description: "Block item name",
			required: true
		}
	},
	async run({ args }) {
		await runBlockUpstreamApplyCommand(args.path, args.kind, args.item);
		if (!process.exitCode) process.exit(0);
	}
});
function isBlockItemKind(kind) {
	return blockItemKinds.includes(kind);
}
//#endregion
export { applyCommand, syncCommand };

//# sourceMappingURL=upstream-B4p9zRXY.mjs.map