import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import { Text } from "ink";
import { cellWidth, createPromptViewportDisplay } from "../prompt/promptViewport.js";
import { takeCells } from "./design/textLayout.js";
export function BaseTextInput(props) {
    return (_jsxs(Text, { children: [props.model.prefixHidden && _jsx(Text, { color: "gray", children: "..." }), props.model.placeholder ? (_jsxs(_Fragment, { children: [_jsx(Text, { color: "cyan", children: props.model.cursor }), _jsx(Text, { color: "gray", children: props.model.placeholder })] })) : (_jsxs(_Fragment, { children: [_jsx(Text, { children: props.model.before }), _jsx(Text, { color: "cyan", children: props.model.cursor }), _jsx(Text, { children: props.model.after }), props.model.argumentHint ? _jsx(Text, { color: "gray", children: props.model.argumentHint }) : null] })), props.model.suffixHidden && _jsx(Text, { color: "gray", children: "..." }), _jsx(Text, { children: props.model.padding })] }));
}
export function baseTextInputModel(input) {
    const width = Math.max(1, Math.floor(input.width));
    const showCursor = input.showCursor ?? true;
    const cursor = showCursor ? "|" : "";
    if (!input.value && input.placeholder) {
        const placeholder = takeCells(input.placeholder, Math.max(0, width - cellWidth(cursor)));
        return {
            before: "",
            after: "",
            cursor,
            prefixHidden: false,
            suffixHidden: false,
            padding: " ".repeat(Math.max(0, width - cellWidth(cursor) - cellWidth(placeholder))),
            placeholder,
            argumentHint: "",
        };
    }
    const viewport = createPromptViewportDisplay(input.value, input.cursor, width);
    const rawArgumentHint = input.argumentHint && shouldShowArgumentHint(input.value)
        ? `${input.value.endsWith(" ") ? "" : " "}${input.argumentHint}`
        : "";
    const baseWidth = (viewport.prefixHidden ? 3 : 0) +
        cellWidth(viewport.before) +
        cellWidth(cursor) +
        cellWidth(viewport.after) +
        (viewport.suffixHidden ? 3 : 0);
    const argumentHint = takeCells(rawArgumentHint, Math.max(0, width - baseWidth));
    return {
        before: viewport.before,
        after: viewport.after,
        cursor,
        prefixHidden: viewport.prefixHidden,
        suffixHidden: viewport.suffixHidden,
        padding: " ".repeat(Math.max(0, width - baseWidth - cellWidth(argumentHint))),
        placeholder: "",
        argumentHint,
    };
}
function shouldShowArgumentHint(value) {
    if (!value.startsWith("/"))
        return false;
    const trimmed = value.trim();
    return trimmed.length > 0 && (trimmed.indexOf(" ") === -1 || value.endsWith(" "));
}
