import { Container, Text } from "@earendil-works/pi-tui";
import { DynamicBorder } from "./components/dynamic_border.js";
import { HeaderLineComponent } from "./components/header_line.js";
export function inlineText(text) {
    return text.replace(/\s+/g, " ").trim();
}
function buildHeaderSegments(spec) {
    const bullet = spec.bullet ?? "▪";
    return [
        { text: " ", style: (s) => s },
        { text: bullet, style: spec.bulletStyle },
        { text: " ", style: (s) => s },
        { text: spec.label, style: spec.labelStyle },
        { text: " ", style: (s) => s },
        { text: spec.accent, style: spec.accentStyle ?? ((s) => s) },
    ];
}
export function buildSection(lines) {
    const filtered = lines.filter((line) => Boolean(line?.trim()));
    return filtered.length > 0 ? filtered.join("\n") : undefined;
}
export function buildExpandedText(expanded) {
    const sections = expanded.sections ?? [];
    const filteredSections = sections.filter((section) => Boolean(section));
    const parts = [expanded.title, ...filteredSections];
    return parts.join("\n\n");
}
function styleToolUiLine(line, theme, baseStyle) {
    if (!line.tone)
        return baseStyle(line.text);
    switch (line.tone) {
        case "diffAdd":
            return theme.palette.diffAdd(line.text);
        case "diffRemove":
            return theme.palette.diffRemove(line.text);
        default:
            return baseStyle(line.text);
    }
}
export function renderToolUiTextLines(args) {
    const { uiText, kind, theme, baseStyle } = args;
    const lines = kind === "preview" ? uiText.previewLines : uiText.fullLines;
    if (!lines.some((line) => line.text.trim()))
        return undefined;
    return lines.map((line) => styleToolUiLine(line, theme, baseStyle)).join("\n");
}
export const TOOL_UI_INDENT = 4;
const DEFAULT_STATUS_WRAPPER = (text) => `(${text})`;
export function buildToolHeaderLine(spec) {
    return { segments: buildHeaderSegments(spec) };
}
export function renderToolUiCompactText(args) {
    const { uiText, theme, previewStyle, statusStyle, indent = TOOL_UI_INDENT, statusWrapper = DEFAULT_STATUS_WRAPPER, } = args;
    const pad = " ".repeat(Math.max(0, indent));
    const statusText = uiText.statusLine?.trim();
    const formattedStatus = statusText ? `${pad}${statusWrapper(statusText)}` : undefined;
    const indentedUiText = {
        ...uiText,
        previewLines: uiText.previewLines.map((line) => ({
            ...line,
            text: `${pad}${line.text}`,
        })),
        statusLine: formattedStatus,
    };
    const compactParts = [];
    const previewText = renderToolUiTextLines({
        uiText: indentedUiText,
        kind: "preview",
        theme,
        baseStyle: previewStyle,
    });
    if (previewText) {
        compactParts.push(previewText);
    }
    if (formattedStatus) {
        compactParts.push(statusStyle(formattedStatus));
    }
    return compactParts.length > 0 ? compactParts.join("\n") : undefined;
}
export class ToolOutputComponent extends Container {
    constructor(props) {
        super();
        this.update(props);
    }
    update(props) {
        this.clear();
        if (props.compact) {
            const { headerComponent, extraText, extraComponent, paddingX, paddingY } = props.compactView;
            if (headerComponent) {
                this.addChild(headerComponent);
            }
            if (extraComponent) {
                this.addChild(extraComponent);
            }
            else if (extraText && extraText.trim() !== "") {
                this.addChild(new Text(extraText, paddingX ?? 0, paddingY ?? 0));
            }
            return;
        }
        const { borderColor, text, paddingX, paddingY } = props.expanded;
        this.addChild(new DynamicBorder(borderColor));
        this.addChild(new Text(text, paddingX ?? 1, paddingY ?? 0));
        this.addChild(new DynamicBorder(borderColor));
    }
}
function isComponent(value) {
    return typeof value.render === "function";
}
export function renderToolOutput(view, compact) {
    return new ToolOutputComponent(buildToolOutputProps(view, compact));
}
export function buildBlockedToolView(args) {
    const { theme, label, title, accent, reason } = args;
    const { palette, text } = theme;
    const errorColor = (s) => palette.actionError(s);
    const msg = reason.trim();
    const section = buildSection(msg ? [errorColor(msg)] : []);
    const accentInline = inlineText(accent);
    const whyInline = inlineText(reason);
    const header = buildToolHeaderLine({
        bulletStyle: errorColor,
        bullet: "✗",
        label,
        labelStyle: palette.textMuted,
        accent: accentInline,
        accentStyle: palette.brandAccent,
    });
    return {
        borderColor: errorColor,
        expanded: {
            title: errorColor(text.bold(title)),
            sections: section ? [section] : [],
        },
        compact: {
            header,
            extraText: whyInline ? `    ${errorColor(whyInline)}` : undefined,
        },
    };
}
export function buildToolOutputProps(view, compact) {
    const header = view.compact.header === undefined
        ? undefined
        : isComponent(view.compact.header)
            ? view.compact.header
            : new HeaderLineComponent(view.compact.header);
    return {
        compact,
        expanded: {
            borderColor: view.borderColor,
            text: buildExpandedText(view.expanded),
            paddingX: view.expanded.paddingX,
            paddingY: view.expanded.paddingY,
        },
        compactView: {
            headerComponent: header,
            extraText: view.compact.extraText,
            extraComponent: view.compact.extraComponent,
            paddingX: view.compact.paddingX,
            paddingY: view.compact.paddingY,
        },
    };
}
//# sourceMappingURL=tool_output.js.map