import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import { IntlProvider } from 'react-intl';
export const REACT_INTL_ERROR_MESSAGE = '<IntlProvider> needs to exist in the component ancestry';
const isMissingIntlProviderInAncestryError = err => {
  var _err$toString;
  return err === null || err === void 0 ? void 0 : (_err$toString = err.toString()) === null || _err$toString === void 0 ? void 0 : _err$toString.includes('<IntlProvider> needs to exist in the component ancestry');
};

// Ignored via go/ees005
// eslint-disable-next-line @repo/internal/react/no-class-components, @atlaskit/volt-strict-mode/no-multiple-exports
export class IntlErrorBoundary extends React.Component {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "state", {
      missingIntlProviderInAncestry: false
    });
  }
  componentDidCatch(error, _errorInfo) {
    // if missing IntlProvider in ancestry, we setup a fallback IntlProvider ourselves
    if (isMissingIntlProviderInAncestryError(error)) {
      this.setState({
        missingIntlProviderInAncestry: true
      });
    } else {
      // else we re-propagate the non-react-intl error
      throw error;
    }
  }
  render() {
    if (this.state.missingIntlProviderInAncestry) {
      return /*#__PURE__*/React.createElement(IntlProvider, {
        locale: "en"
      }, this.props.children);
    }
    return this.props.children;
  }
}