import { c as parseProviderSource, i as getCatalogProviderDeclaration } from "./manifest-CslkSMd8.mjs";
import { createRequire } from "node:module";
import { existsSync, readdirSync } from "node:fs";
import path from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import { homedir } from "node:os";
//#region src/catalog.ts
const require = createRequire(import.meta.url);
function getCatalogCandidates(relativePath) {
	const currentDir = path.dirname(fileURLToPath(import.meta.url));
	return [path.resolve(process.cwd(), relativePath), path.resolve(currentDir, "../../../", relativePath)];
}
function resolveCatalogPath(relativePath) {
	for (const candidate of getCatalogCandidates(relativePath)) if (existsSync(candidate)) return candidate;
	return null;
}
function getDefaultCatalogProviderKey(loadedManifest) {
	if (loadedManifest.manifest.catalogs?.anvil) return "anvil";
	return Object.keys(loadedManifest.manifest.catalogs ?? {})[0] ?? null;
}
function resolveCatalogProviders(loadedManifest) {
	return Object.entries(loadedManifest.manifest.catalogs ?? {}).map(([key, declaration]) => resolveCatalogProvider(loadedManifest, key, declaration)).filter((provider) => Boolean(provider));
}
function resolveCatalogProviderByKey(loadedManifest, providerKey) {
	const declaration = getCatalogProviderDeclaration(loadedManifest, providerKey);
	if (!declaration) return null;
	return resolveCatalogProvider(loadedManifest, providerKey, declaration);
}
function resolveCatalogRootFromDeclaration(declaration, baseDir) {
	const parsed = parseProviderSource(typeof declaration === "string" ? declaration : declaration.source);
	if (parsed.type === "local") return resolveLocalCatalogRoot(parsed.value, baseDir);
	if (parsed.type === "npm") return resolveNpmCatalogRoot(parsed.value, baseDir);
	return null;
}
function resolveCatalogPathFromProvider(provider, relativePath) {
	const candidate = path.join(provider.rootPath, relativePath);
	return existsSync(candidate) ? candidate : null;
}
function resolveLocalCatalogRoot(value, baseDir) {
	const expanded = expandLocalSourcePath(value, baseDir);
	const directCatalogRoot = path.basename(expanded) === "@catalog" ? expanded : path.join(expanded, "@catalog");
	if (existsSync(path.join(directCatalogRoot, "blocks")) || existsSync(path.join(directCatalogRoot, "rules"))) return directCatalogRoot;
	return null;
}
function getCatalogBlocksDir() {
	return resolveCatalogPath("@catalog/blocks");
}
function getCatalogRoot() {
	return resolveCatalogPath("@catalog");
}
function getCatalogBlockPluginPath(name) {
	const blocksDir = getCatalogBlocksDir();
	if (!blocksDir) return null;
	for (const blockName of readdirSync(blocksDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name)) {
		const pluginPath = path.join(blocksDir, blockName, "content", "plugins", name);
		if (existsSync(pluginPath)) return pluginPath;
		for (const extension of [".ts", ".js"]) {
			const singleFilePath = path.join(blocksDir, blockName, "content", "plugins", `${name}${extension}`);
			if (existsSync(singleFilePath)) return singleFilePath;
		}
	}
	return null;
}
function getCatalogBlockSkillPath(name) {
	const blocksDir = getCatalogBlocksDir();
	if (!blocksDir) return null;
	for (const blockName of readdirSync(blocksDir, { withFileTypes: true }).filter((entry) => entry.isDirectory()).map((entry) => entry.name)) {
		const skillPath = path.join(blocksDir, blockName, "content", "skills", name);
		if (existsSync(skillPath)) return skillPath;
	}
	return null;
}
function getCatalogPluginPath(name) {
	return resolveCatalogPath(`@catalog/plugins/${name}/content`);
}
function getCatalogSkillPluginPath(skillName, pluginName) {
	return resolveCatalogPath(`@catalog/skills/${skillName}/plugins/${pluginName}`);
}
function getCatalogSkillRootPath(name) {
	return resolveCatalogPath(`@catalog/skills/${name}`);
}
function resolveCatalogProvider(loadedManifest, key, declaration) {
	const source = parseProviderSource(typeof declaration === "string" ? declaration : declaration.source);
	const rootPath = resolveCatalogRootFromDeclaration(declaration, loadedManifest.directory);
	if (!rootPath) return null;
	return {
		key,
		rootPath,
		source
	};
}
function expandLocalSourcePath(value, baseDir) {
	if (value === "~") return homedir();
	if (value.startsWith("~/")) return path.join(homedir(), value.slice(2));
	return path.resolve(baseDir, value);
}
function resolveNpmCatalogRoot(value, baseDir) {
	const packageName = parseNpmPackageName(value);
	if (!packageName) return null;
	try {
		const packageJsonPath = require.resolve(`${packageName}/package.json`, { paths: [baseDir] });
		const packageRoot = path.dirname(packageJsonPath);
		for (const candidate of [packageRoot, path.join(packageRoot, "build")]) if (existsSync(path.join(candidate, "blocks")) || existsSync(path.join(candidate, "rules"))) return candidate;
	} catch {
		return null;
	}
	return null;
}
function parseNpmPackageName(value) {
	if (!value) return null;
	if (value.startsWith("@")) {
		const secondSeparator = value.indexOf("@", value.indexOf("/") + 1);
		return secondSeparator === -1 ? value : value.slice(0, secondSeparator);
	}
	const separatorIndex = value.indexOf("@");
	return separatorIndex === -1 ? value : value.slice(0, separatorIndex);
}
//#endregion
export { getCatalogRoot as a, getDefaultCatalogProviderKey as c, resolveCatalogProviders as d, resolveLocalCatalogRoot as f, getCatalogPluginPath as i, resolveCatalogPathFromProvider as l, getCatalogBlockSkillPath as n, getCatalogSkillPluginPath as o, getCatalogBlocksDir as r, getCatalogSkillRootPath as s, getCatalogBlockPluginPath as t, resolveCatalogProviderByKey as u };

//# sourceMappingURL=catalog-BN_wLtPe.mjs.map