import type React from "react";
import { Component, type ReactNode } from "react";
import { Button } from "@/ui/button";

interface Props {
  children: ReactNode;
  fallback?: ReactNode;
}

interface State {
  hasError: boolean;
  error?: Error;
}

export class TerminalErrorBoundary extends Component<Props, State> {
  constructor(props: Props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error: Error): State {
    return { hasError: true, error };
  }

  componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
    console.error("Terminal Error:", error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      return (
        this.props.fallback || (
          <div className="flex h-full items-center justify-center bg-primary-bg p-4">
            <div className="text-center">
              <p className="mb-2 text-error ui-text-sm">Terminal Error</p>
              <p className="text-text-lighter ui-text-xs">
                {this.state.error?.message || "Failed to initialize terminal"}
              </p>
              <Button
                type="button"
                variant="default"
                onClick={() => this.setState({ hasError: false, error: undefined })}
                className="mt-4"
              >
                Retry
              </Button>
            </div>
          </div>
        )
      );
    }

    return this.props.children;
  }
}
