import React__default, { useState, useCallback } from 'react';
import { Modal } from './Modal.mjs';
import { Typography } from './Typography.mjs';
import { useToggleState } from '../shared/utils/useToggleState.mjs';
import { useMutateOrganization } from '../utils/useMutateOrganization.mjs';

const SetDefaultConfirmModal = ({ connectionName, defaultConnectionName, ...modalProps })=>{
    return /*#__PURE__*/ React__default.createElement(Modal, {
        ...modalProps,
        title: `Set ${connectionName || ''} as default?`,
        confirmButtonText: "Set as default"
    }, /*#__PURE__*/ React__default.createElement(Typography, null, "This will be the default SSO connection used to automatically initiate authentication for your Members."), defaultConnectionName && /*#__PURE__*/ React__default.createElement(Typography, {
        color: "error"
    }, "This will replace ", defaultConnectionName, " as your default SSO connection."));
};
const useMutateDefaultConnection = ()=>{
    const { mutate } = useMutateOrganization();
    const setDefault = async ({ connectionId, connectionName })=>{
        try {
            await mutate({
                sso_default_connection_id: connectionId
            }, {
                errorMessage: `Failed to set ${connectionName} as default connection`
            });
        } catch  {
        // no-op; error toast is handled by `mutate` call above
        }
    };
    return {
        setDefault
    };
};
const useSetDefaultConnection = ({ defaultConnectionName })=>{
    const [connection, setConnection] = useState();
    const [cachedDefaultConnectionName, setCachedDefaultConnectionName] = useState(defaultConnectionName);
    const { isOpen, open, close } = useToggleState();
    const handleRequestSetDefault = useCallback((connection)=>{
        setConnection(connection);
        setCachedDefaultConnectionName(defaultConnectionName);
        open();
    }, [
        defaultConnectionName,
        open
    ]);
    const { setDefault } = useMutateDefaultConnection();
    const confirm = async ()=>{
        if (connection) {
            setDefault({
                connectionId: connection.id,
                connectionName: connection.displayName
            });
        }
        return Promise.resolve();
    };
    return {
        handleRequestSetDefault,
        modalProps: {
            connectionName: connection?.displayName,
            defaultConnectionName: cachedDefaultConnectionName,
            isOpen,
            close,
            confirm
        }
    };
};

export { SetDefaultConfirmModal, useMutateDefaultConnection, useSetDefaultConnection };
//# sourceMappingURL=SetDefaultConnection.mjs.map
