import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
// Keep this root component thin: wire top-level app state and render surfaces
// only. Put feature behavior in cli-v2 hooks, services, or focused components
// instead of growing App.tsx directly.
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Box, Text, useInput } from 'ink';
import { ApprovalPanel } from './components/ApprovalPanel.js';
import { AgentPlanPanel } from './components/AgentPlanPanel.js';
import { ComposerPanel } from './components/ComposerPanel.js';
import { CommandResultPanel } from './components/CommandResultPanel.js';
import { ConversationPanel } from './components/ConversationPanel.js';
import { DirectShellConfirmationPanel } from './components/DirectShellConfirmationPanel.js';
import { PromptStatusPanel } from './components/PromptStatusPanel.js';
import { QueuedPromptPanel } from './components/QueuedPromptPanel.js';
import { RecentEditDiffPanel } from './components/RecentEditDiffPanel.js';
import { RunControls } from './components/RunControls.js';
import { RuntimeStatusBar } from './components/RuntimeStatusBar.js';
import { useControlPlaneSessionStore } from './hooks/useControlPlaneSessionStore.js';
import { useRecentEditDiffReview } from './hooks/useRecentEditDiffReview.js';
import { PromptActivityService } from './services/activities/prompt-activity-service.js';
import { TuiLocalSlashCommandService } from './services/slash-commands/index.js';
import { ClientSharedSessionTurnPresentationService } from '../client-shared/services/session-turn-presentation/index.js';
export function App({ store, initialSelection, }) {
    const startedRef = useRef(false);
    const [activityExpanded, setActivityExpanded] = useState(false);
    const snapshot = useControlPlaneSessionStore(store);
    const recentEditDiffReview = useRecentEditDiffReview(snapshot.recentEditDiffs);
    const hasConversationActivityGroups = ClientSharedSessionTurnPresentationService
        .projectConversationTimeline(snapshot.activeSession)
        .some((item) => item.type === 'turn_activity_group');
    const hasCommandResults = snapshot.commandResults.some((result) => result.handled);
    useEffect(() => {
        if (startedRef.current) {
            return;
        }
        startedRef.current = true;
        void store.start(initialSelection);
        return () => {
            store.dispose();
        };
    }, [initialSelection, store]);
    const resolveApproval = useCallback((decision) => {
        void store.resolvePendingApproval(decision);
    }, [store]);
    const resolveDirectShellConfirmation = useCallback((accepted) => {
        void store.resolveDirectShellConfirmation(accepted);
    }, [store]);
    const cancelRun = useCallback(() => {
        void store.cancelRun();
    }, [store]);
    useInput((input, key) => {
        recentEditDiffReview.handleReviewKey(input, key);
    }, { isActive: recentEditDiffReview.mode === 'review' });
    const localSlashCommandHandlers = useMemo(() => ({
        activity: () => {
            if (!hasConversationActivityGroups) {
                store.showLocalCommandMessage('No agent tool activities are available to expand.');
                return;
            }
            setActivityExpanded((current) => !current);
        },
        commandResults: () => {
            if (!hasCommandResults) {
                store.showLocalCommandMessage('No command output is available to expand.');
                return;
            }
            store.toggleCommandResultExpanded();
        },
        diff: () => {
            if (!recentEditDiffReview.hasDiffs) {
                store.showLocalCommandMessage('No recent edit diffs are available to review.');
                return;
            }
            recentEditDiffReview.open();
        },
    }), [hasCommandResults, hasConversationActivityGroups, recentEditDiffReview, store]);
    const localSlashCommandHints = useMemo(() => TuiLocalSlashCommandService.hints(), []);
    const executeLocalSlashCommand = useCallback((command) => (TuiLocalSlashCommandService.execute(command, localSlashCommandHandlers)), [localSlashCommandHandlers]);
    return (_jsxs(Box, { flexDirection: "column", paddingX: 1, children: [_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { bold: true, children: "Heddle CLI V2" }), _jsx(Text, { dimColor: true, children: " \u00B7 API-backed terminal UI" })] }), _jsxs(Text, { dimColor: true, children: [snapshot.workspaceId ? `Workspace ${snapshot.workspaceId}` : 'Loading workspace...', snapshot.activeSession ? ` · ${snapshot.activeSession.name}` : ''] }), _jsx(ConversationPanel, { activityExpanded: activityExpanded, runtimeContext: snapshot.runtimeContext, session: snapshot.activeSession }), snapshot.running ? (_jsx(RecentEditDiffPanel, { diffs: snapshot.recentEditDiffs, review: recentEditDiffReview, running: snapshot.running })) : null, _jsx(CommandResultPanel, { expanded: snapshot.commandResultExpanded, results: snapshot.commandResults }), snapshot.pendingApproval ? (_jsx(ApprovalPanel, { approval: snapshot.pendingApproval, resolving: snapshot.approvalResolving, onResolve: resolveApproval })) : null, snapshot.pendingDirectShellConfirmation ? (_jsx(DirectShellConfirmationPanel, { confirmation: snapshot.pendingDirectShellConfirmation, onResolve: resolveDirectShellConfirmation })) : null, _jsx(RunControls, { running: snapshot.running, cancelling: snapshot.cancelling, keyboardDisabled: recentEditDiffReview.mode === 'review', onCancel: cancelRun }), _jsx(AgentPlanPanel, { plan: snapshot.activePlan }), _jsx(PromptStatusPanel, { currentActivity: snapshot.currentActivity, latestActivity: PromptActivityService.build(snapshot) }), _jsx(QueuedPromptPanel, { session: snapshot.activeSession }), _jsx(ComposerPanel, { store: store, snapshot: snapshot, keyboardDisabled: recentEditDiffReview.mode === 'review', localSlashCommandHints: localSlashCommandHints, onLocalSlashCommand: executeLocalSlashCommand }), _jsx(RuntimeStatusBar, { snapshot: snapshot })] }));
}
//# sourceMappingURL=App.js.map