import axios from "axios";
export async function apiGet(route, config) {
    const url = process.env.LAYOUT_SERVICE_URL + route;
    const requestHeaders = getRequestHeaders(config);
    try {
        const response = await axios.get(url, {
            headers: requestHeaders,
        });
        return {
            data: response.data,
            status: response.status,
            headers: convertAxiosHeaders(response.headers),
        };
    }
    catch (error) {
        if (error.response) {
            return {
                status: error.response.status,
                error: error.response.statusText,
                headers: convertAxiosHeaders(error.response.headers),
            };
        }
        else {
            return {
                status: 500,
                error: error.message,
                headers: convertAxiosHeaders(error.response.headers),
            };
        }
    }
}
function getRequestHeaders(config) {
    const requestHeaders = {};
    // Convert Headers (node-fetch) to AxiosRequestHeaders (plain object)
    if (config?.headers) {
        config.headers.forEach((value, key) => {
            requestHeaders[key] = value;
        });
    }
    // Set default headers
    requestHeaders["Content-Type"] = "application/json";
    requestHeaders["x-api-key"] = config?.apiKey ?? getApiKey();
    return requestHeaders;
}
export async function apiPostObject(route, object, config) {
    const url = process.env.LAYOUT_SERVICE_URL + route;
    const requestHeaders = getRequestHeaders(config);
    const body = JSON.stringify(object);
    try {
        const response = await axios.post(url, body, {
            headers: requestHeaders,
        });
        return {
            data: response.data,
            status: response.status,
            headers: convertAxiosHeaders(response.headers),
        };
    }
    catch (error) {
        if (error.errors) {
            let message = "";
            error.errors.forEach((error) => {
                message += error.message + " ";
            });
            return {
                status: 500,
                error: message,
                headers: new Headers(),
            };
        }
        if (error.response) {
            return {
                status: error.response.status,
                error: error.response.statusText,
                headers: convertAxiosHeaders(error.response.headers),
            };
        }
        else {
            return {
                status: 500,
                error: error.message,
                headers: new Headers(),
            };
        }
    }
}
export function getApiKey() {
    const apiKey = process.env.ALPACA_HEADLESS_API_KEY || process.env.API_KEY;
    if (!apiKey)
        console.log("ERROR: Could not find API key environment variable: ALPACA_HEADLESS_API_KEY");
    return apiKey;
}
function convertAxiosHeaders(headers) {
    const convertedHeaders = new Headers();
    for (const key in headers) {
        if (headers.hasOwnProperty(key)) {
            convertedHeaders.set(key.toLowerCase(), headers[key]);
        }
    }
    return convertedHeaders;
}
export async function getApiHeaders(requestHeadersSource) {
    const requestHeaders = typeof requestHeadersSource === "function"
        ? await requestHeadersSource()
        : await requestHeadersSource;
    const apiHeaders = new Headers();
    const normalizedHeaders = new Headers(requestHeaders ?? {});
    const cookie = normalizedHeaders.get("Cookie") ?? normalizedHeaders.get("cookie") ?? "";
    const authorization = normalizedHeaders.get("Authorization") ??
        normalizedHeaders.get("authorization") ??
        "";
    apiHeaders.set("Cookie", cookie);
    apiHeaders.set("Authorization", authorization);
    return apiHeaders;
}
//# sourceMappingURL=api.js.map