import React from "react";
import { View } from "react-native";
import { Text } from "./text";
import { Button } from "./button";
export class ErrorBoundary extends React.Component {
    state = { hasError: false, resetKey: 0 };
    static getDerivedStateFromError() {
        return { hasError: true };
    }
    componentDidCatch(error, info) {
        if (__DEV__) {
            console.error("[ErrorBoundary]", error, info.componentStack);
        }
    }
    handleReset = () => {
        try {
            this.props.onReset?.();
        }
        catch {
            // onReset failures must not crash the boundary itself.
        }
        this.setState((prev) => ({ hasError: false, resetKey: prev.resetKey + 1 }));
    };
    render() {
        if (this.state.hasError) {
            return (this.props.fallback ?? (<View className="flex-1 items-center justify-center p-8">
            <Text className="mb-4 text-lg font-semibold text-foreground dark:text-white">
              Something went wrong
            </Text>
            <Button onPress={this.handleReset}>
              <Text className="text-primary-foreground dark:text-black">Try Again</Text>
            </Button>
          </View>));
        }
        // Keying the subtree on `resetKey` forces a remount on retry so
        // component-level state that contributed to the crash is
        // recreated from scratch rather than re-rendering against the
        // same stale props/state and crash-looping.
        return <React.Fragment key={this.state.resetKey}>{this.props.children}</React.Fragment>;
    }
}
//# sourceMappingURL=error-boundary.jsx.map