import { Container, ProcessTerminal, setKeybindings, TUI, VERSION, FooterDataProvider, KeybindingsManager, CustomEditor, FooterComponent, UsageMeterComponent, getEditorTheme, setRegisteredThemes, InteractiveThemeController } from "./interactive-mode-deps.js";
function isCommandLikeStartupInput(text) {
    const trimmed = text.trimStart();
    return trimmed.startsWith("/") || trimmed.startsWith("!");
}
export function seedStartupInput(pendingUserInputs, editor, startupInput, startupReplayInputs = [], setStartupDraftText, setStartupReplayActiveInput) {
    if (!startupInput)
        return;
    let commandReplayStarted = false;
    for (const submission of startupInput.submissions) {
        if (commandReplayStarted) {
            startupReplayInputs.push(submission);
        }
        else if (isCommandLikeStartupInput(submission)) {
            const commandText = submission.trim();
            commandReplayStarted = true;
            editor.setText(commandText);
            setStartupReplayActiveInput?.(commandText);
        }
        else {
            pendingUserInputs.push(submission);
        }
    }
    if (startupInput.text.length === 0)
        return;
    if (commandReplayStarted) {
        setStartupDraftText?.(startupInput.text);
    }
    else {
        editor.setText(startupInput.text);
    }
}
export class InteractiveModeBase {
    // Convenience accessors
    get session() {
        return this.runtimeHost.session;
    }
    get agent() {
        return this.session.agent;
    }
    get sessionManager() {
        return this.session.sessionManager;
    }
    get settingsManager() {
        return this.session.settingsManager;
    }
    constructor(runtimeHost, options = {}) {
        this.autocompleteProviderWrappers = [];
        this.isInitialized = false;
        // GitHub Copilot CAPI context-window catalog load state (gated on the Copilot provider).
        this.copilotCatalogApplied = false;
        this.pendingUserInputs = [];
        this.startupReplayInputs = [];
        this.startupReplayActiveInput = undefined;
        this.startupDraftText = undefined;
        this.startupCookedInputRecovered = false;
        this.deferredRenderedUserInputs = [];
        this.deferredRenderedUserInputComponents = new Map();
        this.loadingAnimation = undefined;
        this.workingMessage = undefined;
        this.workingVisible = true;
        this.workingIndicatorOptions = undefined;
        this.defaultWorkingMessage = "Working...";
        this.defaultHiddenThinkingLabel = "Thinking...";
        this.hiddenThinkingLabel = this.defaultHiddenThinkingLabel;
        this.lastSigintTime = 0;
        this.lastEscapeTime = 0;
        this.changelogMarkdown = undefined;
        this.startupNoticesShown = false;
        this.startupNoticesPrepared = false;
        this.anthropicSubscriptionWarningShown = false;
        this.firstRunNoticeVisible = false;
        this.hadLastChangelogVersionAtStartup = false;
        this.firstRunOnboardingNoticeComponents = [];
        // Status line tracking (for mutating immediately-sequential status updates)
        this.lastStatusSpacer = undefined;
        this.lastStatusText = undefined;
        // Streaming message tracking
        this.streamingComponent = undefined;
        this.streamingMessage = undefined;
        // Tool execution tracking: toolCallId -> component
        this.pendingTools = new Map();
        // Tool output expansion state
        this.toolOutputExpanded = false;
        // Thinking block visibility state
        this.hideThinkingBlock = false;
        this.outputPad = 1;
        // Skill commands: command name -> skill file path
        this.skillCommands = new Map();
        this.signalCleanupHandlers = [];
        // Track if editor is in bash mode (text starts with !)
        this.isBashMode = false;
        // Track current bash execution component
        this.bashComponent = undefined;
        // Track pending bash components (shown in pending area, moved to chat on submit)
        this.pendingBashComponents = [];
        // Auto-compaction state
        this.autoCompactionLoader = undefined;
        // Auto-retry state
        this.retryLoader = undefined;
        this.fallbackLoader = undefined;
        this.retryCountdown = undefined;
        // Messages queued while compaction is running
        this.compactionQueuedMessages = [];
        // Deferred extension load state (first paint happens before extensions load)
        this.deferredStartupPending = false;
        this.deferredStartupPromise = undefined;
        this.inputHandlerReadyRecorded = false;
        this.firstSubmitRecorded = false;
        this.deferLoadedResourcesDisclosureUntilAgentEnd = false;
        this.pendingLoadedResourcesDisclosure = false;
        // Shutdown state
        this.shutdownRequested = false;
        // Extension UI state
        this.extensionSelector = undefined;
        this.extensionInput = undefined;
        this.extensionEditor = undefined;
        this.extensionTerminalInputUnsubscribers = new Set();
        this.blockingInlineCustomUiDepth = 0;
        this.deferredInlineCustomUiFocusDepth = 0;
        this.pendingInlineCustomUiFocus = undefined;
        this.hostCustomUiStateListeners = new Set();
        // Extension widgets (components rendered above/below the editor)
        this.extensionWidgetsAbove = new Map();
        this.extensionWidgetsBelow = new Map();
        // Custom footer from extension (undefined = use built-in footer)
        this.customFooter = undefined;
        // Built-in header (logo + keybinding hints + changelog)
        this.builtInHeader = undefined;
        // Custom header from extension (undefined = use built-in header)
        this.customHeader = undefined;
        /**
         * Gracefully shutdown the agent.
         * Stops the TUI before emitting shutdown events so extension UI cleanup cannot
         * repaint the final frame while the process is exiting.
         */
        this.isShuttingDown = false;
        this.options = options;
        this.deferredStartupPending = Boolean(options.deferredExtensionLoad);
        this.autoTrustOnReloadCwd = options.autoTrustOnReloadCwd;
        this.runtimeHost = runtimeHost;
        this.runtimeHost.setBeforeSessionInvalidate(() => {
            this.resetExtensionUI();
        });
        this.runtimeHost.setRebindSession(async () => {
            await this.rebindCurrentSession();
        });
        this.version = VERSION;
        this.ui = new TUI(new ProcessTerminal(), this.settingsManager.getShowHardwareCursor());
        this.ui.setClearOnShrink(this.settingsManager.getClearOnShrink());
        this.headerContainer = new Container();
        this.chatContainer = new Container();
        this.startupNoticesContainer = new Container();
        this.pendingMessagesContainer = new Container();
        this.statusContainer = new Container();
        this.widgetContainerAbove = new Container();
        this.widgetContainerBelow = new Container();
        this.keybindings = KeybindingsManager.create();
        setKeybindings(this.keybindings);
        const editorPaddingX = this.settingsManager.getEditorPaddingX();
        const autocompleteMaxVisible = this.settingsManager.getAutocompleteMaxVisible();
        this.defaultEditor = new CustomEditor(this.ui, getEditorTheme(), this.keybindings, {
            paddingX: editorPaddingX,
            autocompleteMaxVisible,
        });
        this.editor = this.defaultEditor;
        this.editorContainer = new Container();
        this.editorContainer.addChild(this.editor);
        this.footerDataProvider = new FooterDataProvider(this.sessionManager.getCwd());
        this.footer = new FooterComponent(this.session, this.footerDataProvider);
        this.usageMeter = new UsageMeterComponent(this.session);
        this.usageMeter.setAutoCompactEnabled(this.session.autoCompactionEnabled);
        // Load hide thinking block setting
        this.hideThinkingBlock = this.settingsManager.getHideThinkingBlock();
        this.outputPad = this.settingsManager.getOutputPad();
        // Register themes from resource loader and initialize
        setRegisteredThemes(this.session.resourceLoader.getThemes().themes);
        this.themeController = new InteractiveThemeController(this.ui, this.settingsManager, (message) => this.showError(message), () => this.updateEditorBorderColor());
    }
    // Maximum total widget lines to prevent viewport overflow
    static { this.MAX_WIDGET_LINES = 10; }
}
//# sourceMappingURL=interactive-mode-base.js.map