import { useMemo } from 'react';
import { collator } from './collator.mjs';
import { useOrgInfo } from './useOrgInfo.mjs';
import { useSsoConnections } from './useSsoConnections.mjs';

const useConnections = (canGetSso)=>{
    const ssoConnections = useSsoConnections({
        shouldFetch: canGetSso
    });
    const orgInfo = useOrgInfo();
    const defaultConnectionId = orgInfo.data?.sso_default_connection_id;
    const connections = useMemo(()=>{
        return ssoConnections.data ? [
            ...ssoConnections.data.saml_connections.map((c)=>({
                    connectionType: 'saml',
                    ...c
                })),
            ...ssoConnections.data.oidc_connections.map((c)=>({
                    connectionType: 'oidc',
                    ...c
                })),
            ...ssoConnections.data.external_connections.map((c)=>({
                    connectionType: 'external',
                    ...c
                }))
        ].map((connection)=>{
            return {
                displayName: connection.display_name,
                idp: 'identity_provider' in connection ? connection.identity_provider : undefined,
                connectionType: connection.connectionType,
                status: connection.status,
                id: connection.connection_id,
                isDefault: connection.connection_id === defaultConnectionId
            };
        }).sort((a, b)=>collator.compare(a.displayName, b.displayName)) : [];
    }, [
        defaultConnectionId,
        ssoConnections.data
    ]);
    return {
        connections,
        isLoading: ssoConnections.isLoading
    };
};

export { useConnections };
//# sourceMappingURL=useConnections.mjs.map
