import { NextResponse } from "next/server";
import { getAuthFromRequest } from "@open-mercato/shared/lib/auth/server";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { getAllIntegrations } from "@open-mercato/shared/modules/integrations/types";
import { getDataSyncAdapter } from "../lib/adapter-registry.js";
const metadata = {
  GET: { requireAuth: true, requireFeatures: ["data_sync.view"] }
};
const openApi = {
  tags: ["DataSync"],
  summary: "List data sync integration options"
};
async function GET(req) {
  const auth = await getAuthFromRequest(req);
  if (!auth?.tenantId || !auth.orgId) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }
  const container = await createRequestContainer();
  const credentialsService = container.resolve("integrationCredentialsService");
  const stateService = container.resolve("integrationStateService");
  const scope = { organizationId: auth.orgId, tenantId: auth.tenantId };
  const items = await Promise.all(
    getAllIntegrations().filter((integration) => integration.hub === "data_sync" && integration.providerKey).map(async (integration) => {
      const adapter = getDataSyncAdapter(integration.providerKey);
      if (!adapter) return null;
      const [credentials, isEnabled] = await Promise.all([
        credentialsService.resolve(integration.id, scope).catch(() => null),
        stateService.resolveState(integration.id, scope).then((state) => state.isEnabled).catch(() => false)
      ]);
      return {
        integrationId: integration.id,
        title: integration.title,
        description: integration.description ?? null,
        providerKey: integration.providerKey ?? null,
        direction: adapter.direction,
        runMode: adapter.runMode ?? "generic",
        canStartRun: adapter.runMode !== "provider",
        supportedEntities: adapter.supportedEntities,
        hasCredentials: Boolean(credentials),
        isEnabled,
        settingsPath: `/backend/integrations/${encodeURIComponent(integration.id)}`
      };
    })
  );
  return NextResponse.json({
    items: items.filter((item) => Boolean(item))
  });
}
export {
  GET,
  metadata,
  openApi
};
//# sourceMappingURL=options.js.map
