import { initializePaddle, type Paddle } from "@paddle/paddle-js";

const paddleToken = import.meta.env.VITE_PADDLE_CLIENT_TOKEN ?? "";
const paddleEnvironment = (import.meta.env.VITE_PADDLE_ENV ?? "sandbox").toLowerCase() === "live" ? "production" : "sandbox";

let paddlePromise: Promise<Paddle | undefined> | null = null;

export function paddleConfigured() {
  return paddleToken.trim() !== "";
}

export async function getPaddle() {
  if (!paddleConfigured()) {
    return undefined;
  }
  if (!paddlePromise) {
    paddlePromise = initializePaddle({
      environment: paddleEnvironment,
      token: paddleToken,
    });
  }
  return paddlePromise;
}
