import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
/**
 * @license
 * Copyright 2025 Vybestack LLC
 * SPDX-License-Identifier: Apache-2.0
 */
import { useCallback, useEffect, useState } from 'react';
import { Box, Text } from 'ink';
import { MessageBusType, DebugLogger, } from '@vybestack/llxprt-code-core';
import { RadioButtonSelect, } from './shared/RadioButtonSelect.js';
import { Colors } from '../colors.js';
import { useKeypress } from '../hooks/useKeypress.js';
const logger = new DebugLogger('llxprt:bucket:confirmation:ui');
/**
 * Component that listens for bucket auth confirmation requests
 * and shows an approval dialog in the TUI.
 */
export const BucketAuthConfirmation = ({ config, isFocused = true, }) => {
    const [pendingRequest, setPendingRequest] = useState(null);
    // Subscribe to bucket auth confirmation requests
    useEffect(() => {
        const messageBus = config.getMessageBus();
        logger.debug('BucketAuthConfirmation useEffect running', {
            hasMessageBus: !!messageBus,
        });
        if (!messageBus) {
            logger.debug('No message bus available, skipping subscription');
            return;
        }
        logger.debug('Subscribing to BUCKET_AUTH_CONFIRMATION_REQUEST');
        const unsubscribe = messageBus.subscribe(MessageBusType.BUCKET_AUTH_CONFIRMATION_REQUEST, (request) => {
            logger.debug('Received bucket auth confirmation request', {
                provider: request.provider,
                bucket: request.bucket,
                bucketIndex: request.bucketIndex,
                totalBuckets: request.totalBuckets,
            });
            setPendingRequest({
                correlationId: request.correlationId,
                provider: request.provider,
                bucket: request.bucket,
                bucketIndex: request.bucketIndex,
                totalBuckets: request.totalBuckets,
            });
        });
        return () => {
            logger.debug('Unsubscribing from BUCKET_AUTH_CONFIRMATION_REQUEST');
            unsubscribe();
        };
    }, [config]);
    const handleConfirm = useCallback((confirmed) => {
        if (!pendingRequest)
            return;
        const messageBus = config.getMessageBus();
        if (messageBus) {
            messageBus.respondToBucketAuthConfirmation(pendingRequest.correlationId, confirmed);
        }
        setPendingRequest(null);
    }, [config, pendingRequest]);
    // Handle escape key to cancel
    useKeypress((key) => {
        if (!pendingRequest || !isFocused)
            return;
        if (key.name === 'escape' || (key.ctrl && key.name === 'c')) {
            handleConfirm(false);
        }
    }, { isActive: !!pendingRequest && isFocused });
    const handleSelect = useCallback((option) => {
        handleConfirm(option === 'proceed');
    }, [handleConfirm]);
    // Don't render anything if no pending request
    if (!pendingRequest) {
        return null;
    }
    const options = [
        {
            label: 'Yes, open browser',
            value: 'proceed',
            key: 'proceed',
        },
        {
            label: 'Cancel (esc)',
            value: 'cancel',
            key: 'cancel',
        },
    ];
    return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: Colors.AccentCyan, padding: 1, marginY: 1, children: [_jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: Colors.AccentCyan, bold: true, children: "OAuth Bucket Authentication" }) }), _jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsxs(Text, { children: ["Bucket ", pendingRequest.bucketIndex, " of ", pendingRequest.totalBuckets, ":", ' ', _jsx(Text, { color: Colors.AccentGreen, children: pendingRequest.bucket })] }), _jsxs(Text, { dimColor: true, children: ["Provider: ", pendingRequest.provider] })] }), _jsx(Box, { marginBottom: 1, children: _jsx(Text, { color: Colors.AccentGreen, children: "Open browser to authenticate this bucket?" }) }), _jsx(RadioButtonSelect, { items: options, onSelect: handleSelect, isFocused: isFocused })] }));
};
//# sourceMappingURL=BucketAuthConfirmation.js.map