// Figma HTTP seam (Epic #750, Issue #751).
//
// The core connector NEVER calls `fetch`/undici directly. It depends only on this small
// injectable port so unit tests can mock the transport and a future proxy-aware /
// custom-CA client (#802) can be slotted in without touching connector logic.
//
// The request type intentionally carries the outbound headers (including the
// `X-Figma-Token` auth header). The token is materialised into that header ONLY by the
// default adapter below, immediately before the platform `fetch` call — it is never
// logged here and never re-emitted by the port.
import { classifyFigmaTransportError, FigmaConnectorError } from "./figmaConnectorErrors.js";
import { gatewayFetch, } from "@oscharko-dev/keiko-model-gateway/internal/http";
// Collects the response headers into a plain lower-cased map. `Headers` already lower-cases
// names, so this is a faithful, allocation-bounded copy with no token (request-only header).
const collectHeaders = (response) => {
    const out = {};
    response.headers.forEach((value, name) => {
        out[name] = value;
    });
    return out;
};
const parseJsonBody = async (response) => {
    const text = await response.text();
    if (text.length === 0)
        return undefined;
    try {
        return JSON.parse(text);
    }
    catch {
        return undefined;
    }
};
/**
 * Thin default adapter over the platform `fetch`. This is the ONLY place `fetch` is named.
 * It forwards the caller-built headers verbatim and reads the body as JSON; it does not
 * log, retry, or inspect the token. Resilience/backoff is out of scope here (#759); the
 * proxy/custom-CA transport is the platform prerequisite (#802) that replaces this adapter.
 */
export const createDefaultFigmaHttpPort = (egress, fetchImpl) => {
    return async (request) => {
        try {
            const response = await gatewayFetch(request.url, {
                method: "GET",
                headers: { ...request.headers },
                ...(egress !== undefined ? { egress } : {}),
                ...(fetchImpl !== undefined ? { fetchImpl } : {}),
            });
            const headers = collectHeaders(response);
            const json = await parseJsonBody(response);
            return { status: response.status, json, headers };
        }
        catch (err) {
            if (err instanceof FigmaConnectorError)
                throw err;
            throw new FigmaConnectorError(classifyFigmaTransportError(err));
        }
    };
};
