import * as react_jsx_runtime from 'react/jsx-runtime';
import { QueryClientConfig, DefaultOptions, QueryClient } from '@tanstack/react-query';

/**
 * API module types
 */
interface ApiClientContextValue {
    getAccessToken: () => Promise<string | undefined>;
    /** @deprecated Read via getOrganizationId() instead. Kept for downstream context consumers during migration. */
    organizationId: string | null;
    isOrganizationReady: boolean;
    getOrganizationId: () => string | null;
    onError?: (endpoint: string, error: Error, details?: ApiErrorDetails) => void;
}
interface ApiErrorDetails {
    method: string;
    statusCode: number;
    requestId?: string;
}
interface ApiClientProviderProps {
    children: React.ReactNode;
    getAccessToken: () => Promise<string | undefined>;
    /**
     * Static org ID. Used when org ID is stable at render time.
     * Cannot reflect org switches without re-rendering the provider.
     * Prefer getOrganizationId for dynamic org context.
     * @deprecated Pass getOrganizationId instead for dynamic org resolution.
     */
    organizationId?: string | null;
    /**
     * Callback invoked on every request to read the current org ID.
     * Takes precedence over organizationId when provided.
     * Allows org switching without re-rendering the provider tree.
     */
    getOrganizationId?: () => string | null;
    isOrganizationReady: boolean;
    onError?: (endpoint: string, error: Error, details?: ApiErrorDetails) => void;
}

/**
 * Hook to access the API client context
 * Must be used within an ApiClientProvider
 */
declare function useApiClientContext(): ApiClientContextValue;
/**
 * Provider component that configures the API client context
 *
 * Configure this at your app root with auth and organization dependencies.
 * All components using useApiClient will automatically get the configured values.
 *
 * @example
 * ```tsx
 * function App() {
 *   const { getAccessToken } = useAuth()
 *   const { currentWorkOSOrganizationId, currentMembership } = useOrganizations()
 *
 *   return (
 *     <ApiClientProvider
 *       getAccessToken={getAccessToken}
 *       organizationId={currentWorkOSOrganizationId}
 *       isOrganizationReady={!!currentMembership}
 *       onError={(endpoint, error, details) => logError(endpoint, error, details)}
 *     >
 *       <YourApp />
 *     </ApiClientProvider>
 *   )
 * }
 * ```
 */
declare function ApiClientProvider({ children, getAccessToken, organizationId, getOrganizationId: getOrganizationIdProp, isOrganizationReady, onError }: ApiClientProviderProps): react_jsx_runtime.JSX.Element;

/**
 * Hook that returns apiRequest and deferredApiRequest bound to the current
 * ApiClientContext. apiUrl is the only parameter because org ID is resolved
 * at call time via getOrganizationId() from context.
 *
 * When OrganizationProvider is present in the tree, isOrganizationReady and
 * isInitializing reflect the live org state (currentWorkOSOrganizationId,
 * isInitializing, isOrgRefreshing). When OrganizationProvider is absent
 * (e.g. theme-only mode, testing), the hook falls back to the values from
 * ApiClientContext.
 *
 * Usage:
 * ```typescript
 * import { useApiClient } from '@repo/ui/api'
 * import { API_URL } from '@/lib/constants/api'
 *
 * function useMyClient() {
 *   return useApiClient(API_URL)
 * }
 * ```
 */
declare function useApiClient(apiUrl: string): {
    apiRequest: <T>(endpoint: string, options?: RequestInit) => Promise<T>;
    deferredApiRequest: <T>(endpoint: string, options?: RequestInit) => Promise<T>;
    isOrganizationReady: boolean;
    isInitializing: boolean;
};

interface CreateElevasisQueryClientOptions extends Omit<QueryClientConfig, 'defaultOptions'> {
    defaultOptions?: DefaultOptions;
    onMutationError?: NonNullable<DefaultOptions['mutations']>['onError'];
}
declare function createElevasisQueryClient(options?: CreateElevasisQueryClientOptions): QueryClient;

export { ApiClientProvider, createElevasisQueryClient, useApiClient, useApiClientContext };
export type { ApiClientContextValue, ApiClientProviderProps, ApiErrorDetails, CreateElevasisQueryClientOptions };
