import { useLingui } from '../../../../../../node_modules/@lingui/react/dist/index.mjs';
import React__default, { useReducer, useEffect, useCallback } from 'react';
import Button from '../atoms/Button.mjs';
import Column from '../atoms/Column.mjs';
import Typography from '../atoms/Typography.mjs';
import ButtonColumn from '../molecules/ButtonColumn.mjs';
import { Confirmation } from '../molecules/Confirmation.mjs';
import { ErrorDisplay } from '../molecules/ErrorScreen.mjs';
import { LoadingScreen } from '../molecules/Loading.mjs';
import { IDPOAuthFlowMissingParamError, StytchAPIError } from '../../../../../core/src/public/SDKErrors.mjs';

const initialState = (error)=>({
        loading: !error,
        preflight: !!error,
        error: error,
        denied: false,
        consent_required: false,
        redirect_uri: ''
    });
const logoutStateReducer = (state, action)=>{
    switch(action.type){
        case 'START_PREFLIGHT':
            return {
                ...state,
                preflight: true,
                loading: true
            };
        case 'PREFLIGHT_ERROR':
            return {
                ...state,
                preflight: true,
                loading: false,
                error: action.error
            };
        case 'COMPLETE_PREFLIGHT':
            return {
                ...state,
                loading: false,
                consent_required: action.consent_required,
                redirect_uri: action.redirect_uri
            };
        case 'CONSENT_DENIED':
            return {
                ...state,
                denied: true
            };
        default:
            return state;
    }
};
const browserNavigate = (href)=>{
    window.location.href = href;
};
const useLogoutState = ({ initialError, initialParams, oauthLogoutStart, revokeSession, onError, navigate = browserNavigate })=>{
    const [state, dispatch] = useReducer(logoutStateReducer, initialState(initialError));
    useEffect(()=>{
        if (!initialError) return;
        onError(new IDPOAuthFlowMissingParamError(initialError));
    }, [
        initialError,
        onError
    ]);
    const submitInternal = useCallback(async (redirectURI)=>{
        try {
            await revokeSession();
        } catch  {
        // Session may already be revoked--in any event, we still want to
        // redirect the user
        }
        navigate(redirectURI);
    }, [
        revokeSession,
        navigate
    ]);
    const logoutPreflight = useCallback(async ()=>{
        dispatch({
            type: 'START_PREFLIGHT'
        });
        let startResponse;
        try {
            startResponse = await oauthLogoutStart(initialParams);
        } catch (e) {
            const err = StytchAPIError.from(e);
            const message = err.error_message;
            dispatch({
                type: 'PREFLIGHT_ERROR',
                error: message
            });
            onError(err);
            return;
        }
        if (startResponse.consent_required) {
            dispatch({
                type: 'COMPLETE_PREFLIGHT',
                consent_required: startResponse.consent_required,
                redirect_uri: startResponse.redirect_uri
            });
            return;
        } else {
            submitInternal(startResponse.redirect_uri);
        }
    }, [
        initialParams,
        oauthLogoutStart,
        onError,
        submitInternal
    ]);
    useEffect(()=>{
        if (state.preflight) {
            return;
        }
        logoutPreflight();
    }, [
        logoutPreflight,
        state.preflight
    ]);
    const submit = useCallback(async ({ consent_granted })=>{
        if (consent_granted) {
            return submitInternal(state.redirect_uri);
        } else {
            dispatch({
                type: 'CONSENT_DENIED'
            });
        }
    }, [
        submitInternal,
        state
    ]);
    return {
        state,
        submit
    };
};
const IDPLogout = ({ state, submit, bootstrap, bootstrapNotLoaded })=>{
    const { i18n: $__i18n, _: $__ } = useLingui();
    const { loading, error, denied } = state;
    if (loading || bootstrapNotLoaded) {
        return /*#__PURE__*/ React__default.createElement(LoadingScreen, null);
    }
    if (error) {
        return /*#__PURE__*/ React__default.createElement(ErrorDisplay, {
            text: error
        });
    }
    if (denied) {
        return /*#__PURE__*/ React__default.createElement(Confirmation, {
            text: $__i18n._({
                id: "idpLogout.notLoggedOut",
                message: "You have not been logged out. You may close this page."
            })
        });
    }
    const projectName = bootstrap.projectName;
    return /*#__PURE__*/ React__default.createElement(Column, {
        gap: 6
    }, /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "header",
        align: "center"
    }, $__i18n._({
        id: "idpLogout.title",
        message: "Log out?"
    })), /*#__PURE__*/ React__default.createElement(Typography, {
        variant: "body"
    }, $__i18n._({
        id: "idpLogout.confirmMessage",
        message: "Are you sure you want to log out of your {projectName} account?",
        values: {
            projectName: projectName
        }
    })), /*#__PURE__*/ React__default.createElement(ButtonColumn, null, /*#__PURE__*/ React__default.createElement(Button, {
        variant: "primary",
        onClick: ()=>submit({
                consent_granted: true
            })
    }, $__i18n._({
        id: "idpLogout.yes",
        message: "Yes"
    })), /*#__PURE__*/ React__default.createElement(Button, {
        onClick: ()=>submit({
                consent_granted: false
            }),
        variant: "ghost"
    }, $__i18n._({
        id: "idpLogout.no",
        message: "No"
    }))));
};

export { IDPLogout, useLogoutState };
//# sourceMappingURL=IDPLogout.mjs.map
