try {
	let e = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof globalThis ? globalThis : "undefined" != typeof self ? self : {}, n = new e.Error().stack;
	n && (e._sentryDebugIds = e._sentryDebugIds || {}, e._sentryDebugIds[n] = "4fb65bc5-2efc-4ba0-ba3d-41af11535a9b", e._sentryDebugIdIdentifier = "sentry-dbid-4fb65bc5-2efc-4ba0-ba3d-41af11535a9b");
} catch (e) {}
import { o as createLogger } from "./translation-Br5wIPO-.js";
import { useContext } from "preact/hooks";
import { jsx } from "preact/jsx-runtime";
import { signal } from "@preact/signals";
import { createContext } from "preact";
//#region src/context/ApiContext/ApiProvider.tsx
function createApiState(id, url) {
	return {
		rootLegalEntityId: signal(id),
		baseUrl: signal(`${signal(url).value}onboardingcomponents/api/v1/`)
	};
}
var ApiContext = createContext(null);
var useApiContext = () => {
	return useContext(ApiContext);
};
/**
* Creates and provides the apiState, which contains the baseUrl and the legalEntity
* which are necessary toconstruct the url in API calls
*
* @param {string} legalEntityId - legalEntityId
* @param {string} baseUrl - base url
*/
var ApiProvider = ({ rootLegalEntityId, baseUrl, children }) => {
	return /* @__PURE__ */ jsx(ApiContext.Provider, {
		value: createApiState(rootLegalEntityId, baseUrl),
		children
	});
};
//#endregion
//#region src/core/services/session.ts
var sdkToken;
var fetchSdkToken;
var logger = createLogger();
var setSdkToken = (token) => {
	sdkToken = token;
};
var setSdkTokenHandler = (handler) => {
	fetchSdkToken = handler;
};
var getSdkToken = () => sdkToken;
var refreshSession = async () => {
	let isSessionRefreshed = false;
	if (!fetchSdkToken) throw Error("Not able to extend session, handler not provided. Make sure to provide getSdkToken handler");
	try {
		const { token } = await fetchSdkToken();
		if (!token) throw Error("Not able to extend session, token not received. Make sure to provide back the token in the getSdkToken response ");
		setSdkToken(token);
		isSessionRefreshed = true;
	} catch (e) {
		logger.error("Failed to fetch sdk token", e);
	}
	return isSessionRefreshed;
};
//#endregion
//#region src/core/services/utils.ts
var getRequestObject = (options, data) => {
	const { method = "GET" } = options;
	const isFormData = data instanceof FormData;
	return {
		method,
		mode: "cors",
		cache: "default",
		credentials: "same-origin",
		headers: {
			Accept: "application/json, text/plain, */*",
			Authorization: `Bearer ${getSdkToken()}`,
			Origin: window.location.origin,
			...data && !isFormData ? { "Content-Type": "application/json" } : {}
		},
		redirect: "follow",
		referrerPolicy: "no-referrer-when-downgrade",
		...data && { body: isFormData ? data : JSON.stringify(data) }
	};
};
var handleFetchResponse = async (response, responseType) => {
	if (response.status === 204) return;
	switch (responseType) {
		case "arraybuffer": return response.arrayBuffer();
		case "blob": return response.blob();
		case "json": return response.json();
		case "text": return response.text();
		default: break;
	}
};
var isValidationErrorResponse = (response) => response?.status === 422;
var isSessionExpiredResponse = (response) => response?.status === 401;
var processValidationErrors = async (response) => {
	const responseData = await response.json();
	throw new ValidationError(responseData.errorCode, responseData);
};
var ValidationError = class extends Error {
	invalidFields;
	errorCode;
	sourceError;
	constructor(message, validationDetails) {
		super(message);
		this.invalidFields = validationDetails?.invalidFields;
		this.errorCode = validationDetails?.errorCode;
		this.sourceError = validationDetails;
	}
};
//#endregion
//#region src/core/services/http.ts
var http = async (options, data, responseType = "json") => {
	const { baseUrl = "", path, errorHandler } = options;
	const request = getRequestObject(options, data);
	const url = `${baseUrl}${path}`;
	let response;
	const logger = createLogger();
	try {
		response = await fetch(url, request);
	} catch (error) {
		const errorMessage = `Service at ${url} is not available. Error= ${error}`;
		logger.warn(errorMessage);
		throw new Error(errorMessage);
	}
	const errorMessage = `Call to ${url} failed.`;
	if (!response) {
		logger.warn(errorMessage);
		throw new Error(errorMessage);
	}
	if (response.ok) return handleFetchResponse(response, responseType);
	if (isSessionExpiredResponse(response)) {
		if (await refreshSession()) return http(options, data, responseType);
	}
	if (errorHandler) return errorHandler(handleFetchResponse(response, responseType));
	if (isValidationErrorResponse(response)) return processValidationErrors(response);
	logger.warn(errorMessage);
	throw new Error(errorMessage);
};
var httpGet = async (options, data) => http({
	...options,
	method: "GET"
}, data);
var httpPost = async (options, data) => http({
	...options,
	method: "POST"
}, data);
var httpDelete = async (options, data) => http({
	...options,
	method: "DELETE"
}, data);
//#endregion
export { setSdkTokenHandler as a, refreshSession as i, httpGet as n, ApiProvider as o, httpPost as r, useApiContext as s, httpDelete as t };
