var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { jsx as _jsx } from "react/jsx-runtime";
import { useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router';
import { HTTP_METHOD } from '@darajs/ui-utils';
import { request } from '../../api/http';
import DefaultFallback from '../../components/fallback/default';
import { useRouterContext } from '../../router/context';
import Center from '../../shared/center/center';
import { handleAuthErrors } from '../auth';
/**
 * Makes a call to /sso-callback and returns the post-auth redirect path.
 * Returns null on auth failure.
 *
 * @param search current search string
 */
export function getSSOCallbackResult(search, defaultPath) {
    return __awaiter(this, void 0, void 0, function* () {
        var _a;
        try {
            const params = new URLSearchParams(search);
            const state = params.get('state');
            const res = yield request('/api/auth/sso-callback', {
                body: JSON.stringify({
                    auth_code: params.get('code'),
                    state,
                }),
                method: HTTP_METHOD.POST,
            });
            const shouldLogOut = yield handleAuthErrors(res);
            if (shouldLogOut) {
                return null;
            }
            if (res.ok) {
                const responseData = (yield res.json());
                return {
                    redirectTo: (_a = responseData.redirect_to) !== null && _a !== void 0 ? _a : defaultPath,
                };
            }
            throw new Error(`${res.status}: ${res.statusText}`);
        }
        catch (_b) {
            return null;
        }
    });
}
function OIDCAuthSSOCallback() {
    const { search } = useLocation();
    const navigate = useNavigate();
    const routerContext = useRouterContext();
    useEffect(() => {
        getSSOCallbackResult(search, routerContext.defaultPath)
            .then((result) => {
            if (result) {
                navigate(result.redirectTo);
            }
        })
            .catch((err) => {
            // eslint-disable-next-line no-console
            console.error('Failed to run SSO callback', err);
            navigate('/logout');
        });
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    return (_jsx(Center, { children: _jsx(DefaultFallback, {}) }));
}
export default OIDCAuthSSOCallback;
//# sourceMappingURL=sso-callback.js.map