import { createClient } from '@supabase/supabase-js' const supabaseUrl = import.meta.env.VITE_SUPABASE_URL const supabaseAnonKey = import.meta.env.VITE_SUPABASE_ANON_KEY const hasCredentials = supabaseUrl && supabaseAnonKey && supabaseUrl !== 'undefined' && supabaseAnonKey !== 'undefined'; // Safe Chainable Thenable Promise to prevent React crashing on arbitrary queries const createChainablePromise = (resolvedValue) => { const promise = Promise.resolve(resolvedValue); const chain = { select: () => chain, order: () => chain, eq: () => chain, then: (onFulfilled, onRejected) => promise.then(onFulfilled, onRejected), catch: (onRejected) => promise.catch(onRejected) }; return chain; }; // Safe Mock to allow sandbox-only local operations if DB keys aren't set const mockSupabase = { from: () => ({ select: () => createChainablePromise({ data: [], error: null }), insert: () => createChainablePromise({ data: null, error: new Error("Supabase credentials not configured in environment.") }), update: () => ({ eq: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }) }), delete: () => ({ eq: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }) }) }), storage: { from: () => ({ upload: () => createChainablePromise({ data: null, error: new Error("Supabase credentials not configured.") }), remove: () => createChainablePromise({ error: new Error("Supabase credentials not configured.") }), getPublicUrl: () => ({ data: { publicUrl: "" } }) }) } }; export const supabase = hasCredentials ? createClient(supabaseUrl, supabaseAnonKey) : mockSupabase;