import { expoClient } from "@better-auth/expo/client";
import { env } from "@code/env/native";
import { convexClient, crossDomainClient } from "@convex-dev/better-auth/client/plugins";
import { createAuthClient } from "better-auth/react";
import Constants from "expo-constants";
import * as SecureStore from "expo-secure-store";
import { Platform } from "react-native";

const scheme = Constants.expoConfig?.scheme;

if (typeof scheme !== "string") {
  throw new Error("Expo auth requires a string app scheme in app.json");
}

export const authClient = createAuthClient({
  baseURL: env.EXPO_PUBLIC_CONVEX_SITE_URL,
  plugins: [
    convexClient(),
    Platform.OS === "web"
      ? crossDomainClient()
      : expoClient({ scheme, storage: SecureStore, storagePrefix: scheme }),
  ],
});
