import { dev } from "$app/environment";
import * as env from "$env/static/public";
import PocketBase, { LocalAuthStore } from "pocketbase";
import type { TypedPocketBase } from "./pocketbase-types";
export type { ClientResponseError } from "pocketbase";

const forcePublicAuthStore =
	"PUBLIC_PROD_AUTH_STORE" in env &&
	typeof env.PUBLIC_PROD_AUTH_STORE === "string" &&
	["true", "t", "yes", "y", "1"].includes(env.PUBLIC_PROD_AUTH_STORE.toLowerCase());
export const pb = new PocketBase(
	undefined,
	dev && !forcePublicAuthStore
		? new LocalAuthStore("__pb_superuser_auth__") // When in development, and not forcing the prod auth store, we just hijack the PocketBase Admin UI session
		: new LocalAuthStore("cctv_auth")
) as TypedPocketBase;

export function tw(s: TemplateStringsArray, ...args: unknown[]) {
	return s.reduce((acc, str, idx) => acc + str + (args[idx] || ""), "");
}
