import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { Box, Text } from 'ink';
import React from 'react';
import { getLogger } from '../utils/logging/index.js';
/**
 * Isolates a single rendered chat/live item so a throw during its render
 * (most commonly a malformed model tool-call arg reaching JSX — e.g. an object
 * where a string was expected) degrades to one inline error line instead of
 * tearing down the whole Ink tree. Without this, any such bug crashes the TUI.
 *
 * Must be a class component: error boundaries require getDerivedStateFromError.
 */
export class RenderErrorBoundary extends React.Component {
    state = { message: null };
    static getDerivedStateFromError(error) {
        return {
            message: error instanceof Error ? error.message : String(error),
        };
    }
    componentDidCatch(error) {
        try {
            getLogger().error('Failed to render chat item', {
                label: this.props.label,
                error: error instanceof Error ? error.message : String(error),
            });
        }
        catch {
            // The boundary must never throw — swallow logging failures.
        }
    }
    render() {
        if (this.state.message !== null) {
            const what = this.props.label ? ` ${this.props.label}` : '';
            return (_jsx(Box, { children: _jsxs(Text, { color: "yellow", children: ["\u26A0 Could not render", what, " output (", this.state.message, ")"] }) }));
        }
        return this.props.children;
    }
}
//# sourceMappingURL=render-error-boundary.js.map