import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import React from "react";
import Sentry from "../../utils/sentry.js";
import BadDataset from "../BadDataset";

var ErrorBoundary = /*#__PURE__*/function (_React$Component) {
  _inherits(ErrorBoundary, _React$Component);

  var _super = _createSuper(ErrorBoundary);

  function ErrorBoundary(props) {
    var _this;

    _classCallCheck(this, ErrorBoundary);

    _this = _super.call(this, props);
    _this.state = {
      hasError: false
    };
    return _this;
  }

  _createClass(ErrorBoundary, [{
    key: "componentDidCatch",
    value: function componentDidCatch(error, errorInfo) {
      var _this2 = this;

      // You can also log the error to an error reporting service
      Sentry.withScope(function (scope) {
        scope.setExtras(errorInfo);
        var eventId = Sentry.captureException(error);

        _this2.setState({
          eventId: eventId
        });
      });
    }
  }, {
    key: "render",
    value: function render() {
      if (this.state.hasError) {
        // You can render any custom fallback UI
        return /*#__PURE__*/React.createElement(BadDataset, {
          title: "An error occurred loading the labeling interface",
          description: this.state.errorString
        });
      }

      return this.props.children;
    }
  }], [{
    key: "getDerivedStateFromError",
    value: function getDerivedStateFromError(error) {
      // Update state so the next render will show the fallback UI.
      console.error("The following error occurred when loading a labeling interface:", error);
      return {
        hasError: true,
        errorString: error.toString()
      };
    }
  }]);

  return ErrorBoundary;
}(React.Component);

export default ErrorBoundary;