import { getAuthzMeta } from "./authz-meta.js";
import { getEntitlementMeta } from "./entitlement-meta.js";

//#region src/middleware/create-protected-route.ts
function toArray(value) {
	if (!value) return [];
	return Array.isArray(value) ? value : [value];
}
function createProtectedRoute(config) {
	const { permission, entitlement, ...routeConfig } = config;
	const meta = getAuthzMeta(permission);
	const entitlementMetaValue = entitlement ? getEntitlementMeta(entitlement) : void 0;
	const middlewares = [permission, ...toArray(routeConfig.middleware)];
	if (entitlement) middlewares.push(entitlement);
	return {
		...routeConfig,
		middleware: middlewares,
		...meta && { "x-authz": meta },
		...entitlementMetaValue && { "x-entitlement": entitlementMetaValue },
		...!meta && !("security" in config) && { security: [] }
	};
}

//#endregion
export { createProtectedRoute };