import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { QueryClient, QueryClientContext, QueryClientProvider, } from '@tanstack/react-query';
import { createContext, useContext, useEffect, useMemo, } from 'react';
import { useSeamClient } from './use-seam-client.js';
const defaultQueryClient = new QueryClient();
export function SeamQueryProvider({ children, onSessionUpdate = () => { }, queryClient, ...props }) {
    const value = useMemo(() => {
        const context = createSeamQueryContextValue(props);
        if (context.client == null &&
            context.publishableKey == null &&
            context.clientSessionToken == null &&
            context.consoleSessionToken == null) {
            return defaultSeamQueryContextValue;
        }
        return context;
    }, [props]);
    if (value.client == null &&
        value.publishableKey == null &&
        value.clientSessionToken == null &&
        value.consoleSessionToken == null) {
        throw new Error(`Must provide either a Seam client, clientSessionToken, publishableKey or consoleSessionToken.`);
    }
    const { Provider } = seamContext;
    const queryClientFromContext = useContext(QueryClientContext);
    if (queryClientFromContext != null &&
        queryClient != null &&
        queryClientFromContext !== queryClient) {
        throw new Error('The QueryClient passed into SeamQueryProvider is different from the one in the existing QueryClientContext. Omit the queryClient prop from SeamProvider or SeamQueryProvider to use the existing QueryClient provided by the QueryClientProvider.');
    }
    return (_jsx(QueryClientProvider, { client: queryClientFromContext ?? queryClient ?? defaultQueryClient, children: _jsx(Provider, { value: value, children: _jsx(Session, { onSessionUpdate: onSessionUpdate, children: children }) }) }));
}
function Session({ onSessionUpdate, children, }) {
    const { client } = useSeamClient();
    useEffect(() => {
        if (client != null)
            onSessionUpdate(client);
    }, [onSessionUpdate, client]);
    return _jsx(_Fragment, { children: children });
}
const createDefaultSeamQueryContextValue = () => {
    try {
        if (globalThis.seam == null) {
            return { client: null, endpointClient: null };
        }
        return createSeamQueryContextValue(globalThis.seam);
    }
    catch (err) {
        // eslint-disable-next-line no-console
        console.warn(err);
        return { client: null, endpointClient: null };
    }
};
const createSeamQueryContextValue = (options) => {
    if (isSeamQueryProviderPropsWithClient(options)) {
        if (options.queryKeyPrefix == null) {
            throw new InvalidSeamQueryProviderProps('The client prop must be used with a queryKeyPrefix prop.');
        }
        return {
            ...options,
            endpointClient: null,
        };
    }
    if (isSeamQueryProviderPropsWithClientSessionToken(options)) {
        const { clientSessionToken, ...clientOptions } = options;
        return {
            clientSessionToken,
            clientOptions,
            client: null,
            endpointClient: null,
        };
    }
    if (isSeamQueryProviderPropsWithPublishableKey(options)) {
        const { publishableKey, userIdentifierKey, ...clientOptions } = options;
        return {
            publishableKey,
            userIdentifierKey,
            clientOptions,
            client: null,
            endpointClient: null,
        };
    }
    if (isSeamQueryProviderPropsWithConsoleSessionToken(options)) {
        const { consoleSessionToken, workspaceId, ...clientOptions } = options;
        return {
            consoleSessionToken,
            workspaceId,
            clientOptions,
            client: null,
            endpointClient: null,
        };
    }
    return { client: null, endpointClient: null };
};
const defaultSeamQueryContextValue = createDefaultSeamQueryContextValue();
export const seamContext = createContext(defaultSeamQueryContextValue);
export function useSeamQueryContext() {
    return useContext(seamContext);
}
const isSeamQueryProviderPropsWithClient = (props) => {
    if (!('client' in props))
        return false;
    const { client, ...otherProps } = props;
    if (client == null)
        return false;
    const otherNonNullProps = Object.values(otherProps).filter((v) => v != null);
    if (otherNonNullProps.length > 0) {
        throw new InvalidSeamQueryProviderProps(`The client prop cannot be used with ${otherNonNullProps.join(' or ')}.`);
    }
    return true;
};
const isSeamQueryProviderPropsWithPublishableKey = (props) => {
    if (!('publishableKey' in props))
        return false;
    const { publishableKey } = props;
    if (publishableKey == null)
        return false;
    if ('client' in props && props.client != null) {
        throw new InvalidSeamQueryProviderProps('The client prop cannot be used with the publishableKey prop.');
    }
    if ('clientSessionToken' in props && props.clientSessionToken != null) {
        throw new InvalidSeamQueryProviderProps('The clientSessionToken prop cannot be used with the publishableKey prop.');
    }
    if ('consoleSessionToken' in props && props.consoleSessionToken != null) {
        throw new InvalidSeamQueryProviderProps('The consoleSessionToken prop cannot be used with the publishableKey prop.');
    }
    if ('workspaceId' in props && props.workspaceId != null) {
        throw new InvalidSeamQueryProviderProps('The workspaceId prop cannot be used with the publishableKey prop.');
    }
    return true;
};
const isSeamQueryProviderPropsWithClientSessionToken = (props) => {
    if (!('clientSessionToken' in props))
        return false;
    const { clientSessionToken } = props;
    if (clientSessionToken == null)
        return false;
    if ('client' in props && props.client != null) {
        throw new InvalidSeamQueryProviderProps('The client prop cannot be used with the clientSessionToken prop.');
    }
    if ('publishableKey' in props && props.publishableKey != null) {
        throw new InvalidSeamQueryProviderProps('The publishableKey prop cannot be used with the clientSessionToken prop.');
    }
    if ('userIdentifierKey' in props && props.userIdentifierKey != null) {
        throw new InvalidSeamQueryProviderProps('The userIdentifierKey prop cannot be used with the clientSessionToken prop.');
    }
    if ('consoleSessionToken' in props && props.consoleSessionToken != null) {
        throw new InvalidSeamQueryProviderProps('The consoleSessionToken prop cannot be used with the clientSessionToken prop.');
    }
    if ('workspaceId' in props && props.workspaceId != null) {
        throw new InvalidSeamQueryProviderProps('The workspaceId prop cannot be used with the clientSessionToken prop.');
    }
    return true;
};
const isSeamQueryProviderPropsWithConsoleSessionToken = (props) => {
    if (!('consoleSessionToken' in props))
        return false;
    const { consoleSessionToken } = props;
    if (consoleSessionToken == null)
        return false;
    if ('client' in props && props.client != null) {
        throw new InvalidSeamQueryProviderProps('The client prop cannot be used with the publishableKey prop.');
    }
    if ('clientSessionToken' in props && props.clientSessionToken != null) {
        throw new InvalidSeamQueryProviderProps('The clientSessionToken prop cannot be used with the publishableKey prop.');
    }
    if ('publishableKey' in props && props.publishableKey != null) {
        throw new InvalidSeamQueryProviderProps('The publishableKey prop cannot be used with the consoleSessionToken prop.');
    }
    if ('userIdentifierKey' in props && props.userIdentifierKey != null) {
        throw new InvalidSeamQueryProviderProps('The userIdentifierKey prop cannot be used with the consoleSessionToken prop.');
    }
    return true;
};
class InvalidSeamQueryProviderProps extends Error {
    constructor(message) {
        super(`SeamQueryProvider received invalid props: ${message}`);
        this.name = this.constructor.name;
    }
}
//# sourceMappingURL=SeamQueryProvider.js.map