import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

//#region src/cli/commands/plugin/schema-resources.ts
/**
* Resource types and permissions derived from plugin-manifest.schema.json.
* Single source of truth so create, add-resource, and validate stay in sync with the schema.
*/
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SCHEMA_NAME = "plugin-manifest.schema.json";
const SCHEMA_PATHS = [path.join(__dirname, "..", "..", "..", "schemas", SCHEMA_NAME), path.join(__dirname, "..", "..", "schemas", SCHEMA_NAME)];
function loadSchema() {
	for (const schemaPath of SCHEMA_PATHS) try {
		if (fs.existsSync(schemaPath)) return JSON.parse(fs.readFileSync(schemaPath, "utf-8"));
	} catch {}
	return null;
}
/** Optional display overrides for acronyms (e.g. SQL, UC). Omitted entries use title-case of value. */
const LABEL_OVERRIDES = {
	sql_warehouse: "SQL Warehouse",
	uc_function: "UC Function",
	uc_connection: "UC Connection"
};
function humanize(value) {
	if (LABEL_OVERRIDES[value]) return LABEL_OVERRIDES[value];
	return value.replace(/_/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
}
let cachedOptions = null;
let cachedPermissions = null;
/**
* Resource type options (value + label) from schema $defs.resourceType.enum.
*/
function getResourceTypeOptions() {
	if (cachedOptions) return cachedOptions;
	const enumArr = ((loadSchema()?.$defs)?.resourceType)?.enum;
	if (!Array.isArray(enumArr)) {
		cachedOptions = [];
		return cachedOptions;
	}
	cachedOptions = enumArr.map((value) => ({
		value,
		label: humanize(value)
	}));
	return cachedOptions;
}
/**
* Permissions per resource type from schema resourceRequirement.allOf (if/then).
*/
function getResourceTypePermissions() {
	if (cachedPermissions) return cachedPermissions;
	const schema = loadSchema();
	const out = {};
	if (!schema?.$defs || typeof schema.$defs !== "object") {
		cachedPermissions = out;
		return out;
	}
	const allOf = schema.$defs.resourceRequirement?.allOf;
	if (!Array.isArray(allOf)) {
		cachedPermissions = out;
		return out;
	}
	for (const branch of allOf) {
		const typeConst = branch?.if?.properties?.type?.const;
		const ref = branch?.then?.properties?.permission?.$ref;
		if (typeof typeConst !== "string" || typeof ref !== "string") continue;
		const refSegments = ref.replace(/^#\//, "").split("/");
		let def = schema;
		for (const seg of refSegments) {
			if (def == null || typeof def !== "object") break;
			def = def[seg];
		}
		const enumArr = Array.isArray(def?.enum) ? def.enum : void 0;
		if (enumArr?.length) out[typeConst] = enumArr;
	}
	cachedPermissions = out;
	return out;
}

//#endregion
export { getResourceTypeOptions, getResourceTypePermissions };
//# sourceMappingURL=schema-resources.js.map