import * as React from "react";
/**
 * Component-level error boundary for ui-sdk surfaces. Catches render-phase
 * exceptions in children and renders a localised fallback instead of crashing
 * the whole mount. Storefront callers typically wrap individual surfaces
 * (SearchResults, Facets, etc.) so a single broken component doesn't drop the
 * entire chrome to Shopify's native fallback. SCRUM-268.
 */
export class SeekoraErrorBoundary extends React.Component {
    constructor() {
        super(...arguments);
        this.state = { error: null };
        this.reset = () => {
            this.setState({ error: null });
        };
    }
    static getDerivedStateFromError(error) {
        return { error };
    }
    componentDidCatch(error, info) {
        if (typeof console !== "undefined") {
            // eslint-disable-next-line no-console
            console.error(`[Seekora] ErrorBoundary caught in ${this.props.surface ?? "<unknown surface>"}:`, error, info?.componentStack);
        }
        try {
            this.props.onError?.(error, info);
        }
        catch {
            // never throw out of the boundary
        }
    }
    render() {
        const { error } = this.state;
        if (error) {
            if (this.props.fallback) {
                try {
                    return this.props.fallback(error, this.reset);
                }
                catch {
                    // fall through to default if custom fallback also throws
                }
            }
            return (React.createElement("div", { className: "seekora-error-boundary", "data-seekora-surface": this.props.surface, role: "alert" },
                React.createElement("div", { className: "seekora-error-boundary__message" }, "Something went wrong rendering this section."),
                React.createElement("button", { type: "button", className: "seekora-error-boundary__retry", onClick: this.reset }, "Try again")));
        }
        return this.props.children;
    }
}
