import { NextResponse } from "next/server";
import { z } from "zod";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { getModules } from "@open-mercato/shared/lib/i18n/server";
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["auth.acl.manage"] }
};
function normalizeDependsOn(value) {
  if (!Array.isArray(value)) return void 0;
  const out = [];
  for (const entry of value) {
    if (typeof entry !== "string") continue;
    const trimmed = entry.trim();
    if (!trimmed) continue;
    out.push(trimmed);
  }
  if (out.length === 0) return void 0;
  return Array.from(new Set(out));
}
async function GET(req) {
  const auth = await getAuthFromRequest(req);
  if (!auth) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  const modules = getModules();
  const items = (modules || []).flatMap(
    (m) => (m.features || []).map((f) => {
      const deps = normalizeDependsOn(f?.dependsOn);
      const base = {
        id: String(f.id),
        title: String(f.title || f.id),
        module: String(f.module || m.id)
      };
      if (deps) base.dependsOn = deps;
      return base;
    })
  );
  const byId = /* @__PURE__ */ new Map();
  for (const it of items) if (!byId.has(it.id)) byId.set(it.id, it);
  const list = Array.from(byId.values()).sort((a, b) => a.module.localeCompare(b.module) || a.id.localeCompare(b.id));
  const moduleInfo = /* @__PURE__ */ new Map();
  for (const m of modules) {
    if (m.id) {
      moduleInfo.set(m.id, { id: m.id, title: m.info?.title || m.id });
    }
  }
  return NextResponse.json({ items: list, modules: Array.from(moduleInfo.values()) });
}
const featureItemSchema = z.object({
  id: z.string(),
  title: z.string(),
  module: z.string(),
  dependsOn: z.array(z.string()).optional()
});
const featureModuleSchema = z.object({
  id: z.string(),
  title: z.string()
});
const featuresResponseSchema = z.object({
  items: z.array(featureItemSchema),
  modules: z.array(featureModuleSchema)
});
const featuresMethodDoc = {
  summary: "List declared feature flags",
  description: "Returns all static features contributed by the enabled modules along with their module source.",
  tags: ["Authentication & Accounts"],
  responses: [
    {
      status: 200,
      description: "Aggregated feature catalog",
      schema: featuresResponseSchema
    }
  ],
  errors: [
    {
      status: 401,
      description: "Missing authentication",
      schema: z.object({ error: z.string() })
    }
  ]
};
const openApi = {
  summary: "List declared feature flags",
  methods: {
    GET: featuresMethodDoc
  }
};
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=features.js.map
