import { UNRECOVERABLE_ERROR_TYPES } from '../../public/SDKErrors.mjs';

class HeadlessB2BSessionClient {
    _networkClient;
    _subscriptionService;
    getSync = ()=>{
        return this._subscriptionService.getSession();
    };
    getInfo = ()=>({
            session: this.getSync(),
            fromCache: this._subscriptionService.getFromCache()
        });
    onChange = (callback)=>{
        return this._subscriptionService.subscribeToState((state)=>callback(state?.session ?? null));
    };
    revoke = async (options)=>{
        /**
     * Revoke destroys the local state if the API request is successful
     * or if we return an unrecoverable error (user is unauthenticated)
     * or if the developer passes in a forceClear option.
     * If the API request returns a recoverable error (the user is offline),
     * we do not destroy the local state to let the developer manually add retry
     * logic to call revoke again.
     */ try {
            const resp = await this._networkClient.fetchSDK({
                url: `/b2b/sessions/revoke`,
                method: 'POST'
            });
            this._subscriptionService.destroyState();
            return resp;
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        } catch (error) {
            if (options?.forceClear) {
                this._subscriptionService.destroyState();
            } else if (UNRECOVERABLE_ERROR_TYPES.includes(error.error_type)) {
                this._subscriptionService.destroyState();
            }
            throw error;
        }
    };
    revokeForMember = async (options)=>{
        return await this._networkClient.fetchSDK({
            url: `/b2b/sessions/revoke/${options.member_id}`,
            method: 'POST'
        });
    };
    _authenticate = async (options)=>{
        const initialSession = this._subscriptionService.getSession();
        const isSessionStale = ()=>initialSession?.member_session_id !== this._subscriptionService.getSession()?.member_session_id;
        try {
            const requestBody = {
                session_duration_minutes: options?.session_duration_minutes
            };
            const resp = await this._networkClient.fetchSDK({
                url: '/b2b/sessions/authenticate',
                body: requestBody,
                method: 'POST'
            });
            if (isSessionStale()) {
                // [SDK-1336] The session was updated out from under us while the
                // request was in flight; discard the response and retry
                return this._authenticate(options);
            }
            return resp;
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        } catch (error) {
            if (isSessionStale()) {
                // [SDK-1336] The session was updated out from under us while the
                // request was in flight; discard the response and retry
                return this._authenticate(options);
            }
            if (UNRECOVERABLE_ERROR_TYPES.includes(error.error_type)) {
                this._subscriptionService.destroySession();
            }
            throw error;
        }
    };
    authenticate;
    exchange;
    exchangeAccessToken;
    attest;
    constructor(_networkClient, _subscriptionService){
        this._networkClient = _networkClient;
        this._subscriptionService = _subscriptionService;
        this.authenticate = this._subscriptionService.withUpdateSession(this._authenticate);
        this.exchange = this._subscriptionService.withUpdateSession(async (data)=>{
            return this._networkClient.fetchSDK({
                url: '/b2b/sessions/exchange',
                body: data,
                method: 'POST'
            });
        });
        this.exchangeAccessToken = this._subscriptionService.withUpdateSession(async (data)=>{
            return this._networkClient.fetchSDK({
                url: '/b2b/sessions/exchange_access_token',
                body: data,
                method: 'POST'
            });
        });
        this.attest = this._subscriptionService.withUpdateSession(async (data)=>{
            return this._networkClient.fetchSDK({
                url: '/b2b/sessions/attest',
                body: data,
                method: 'POST'
            });
        });
    }
    getTokens() {
        return this._subscriptionService.getTokens();
    }
    updateSession(tokens) {
        this._subscriptionService.updateTokens(tokens);
    }
}

export { HeadlessB2BSessionClient };
//# sourceMappingURL=HeadlessB2BSessionClient.mjs.map
