"use client";
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
import { jsx as _jsx } from "react/jsx-runtime";
import { Component } from "react";
import { Provider } from "./context.js";
/**
 * **Client Component**
 *
 * This is a minimal fork of the `react-error-boundary` package from
 * {@link https://github.com/bvaughn/react-error-boundary/tree/5.0.0}
 * that only includes things I consider useful. Use the `react-error-boundary`
 * if more complex behavior is required.
 *
 * @example Main Use Case
 * ```tsx
 * import { Button } from "@react-md/core/button/Button";
 * import { ErrorBoundary } from "@react-md/core/error-boundary/ErrorBoundary";
 * import { useErrorBoundary } from "@react-md/core/error-boundary/useErrorBoundary";
 * import { Typography } from "@react-md/core/typography/Typography";
 *
 * function Example() {
 *   return (
 *     <ErrorBoundary
 *       fallback={<SomeFallbackComponent />}
 *     >
 *       <SomeComponentThatMightError />
 *     </ErrorBoundary>
 *   );
 * }
 *
 * "use client";
 *
 * function SomeFallbackComponent() {
 *   const { error, errored, reset } = useErrorBoundary();
 *
 *   return (
 *     <>
 *       <Typography>Uh oh, something broke!</Typography>
 *       <Button onClick={reset}>Try Again?</Button>
 *     </>
 *   );
 * }
 * ```
 *
 * @since 6.0.0
 */ export class ErrorBoundary extends Component {
    static getDerivedStateFromError(error) {
        return {
            error,
            errored: true
        };
    }
    componentDidCatch(error, errorInfo) {
        this.props.onError?.(error, errorInfo);
    }
    render() {
        const { fallback } = this.props;
        const { error, errored } = this.state;
        let { children } = this.props;
        if (errored) {
            children = fallback;
        }
        return /*#__PURE__*/ _jsx(Provider, {
            // should always match
            value: {
                error,
                errored,
                reset: this.reset
            },
            children: children
        });
    }
    constructor(props){
        super(props), _define_property(this, "reset", ()=>{
            this.setState({
                error: null,
                errored: false
            });
        });
        this.state = {
            error: null,
            errored: false
        };
    }
}

//# sourceMappingURL=ErrorBoundary.js.map