import { d as defineEventHandler, aG as requireFeature, c as createError, aR as findSsoProviderBySlug, aX as generateSamlMetadata, s as setResponseHeader, aW as isSamlUnavailableError } from '../../../../../../nitro/nitro.mjs';
import { b as buildSsoCallbackUrl } from '../../../../../../_/url-helpers.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const metadata_xml_get = defineEventHandler(async (event) => {
  var _a;
  await requireFeature("enterprise.sso", event);
  const slug = (_a = event.context.params) == null ? void 0 : _a.slug;
  if (!slug || typeof slug !== "string") {
    throw createError({
      statusCode: 400,
      statusMessage: "Bad Request",
      message: "Provider slug is required"
    });
  }
  const provider = await findSsoProviderBySlug(slug);
  if (!provider || provider.type !== "saml") {
    throw createError({
      statusCode: 404,
      statusMessage: "Not Found",
      message: "SAML provider not found"
    });
  }
  try {
    const callbackUrl = buildSsoCallbackUrl(event, provider.slug);
    const metadata = await generateSamlMetadata(provider.config, callbackUrl);
    setResponseHeader(event, "Content-Type", "application/xml; charset=utf-8");
    return metadata;
  } catch (error) {
    const message = error instanceof Error ? error.message : "Failed to build metadata";
    console.error(`[GET /api/admin/auth/sso/${slug}/metadata.xml] Error:`, message);
    if (isSamlUnavailableError(error)) {
      throw createError({
        statusCode: 501,
        statusMessage: "SAML Support Not Available",
        message
      });
    }
    throw createError({
      statusCode: 500,
      statusMessage: "Failed to build SAML metadata",
      message
    });
  }
});

export { metadata_xml_get as default };
//# sourceMappingURL=metadata.xml.get.mjs.map
