text: import { __assign, __extends, __read, __spread } from "tslib";
  import { captureEvent, captureException, eventFromException, showReportDialog, withScope, } from '@sentry/browser';
  import { parseSemver } from '@sentry/utils';
  import hoistNonReactStatics from 'hoist-non-react-statics';
  import * as React from 'react';
  var reactVersion = parseSemver(React.version);
  export var UNKNOWN_COMPONENT = 'unknown';
  var INITIAL_STATE = {
      componentStack: null,
      error: null,
      eventId: null,
  };
  /**
   * Logs react error boundary errors to Sentry. If on React version >= 17, creates stack trace
   * from componentStack param, otherwise relies on error param for stacktrace.
   *
   * @param error An error captured by React Error Boundary
   * @param componentStack The component stacktrace
   */
  function captureReactErrorBoundaryError(error, componentStack) {
      var errorBoundaryError = new Error(error.message);
      errorBoundaryError.name = "React ErrorBoundary " + errorBoundaryError.name;
      errorBoundaryError.stack = componentStack;
      var errorBoundaryEvent = {};
      void eventFromException({}, errorBoundaryError).then(function (e) {
          errorBoundaryEvent = e;
      });
      if (errorBoundaryEvent.exception &&
          Array.isArray(errorBoundaryEvent.exception.values) &&
          reactVersion.major &&
          reactVersion.major >= 17) {
          var originalEvent_1 = {};
          void eventFromException({}, error).then(function (e) {
              originalEvent_1 = e;
          });
          if (originalEvent_1.exception && Array.isArray(originalEvent_1.exception.values)) {
              originalEvent_1.exception.values = __spread(errorBoundaryEvent.exception.values, originalEvent_1.exception.values);
          }
          return captureEvent(originalEvent_1);
      }
      return captureException(error, { contexts: { react: { componentStack: componentStack } } });
  }
  /**
   * A ErrorBoundary component that logs errors to Sentry. Requires React >= 16.
   * NOTE: If you are a Sentry user, and you are seeing this stack frame, it means the
   * Sentry React SDK ErrorBoundary caught an error invoking your application code. This
   * is expected behavior and NOT indicative of a bug with the Sentry React SDK.
   */
  var ErrorBoundary = /** @class */ (function (_super) {
      __extends(ErrorBoundary, _super);
      function ErrorBoundary() {
          var _this = _super !== null && _super.apply(this, arguments) || this;
          _this.state = INITIAL_STATE;
          _this.resetErrorBoundary = function () {
              var onReset = _this.props.onReset;
              var _a = _this.state, error = _a.error, componentStack = _a.componentStack, eventId = _a.eventId;
              if (onReset) {
                  onReset(error, componentStack, eventId);
              }
              _this.setState(INITIAL_STATE);
          };
          return _this;
      }
      ErrorBoundary.prototype.componentDidCatch = function (error, _a) {
          var _this = this;
          var componentStack = _a.componentStack;
          var _b = this.props, beforeCapture = _b.beforeCapture, onError = _b.onError, showDialog = _b.showDialog, dialogOptions = _b.dialogOptions;
          withScope(function (scope) {
              if (beforeCapture) {
                  beforeCapture(scope, error, componentStack);
              }
              var eventId = captureReactErrorBoundaryError(error, componentStack);
              if (onError) {
                  onError(error, componentStack, eventId);
              }
              if (showDialog) {
                  showReportDialog(__assign(__assign({}, dialogOptions), { eventId: eventId }));
              }
              // componentDidCatch is used over getDerivedStateFromError
              // so that componentStack is accessible through state.
              _this.setState({ error: error, componentStack: componentStack, eventId: eventId });
          });
      };
      ErrorBoundary.prototype.componentDidMount = function () {
          var onMount = this.props.onMount;
          if (onMount) {
              onMount();
          }
      };
      ErrorBoundary.prototype.componentWillUnmount = function () {
          var _a = this.state, error = _a.error, componentStack = _a.componentStack, eventId = _a.eventId;
          var onUnmount = this.props.onUnmount;
          if (onUnmount) {
              onUnmount(error, componentStack, eventId);
          }
      };
      ErrorBoundary.prototype.render = function () {
          var fallback = this.props.fallback;
          var _a = this.state, error = _a.error, componentStack = _a.componentStack, eventId = _a.eventId;
          if (error) {
              if (React.isValidElement(fallback)) {
                  return fallback;
              }
              if (typeof fallback === 'function') {
                  return fallback({ error: error, componentStack: componentStack, resetError: this.resetErrorBoundary, eventId: eventId });
              }
              // Fail gracefully if no fallback provided
              return null;
          }
          return this.props.children;
      };
      return ErrorBoundary;
  }(React.Component));
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  function withErrorBoundary(WrappedComponent, errorBoundaryOptions) {
      var componentDisplayName = WrappedComponent.displayName || WrappedComponent.name || UNKNOWN_COMPONENT;
      var Wrapped = function (props) { return (React.createElement(ErrorBoundary, __assign({}, errorBoundaryOptions),
          React.createElement(WrappedComponent, __assign({}, props)))); };
      Wrapped.displayName = "errorBoundary(" + componentDisplayName + ")";
      // Copy over static methods from Wrapped component to Profiler HOC
      // See: https://reactjs.org/docs/higher-order-components.html#static-methods-must-be-copied-over
      hoistNonReactStatics(Wrapped, WrappedComponent);
      return Wrapped;
  }
  export { ErrorBoundary, withErrorBoundary };
  //# sourceMappingURL=errorboundary.js.map