import { useLingui } from '../../../../../../node_modules/@lingui/react/dist/index.mjs';
import * as React from 'react';
import Button from '../../components/atoms/Button.mjs';
import Column from '../../components/atoms/Column.mjs';
import Typography from '../../components/atoms/Typography.mjs';
import LastUsed from '../../components/molecules/LastUsed.mjs';
import { SSOButton } from '../components/SSOButton.mjs';
import { useGlobalReducer } from '../GlobalContextProvider.mjs';
import { useLastUsedAuthMethod } from '../hooks/useLastUsedAuthMethod.mjs';
import { extractConnectionId, getSsoMethodKey } from '../types/authMethodKeys.mjs';
import { arrayUtils } from '../../../../../core/src/utils/arrayUtils.mjs';

const SSOButtons = ({ availableConnections })=>{
    const [lastUsedMethod, setLastUsedMethod] = useLastUsedAuthMethod();
    const lastUsedSso = extractConnectionId(lastUsedMethod);
    const [connections, foundLastUsed] = arrayUtils.moveToFront(availableConnections, (connection)=>connection.connection_id === lastUsedSso);
    return /*#__PURE__*/ React.createElement(Column, {
        gap: 2
    }, connections.map((provider, index)=>{
        const button = /*#__PURE__*/ React.createElement(SSOButton, {
            key: provider.display_name,
            connection: provider,
            onStart: (connection)=>setLastUsedMethod(getSsoMethodKey(connection))
        });
        return foundLastUsed && index === 0 ? /*#__PURE__*/ React.createElement(LastUsed, {
            key: provider.display_name
        }, button) : button;
    }));
};
const SSODiscoveryMenu = ()=>{
    const [state, dispatch] = useGlobalReducer();
    const { i18n: $__i18n, _: $__ } = useLingui();
    const handleBack = ()=>{
        dispatch({
            type: 'navigate_back'
        });
    };
    return /*#__PURE__*/ React.createElement(Column, {
        gap: 6
    }, /*#__PURE__*/ React.createElement(Typography, {
        variant: "header"
    }, $__i18n._({
        id: "ssoDiscovery.title",
        message: "Select a connection to continue"
    })), /*#__PURE__*/ React.createElement(Column, {
        gap: 4
    }, /*#__PURE__*/ React.createElement(SSOButtons, {
        availableConnections: state.formState.ssoDiscoveryState.connections
    }), /*#__PURE__*/ React.createElement(Button, {
        variant: "ghost",
        onClick: handleBack
    }, $__i18n._({
        id: "button.goBack",
        message: "Go back"
    }))));
};

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