/*! ratio-ui · design system for knowledge sharing · https://github.com/losol/ratio-ui · (c) 2026 Losol AS · MPL-2.0 */
import { ErrorBlock } from "../../blocks/Error/Error.js";
import "../../blocks/Error/index.js";
import React from "react";
import { jsx, jsxs } from "react/jsx-runtime";
//#region src/core/ErrorBoundary/ErrorBoundary.tsx
function changed(a = [], b = []) {
	if (a.length !== b.length) return true;
	return a.some((v, i) => !Object.is(v, b[i]));
}
/**
* Default fallback used when `<ErrorBoundary>` is rendered without a `fallback`
* prop. Shows an inline `ErrorBlock` with a "Try again" button.
*
* The raw `error.message` is intentionally not rendered — it can include
* implementation details or user data. Write a custom `fallback` if you need
* to surface details (typically gated behind a dev flag).
*/
function DefaultFallback({ reset }) {
	return /* @__PURE__ */ jsxs(ErrorBlock, {
		type: "generic",
		status: "error",
		children: [
			/* @__PURE__ */ jsx(ErrorBlock.Title, { children: "Something went wrong" }),
			/* @__PURE__ */ jsx(ErrorBlock.Description, { children: "This part of the page couldn't be displayed." }),
			/* @__PURE__ */ jsx(ErrorBlock.Actions, { children: /* @__PURE__ */ jsx("button", {
				type: "button",
				onClick: reset,
				className: "px-4 py-2 rounded bg-error-bg border border-error-border text-error-text hover:opacity-80 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-error-border",
				children: "Try again"
			}) })
		]
	});
}
/**
* Catches render-time errors thrown by descendants and renders a fallback
* instead of letting the error propagate to the nearest route-level boundary.
*
* Does NOT catch errors in event handlers, async callbacks, or SSR — wrap
* those in `try/catch` yourself.
*
* @example
* ```tsx
* <ErrorBoundary fallback={<p>Couldn't load this section.</p>}>
*   <FlakyChild />
* </ErrorBoundary>
* ```
*/
var ErrorBoundary = class extends React.Component {
	static DefaultFallback = DefaultFallback;
	state = { error: null };
	static getDerivedStateFromError(error) {
		return { error };
	}
	componentDidUpdate(prevProps, prevState) {
		if (prevState.error !== null && changed(prevProps.resetKeys, this.props.resetKeys)) this.reset();
	}
	componentDidCatch(error, info) {
		this.props.onError?.(error, info);
	}
	reset = () => {
		this.setState({ error: null });
	};
	render() {
		const { error } = this.state;
		if (!error) return this.props.children;
		const { fallback } = this.props;
		if (typeof fallback === "function") return fallback({
			error,
			reset: this.reset
		});
		if (fallback !== void 0) return fallback;
		return /* @__PURE__ */ jsx(DefaultFallback, {
			error,
			reset: this.reset
		});
	}
};
//#endregion
export { ErrorBoundary };

//# sourceMappingURL=ErrorBoundary.js.map