import { w as waitForConfig, c as configState } from './config-state.js';
import { g as generateId } from './utils2.js';

const Api = () => {
    async function getAuthorizationHeader(authToken) {
        if (!authToken) {
            return {
                'Content-Type': 'application/json',
            };
        }
        return {
            Authorization: `Bearer ${authToken}`,
            'Idempotency-Key': generateId(),
            'Content-Type': 'application/json',
        };
    }
    async function makeRequest(props) {
        var _a, _b;
        const { authToken, endpoint, method, params, body, signal, headers } = props;
        await waitForConfig();
        const apiOrigin = configState.apiOrigin;
        const url = `${apiOrigin}/v1/${endpoint}`;
        const requestUrl = params ? `${url}?${new URLSearchParams(params)}` : url;
        const defaultHeaders = await getAuthorizationHeader(authToken);
        const mergedHeaders = Object.assign(Object.assign({}, defaultHeaders), headers);
        const response = await fetch(requestUrl, {
            method: method,
            headers: mergedHeaders,
            body: body,
            signal: signal,
        });
        if (!response) {
            handleError(requestUrl);
            return;
        }
        if (response.status === 204) {
            return {};
        }
        const parsed = await response.json().catch(() => ({}));
        if (!response.ok) {
            const err = new Error(((_a = parsed === null || parsed === void 0 ? void 0 : parsed.error) === null || _a === void 0 ? void 0 : _a.message)
                || (typeof (parsed === null || parsed === void 0 ? void 0 : parsed.error) === 'string' ? parsed.error : null)
                || (parsed === null || parsed === void 0 ? void 0 : parsed.message)
                || `Request failed: ${response.status}`);
            err.code = (_b = parsed === null || parsed === void 0 ? void 0 : parsed.error) === null || _b === void 0 ? void 0 : _b.code;
            err.status = response.status;
            err.body = parsed;
            throw err;
        }
        return parsed;
    }
    async function get(props) {
        const { authToken, endpoint, params, signal, headers } = props;
        return makeRequest({ authToken, endpoint, method: 'GET', params, signal, headers });
    }
    async function post(props) {
        const { authToken, endpoint, body, params, signal, headers } = props;
        return makeRequest({
            authToken,
            endpoint,
            method: 'POST',
            params,
            body: JSON.stringify(body),
            signal,
            headers,
        });
    }
    async function put(props) {
        const { authToken, endpoint, body, params, signal } = props;
        return makeRequest({
            authToken,
            endpoint,
            method: 'PUT',
            params,
            body: JSON.stringify(body),
            signal,
        });
    }
    async function patch(props) {
        const { authToken, endpoint, body, params, signal } = props;
        return makeRequest({
            authToken,
            endpoint,
            method: 'PATCH',
            params,
            body: JSON.stringify(body),
            signal,
        });
    }
    async function destroy(props) {
        const { authToken, endpoint, params, signal } = props;
        return makeRequest({ authToken, endpoint, method: 'DELETE', params, signal });
    }
    return { get, post, put, patch, destroy };
};
function handleError(requestUrl) {
    console.error(`Error fetching from ${requestUrl}`);
}

export { Api as A };
