import { removeResponseCommon, omitUser } from '../utils/index.mjs';
import { validateInDev } from '../utils/dev.mjs';

class HeadlessUserClient {
    _networkClient;
    _subscriptionService;
    constructor(_networkClient, _subscriptionService){
        this._networkClient = _networkClient;
        this._subscriptionService = _subscriptionService;
    }
    get = async ()=>{
        const resp = await this._networkClient.fetchSDK({
            url: '/users/me',
            method: 'GET'
        });
        const user = removeResponseCommon(resp);
        this._subscriptionService.updateUser(user);
        return user;
    };
    getSync = ()=>{
        return this._subscriptionService.getUser();
    };
    getInfo = ()=>({
            user: this.getSync(),
            fromCache: this._subscriptionService.getFromCache()
        });
    update = async (options)=>{
        validateInDev('stytch.user.update', options, {
            untrusted_metadata: 'optionalObject'
        });
        const resp = await this._networkClient.fetchSDK({
            url: '/users/me',
            body: options,
            method: 'PUT'
        });
        const user = removeResponseCommon(resp.__user);
        this._subscriptionService.updateUser(user);
        return omitUser(resp);
    };
    deleteEmail = async (emailId)=>{
        const resp = await this._networkClient.fetchSDK({
            url: `/users/emails/${emailId}`,
            method: 'DELETE'
        });
        const user = removeResponseCommon(resp.__user);
        this._subscriptionService.updateUser(user);
        return omitUser(resp);
    };
    deletePhoneNumber = async (phoneId)=>{
        const resp = await this._networkClient.fetchSDK({
            url: `/users/phone_numbers/${phoneId}`,
            method: 'DELETE'
        });
        const user = removeResponseCommon(resp.__user);
        this._subscriptionService.updateUser(user);
        return omitUser(resp);
    };
    deleteTOTP = async (totpId)=>{
        const resp = await this._networkClient.fetchSDK({
            url: `/users/totps/${totpId}`,
            method: 'DELETE'
        });
        const user = removeResponseCommon(resp.__user);
        this._subscriptionService.updateUser(user);
        return omitUser(resp);
    };
    deleteCryptoWallet = async (cryptoWalletId)=>{
        const resp = await this._networkClient.fetchSDK({
            url: `/users/crypto_wallets/${cryptoWalletId}`,
            method: 'DELETE'
        });
        const user = removeResponseCommon(resp.__user);
        this._subscriptionService.updateUser(user);
        return omitUser(resp);
    };
    deleteOAuthRegistration = async (oauthUserRegistrationId)=>{
        const resp = await this._networkClient.fetchSDK({
            url: `/users/oauth/${oauthUserRegistrationId}`,
            method: 'DELETE'
        });
        const user = removeResponseCommon(resp.__user);
        this._subscriptionService.updateUser(user);
        return omitUser(resp);
    };
    deleteWebauthnRegistration = async (webAuthnId)=>{
        const resp = await this._networkClient.fetchSDK({
            url: `/users/webauthn_registrations/${webAuthnId}`,
            method: 'DELETE'
        });
        const user = removeResponseCommon(resp.__user);
        this._subscriptionService.updateUser(user);
        return omitUser(resp);
    };
    deleteBiometricRegistration = async (biometricRegistrationId)=>{
        const resp = await this._networkClient.fetchSDK({
            url: `/users/biometric_registrations/${biometricRegistrationId}`,
            method: 'DELETE'
        });
        const user = removeResponseCommon(resp.__user);
        this._subscriptionService.updateUser(user);
        return omitUser(resp);
    };
    onChange = (callback)=>{
        let lastVal = this._subscriptionService.getUser();
        const listener = (state)=>{
            if (state?.user !== lastVal) {
                lastVal = state?.user ?? null;
                callback(lastVal);
            }
        };
        return this._subscriptionService.subscribeToState(listener);
    };
    getConnectedApps = async ()=>{
        return await this._networkClient.fetchSDK({
            url: '/users/connected_apps',
            method: 'GET'
        });
    };
    revokedConnectedApp = async (connectedAppId)=>{
        return await this._networkClient.fetchSDK({
            url: `/users/connected_apps/${connectedAppId}/revoke`,
            method: 'POST'
        });
    };
}

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