/**
 * graphql-schema-builder.ts
 *
 * Builds a live GraphQL schema from OpenAPI 3 via **@omnigraph/openapi**
 * (the same stack as [GraphQL Mesh OpenAPI](https://the-guild.dev/graphql/mesh/docs/handlers/openapi)).
 * Resolvers proxy to the upstream REST API with headers from
 * CLAWQL_HTTP_HEADERS, CLAWQL_BEARER_TOKEN, and/or GOOGLE_ACCESS_TOKEN.
 */
import loadGraphQLSchemaFromOpenAPI from "@omnigraph/openapi";
import { getPackageRoot } from "./package-root.js";
function mergeAuthHeaders() {
    const out = {};
    const raw = process.env.CLAWQL_HTTP_HEADERS;
    if (raw) {
        try {
            Object.assign(out, JSON.parse(raw));
        }
        catch {
            console.error("[graphql-schema] Invalid CLAWQL_HTTP_HEADERS (expected JSON object)");
        }
    }
    const bearer = process.env.CLAWQL_BEARER_TOKEN || process.env.GOOGLE_ACCESS_TOKEN;
    if (bearer && !out.Authorization && !out.authorization) {
        out.Authorization = `Bearer ${bearer}`;
    }
    return out;
}
export async function buildGraphQLSchema(openapi, baseUrl) {
    const headers = mergeAuthHeaders();
    // `source` may be a file path, URL, or an in-memory OpenAPI object (Mesh dereferences it).
    const schema = await loadGraphQLSchemaFromOpenAPI("ClawQL", {
        source: openapi,
        endpoint: baseUrl,
        cwd: getPackageRoot(),
        operationHeaders: Object.keys(headers).length > 0 ? headers : undefined,
        // Fewer union types on large specs; still returns JSON bodies.
        ignoreErrorResponses: true,
    });
    return { schema, contextValue: {} };
}
