import configuration from "@intlayer/config/built";
import { passkeyClient } from "@better-auth/passkey/client";
import { ssoClient } from "@better-auth/sso/client";
import { createAuthClient } from "better-auth/client";
import { magicLinkClient, twoFactorClient } from "better-auth/client/plugins";

//#region src/libs/auth.ts
const getAuthClient = (backendURL) => createAuthClient({
	baseURL: backendURL,
	withCredentials: true,
	plugins: [
		twoFactorClient({ onTwoFactorRedirect: () => {
			const redirectUrl = new URLSearchParams(window.location.search).get("redirect_url");
			const twoFaUrl = redirectUrl ? `/auth/2fa?redirect_url=${encodeURIComponent(redirectUrl)}` : "/auth/2fa";
			window.location.href = twoFaUrl;
		} }),
		passkeyClient(),
		magicLinkClient(),
		ssoClient()
	]
});
const getAuthAPI = (intlayerConfig) => {
	const backendURL = intlayerConfig?.editor?.backendURL ?? configuration?.editor?.backendURL;
	if (!backendURL) throw new Error("Backend URL is not defined in the Intlayer configuration.");
	const client = getAuthClient(backendURL);
	const signInEmail = async (...args) => {
		return client.signIn.email(...args);
	};
	const signInSocial = async (...args) => {
		return client.signIn.social(...args);
	};
	const signUpEmail = async (...args) => {
		return client.signUp.email(...args);
	};
	const signOut = async (...args) => {
		return client.signOut(...args);
	};
	const changePasswordSession = async (...args) => {
		return client.changePassword(...args);
	};
	const requestPasswordResetSession = async (...args) => {
		return client.requestPasswordReset(...args);
	};
	const resetPassword = async (...args) => {
		return client.resetPassword(...args);
	};
	const verifyEmailSession = async (...args) => {
		return client.verifyEmail(...args);
	};
	const getSession = async (...args) => {
		return client.getSession(...args);
	};
	const forgetPassword = async (...args) => {
		return client.requestPasswordReset(...args);
	};
	const sendVerificationEmail = async (...args) => {
		return client.sendVerificationEmail(...args);
	};
	const changeEmail = async (...args) => {
		return client.changeEmail(...args);
	};
	const deleteUser = async (...args) => {
		return client.deleteUser(...args);
	};
	const revokeSession = async (...args) => {
		return client.revokeSession(...args);
	};
	const revokeSessions = async (...args) => {
		return client.revokeSessions(...args);
	};
	const revokeOtherSessions = async (...args) => {
		return client.revokeOtherSessions(...args);
	};
	const linkSocial = async (...args) => {
		return client.linkSocial(...args);
	};
	const listAccounts = async (...args) => {
		return client.listAccounts(...args);
	};
	const unlinkAccount = async (...args) => {
		return client.unlinkAccount(...args);
	};
	const refreshToken = async (...args) => {
		return client.refreshToken(...args);
	};
	const getAccessToken = async (...args) => {
		return client.getAccessToken(...args);
	};
	const accountInfo = async (...args) => {
		return client.accountInfo(...args);
	};
	const updateUser = async (...args) => {
		return client.updateUser(...args);
	};
	const listSessions = async (...args) => {
		return client.listSessions(...args);
	};
	const enableTwoFactor = async (...args) => {
		return client.twoFactor.enable(...args);
	};
	const disableTwoFactor = async (...args) => {
		return client.twoFactor.disable(...args);
	};
	const verifyTotp = async (...args) => {
		return client.twoFactor.verifyTotp(...args);
	};
	const verifyBackupCode = async (...args) => {
		return client.twoFactor.verifyBackupCode(...args);
	};
	const addPasskey = async (...args) => {
		return client.passkey.addPasskey(...args);
	};
	const signInPasskey = async (...args) => {
		return client.signIn.passkey(...args);
	};
	const deletePasskey = async (...args) => {
		return client.passkey.deletePasskey(...args);
	};
	const listPasskeys = async () => {
		return client.$fetch("/passkey/list-user-passkeys", { method: "GET" });
	};
	const signInMagicLink = async (...args) => {
		return client.signIn.magicLink(...args);
	};
	const signInSSO = async (...args) => {
		return client.signIn.sso(...args);
	};
	const registerSSO = async (...args) => {
		return client.sso.register(...args);
	};
	const listSSOProviders = async () => {};
	const deleteSSOProvider = async (args) => {};
	return {
		getAuthClient: () => client,
		signInEmail,
		signUpEmail,
		signOut,
		signInSocial,
		linkSocial,
		changePasswordSession,
		requestPasswordResetSession,
		resetPassword,
		verifyEmailSession,
		getSession,
		forgetPassword,
		sendVerificationEmail,
		changeEmail,
		deleteUser,
		revokeSession,
		revokeSessions,
		revokeOtherSessions,
		listAccounts,
		unlinkAccount,
		refreshToken,
		getAccessToken,
		accountInfo,
		updateUser,
		listSessions,
		enableTwoFactor,
		disableTwoFactor,
		verifyTotp,
		verifyBackupCode,
		addPasskey,
		signInPasskey,
		deletePasskey,
		listPasskeys,
		signInMagicLink,
		signInSSO,
		registerSSO,
		listSSOProviders,
		deleteSSOProvider
	};
};

//#endregion
export { getAuthAPI };
//# sourceMappingURL=auth.mjs.map