import { startAuthentication, startRegistration } from "@simplewebauthn/browser";
import { startPaymentRequest } from "./secure-payment.js";
export async function authenticate(options) {
    if (options.payment) {
        if (!options.authenticatorType) {
            options = {
                ...options,
                authenticatorType: "payment"
            };
        }
    }
    if (!options.authenticatorType) {
        options = {
            ...options,
            authenticatorType: "credential"
        };
    }
    const { registration, authentication, payment } = options.response ?? await generateOptions(options);
    console.log(options, { registration, authentication, payment });
    let response;
    if (options.register || (!authentication && !payment)) {
        if (!registration) {
            const message = "Can only register new credentials on original device";
            alert(message);
            throw new Error(message);
        }
        response = await verify({
            registration: await startRegistration(registration.options),
            state: registration.state
        });
    }
    else if (payment) {
        const paymentPartialOptions = typeof options.payment !== "boolean" ? options.payment : {};
        const paymentOptions = {
            ...payment.options,
            ...paymentPartialOptions,
            data: {
                ...payment.options.data,
                ...paymentPartialOptions?.data,
                instrument: {
                    ...payment.options.data.instrument,
                    ...paymentPartialOptions?.data?.instrument
                },
            },
            details: {
                ...payment.options.details,
                ...paymentPartialOptions.details,
                total: {
                    ...payment.options.details.total,
                    ...paymentPartialOptions.details?.total
                }
            }
        };
        response = await verify({
            payment: await startPaymentRequest(paymentOptions),
            state: authentication.state
        });
    }
    else {
        response = await verify({
            authentication: await startAuthentication(authentication.options),
            state: authentication.state
        });
    }
    if (options.redirect !== false) {
        if (response?.verified && response?.redirectUrl) {
            location.href = response.redirectUrl;
        }
    }
    return response;
}
async function generateOptions(options) {
    const response = await fetch("/api/authentication/webauthn/generate-options", {
        method: "POST",
        body: JSON.stringify(options),
        headers: {
            "Content-Type": "application/json"
        }
    });
    return response.json();
}
async function verify(options) {
    const response = await fetch("/api/authentication/webauthn/verify", {
        method: "POST",
        body: JSON.stringify(options),
        headers: {
            "Content-Type": "application/json"
        }
    });
    return response.json();
}
//# sourceMappingURL=webauthn.js.map