import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useEffect, useState } from "react";
import { Box, Text, useInput } from "ink";
import { computeComposerLayout } from "../layout.js";
import { randomRunStatusSeed, runStatusParts } from "../runStatus.js";
export function Composer(props) {
    const [frameIndex, setFrameIndex] = useState(0);
    const [runningSince, setRunningSince] = useState(null);
    const [verbSeed, setVerbSeed] = useState(() => randomRunStatusSeed());
    const prompt = props.isApprovalWaiting ? "input > " : props.isRunning ? "run   > " : "patch > ";
    const layout = computeComposerLayout({
        input: props.input,
        width: props.width,
        promptWidth: prompt.length
    });
    useEffect(() => {
        if (!props.isRunning) {
            setRunningSince(null);
            setFrameIndex(0);
            return;
        }
        setRunningSince((currentValue) => {
            if (currentValue === null) {
                setVerbSeed(randomRunStatusSeed());
                return Date.now();
            }
            return currentValue;
        });
        const timer = setInterval(() => {
            setFrameIndex((currentValue) => (currentValue + 1) % spinnerFrames.length);
        }, 120);
        return () => {
            clearInterval(timer);
        };
    }, [props.isRunning]);
    useInput((inputValue, key) => {
        if (props.isRunning || props.isApprovalWaiting) {
            return;
        }
        if (key.return) {
            if (key.shift || key.meta || key.super) {
                props.onChange(`${props.input}\n`);
                return;
            }
            props.onSubmit(props.input);
            return;
        }
        if (key.backspace || key.delete) {
            props.onChange(props.input.slice(0, -1));
            return;
        }
        if (key.tab || key.escape || key.upArrow || key.downArrow || key.leftArrow || key.rightArrow || key.pageUp || key.pageDown || key.home || key.end) {
            return;
        }
        if (key.ctrl || inputValue.length === 0) {
            return;
        }
        props.onChange(`${props.input}${inputValue}`);
    }, {
        isActive: !props.isRunning && !props.isApprovalWaiting
    });
    const elapsedSeconds = runningSince ? Math.max(0, Math.floor((Date.now() - runningSince) / 1000)) : 0;
    const runStatus = runStatusParts({
        workState: props.workState,
        status: props.status,
        elapsedMs: elapsedSeconds * 1000,
        seed: verbSeed
    });
    const renderedRows = props.mask ? layout.visibleRows.map((row) => props.mask?.repeat(row.length) ?? row) : layout.visibleRows;
    const placeholder = props.input.length === 0 ? "Ask PatchPilot or type /help..." : "";
    return (_jsxs(Box, { flexDirection: "column", height: layout.height, overflowY: "hidden", children: [_jsx(Box, { height: layout.editorRows, flexDirection: "column", children: props.isApprovalWaiting || props.isRunning ? (_jsxs(Box, { height: 1, children: [_jsx(Text, { color: props.isApprovalWaiting ? "yellow" : "cyan", children: prompt }), props.isApprovalWaiting ? (_jsx(Text, { color: "yellow", children: "approval waiting" })) : (_jsxs(Text, { color: "yellow", children: [spinnerFrames[frameIndex], " ", runStatus.verb, "  \u00B7  ", runStatus.state, runStatus.detail ? ` · ${runStatus.detail}` : "", _jsx(Text, { color: "gray", children: elapsedSeconds > 0 ? `  ${elapsedSeconds}s` : "  starting" })] }))] })) : renderedRows.map((row, index) => {
                    const isLastRow = index === renderedRows.length - 1;
                    const text = row || (isLastRow ? placeholder : "");
                    return (_jsxs(Box, { height: 1, children: [_jsx(Text, { color: "cyan", children: index === 0 ? prompt : " ".repeat(prompt.length) }), _jsxs(Text, { color: placeholder && isLastRow ? "gray" : "white", children: [text, isLastRow ? _jsx(Text, { inverse: true, children: " " }) : null] })] }, `composer-row-${index}`));
                }) }), _jsx(Text, { color: "gray", wrap: "truncate", children: props.isApprovalWaiting ? "Approval waiting: press y once, a session, or n deny." : props.isRunning ? "Input is locked while the current run is active." : `prompt ${props.draftTokens} tok est${layout.hiddenRows > 0 ? `  ${layout.hiddenRows} earlier draft line${layout.hiddenRows === 1 ? "" : "s"}` : ""}  Enter sends  Shift/Meta+Enter newline` })] }));
}
export function FooterHints(props) {
    return (_jsx(Box, { height: 1, overflowY: "hidden", children: _jsxs(Text, { color: "gray", children: ["tab plan/build/bypass  |  pane ", props.activePane, "  |  left/right pane  |  pgup/pgdn scroll  |  / starts palette"] }) }));
}
const spinnerFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
//# sourceMappingURL=Composer.js.map