'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _possibleConstructorReturn = require('@babel/runtime/helpers/possibleConstructorReturn');
var _getPrototypeOf = require('@babel/runtime/helpers/getPrototypeOf');
var _inherits = require('@babel/runtime/helpers/inherits');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var React = require('react');
var _taggedTemplateLiteral = require('@babel/runtime/helpers/taggedTemplateLiteral');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var Button = require('@prom-ui/core/Button');
var BodyProvider = require('@prom-ui/core/BodyProvider');
var Reload = require('@prom-ui/icons/Reload');
var Close = require('@prom-ui/icons/Close');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

function _interopNamespaceCompat(e) {
    if (e && typeof e === 'object' && 'default' in e) return e;
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () { return e[k]; }
                });
            }
        });
    }
    n.default = e;
    return Object.freeze(n);
}

var _classCallCheck__default = /*#__PURE__*/_interopDefaultCompat(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultCompat(_createClass);
var _possibleConstructorReturn__default = /*#__PURE__*/_interopDefaultCompat(_possibleConstructorReturn);
var _getPrototypeOf__default = /*#__PURE__*/_interopDefaultCompat(_getPrototypeOf);
var _inherits__default = /*#__PURE__*/_interopDefaultCompat(_inherits);
var _defineProperty__default = /*#__PURE__*/_interopDefaultCompat(_defineProperty);
var React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);
var _taggedTemplateLiteral__default = /*#__PURE__*/_interopDefaultCompat(_taggedTemplateLiteral);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultCompat(_slicedToArray);

var logError = function logError(msg, error, info) {
  console.error(msg, error, info);
};
var setErrorCatchLog = function setErrorCatchLog(fn) {
  logError = fn;
};

var css = {"root":"ErrorCatch__root___MgWkO","text":"ErrorCatch__text___mFI5O","name":"ErrorCatch__name___-bzUk","button":"ErrorCatch__button___9tlvu"};

var _templateObject, _templateObject2, _templateObject3;
var ErrorView = function ErrorView(_ref) {
  var name = _ref.name,
    onClose = _ref.onClose,
    onReload = _ref.onReload;
  var _React$useState = React__namespace.useState(false),
    _React$useState2 = _slicedToArray__default.default(_React$useState, 2),
    isLoading = _React$useState2[0],
    setLoading = _React$useState2[1];
  var _useBodyStoreTTag = BodyProvider.useBodyStoreTTag(),
    t = _useBodyStoreTTag.t;
  var handleOnReload = function handleOnReload() {
    setLoading(true);
    setTimeout(function () {
      setLoading(false);
      if (onReload) {
        onReload();
      }
    }, 500);
  };
  if (process.env.NODE_ENV !== 'production') {
    return /*#__PURE__*/React__namespace.createElement("div", {
      className: css.root,
      "data-qaid": "error_catch",
      "data-testid": "error_catch",
      "data-pds-color-scheme": "light"
    }, /*#__PURE__*/React__namespace.createElement("div", {
      className: css.text,
      "data-qaid": "error_catch_text",
      "data-testid": "error_catch_text"
    }, t(_templateObject || (_templateObject = _taggedTemplateLiteral__default.default(["\u041E\u0448\u0438\u0431\u043A\u0430:"]))), !!name && /*#__PURE__*/React__namespace.createElement(React__namespace.Fragment, null, ' ', /*#__PURE__*/React__namespace.createElement("span", {
      className: css.name,
      "data-qaid": "error_catch_component_name",
      "data-testid": "error_catch_component_name"
    }, name))), onReload && /*#__PURE__*/React__namespace.createElement(Button.Button, {
      "button-theme": "white-inherit",
      "button-padding": "xxs",
      type: "button",
      className: css.button,
      onClick: handleOnReload,
      title: t(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral__default.default(["\u041E\u0431\u043D\u043E\u0432\u0438\u0442\u044C"]))),
      loading: isLoading,
      "data-qaid": "error_catch_button_reload",
      "data-testid": "error_catch_button_reload"
    }, /*#__PURE__*/React__namespace.createElement(Reload.SvgReload, {
      width: 18,
      height: 18
    })), onClose && /*#__PURE__*/React__namespace.createElement(Button.Button, {
      "button-theme": "white-inherit",
      "button-padding": "xxs",
      type: "button",
      disabled: isLoading,
      className: css.button,
      onClick: onClose,
      title: t(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral__default.default(["\u0421\u043A\u0440\u044B\u0442\u044C"]))),
      "data-qaid": "error_catch_button_close",
      "data-testid": "error_catch_button_close"
    }, /*#__PURE__*/React__namespace.createElement(Close.SvgClose, {
      width: 18,
      height: 18
    })));
  }
  return null;
};
ErrorView.displayName = 'ErrorView';

function _callSuper(t, o, e) { return o = _getPrototypeOf__default.default(o), _possibleConstructorReturn__default.default(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf__default.default(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
/**
 * React-компонент, що перехоплює помилки JavaScript у будь-якому місці свого дочірнього дерева компонентів, реєструє ці помилки та відображає резервний користувацький інтерфейс.
 */
var ErrorCatch = /*#__PURE__*/function (_React$Component) {
  function ErrorCatch(props) {
    var _this;
    _classCallCheck__default.default(this, ErrorCatch);
    _this = _callSuper(this, ErrorCatch, [props]);
    _defineProperty__default.default(_this, "onClose", function () {
      _this.setState({
        isHidden: true
      });
    });
    _defineProperty__default.default(_this, "onReload", function () {
      _this.setState({
        isError: false
      });
    });
    _this.state = {
      isError: false,
      isHidden: false
    };
    return _this;
  }
  _inherits__default.default(ErrorCatch, _React$Component);
  return _createClass__default.default(ErrorCatch, [{
    key: "componentDidCatch",
    value: function componentDidCatch(error, info) {
      var name = this.props.name;
      logError("".concat(name, ": Failed to render component"), error, info);
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props = this.props,
        name = _this$props.name,
        errorView = _this$props.errorView,
        children = _this$props.children;
      var _this$state = this.state,
        isError = _this$state.isError,
        isHidden = _this$state.isHidden;
      if (isHidden) {
        return null;
      }
      if (isError) {
        if (typeof window === 'undefined') return null;
        if (errorView && /*#__PURE__*/React__namespace.isValidElement(errorView)) {
          return /*#__PURE__*/React__namespace.cloneElement(errorView, {
            name: name,
            onReload: this.onReload,
            onClose: this.onClose
          });
        }
        return null;
      }
      return children;
    }
  }], [{
    key: "getDerivedStateFromError",
    value: function getDerivedStateFromError() {
      return {
        isError: true
      };
    }
  }]);
}(React__namespace.Component);
_defineProperty__default.default(ErrorCatch, "defaultProps", {
  errorView: /*#__PURE__*/React__namespace.createElement(ErrorView, null)
});

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
var withErrorCatch = function withErrorCatch(Component, errorCatchProps) {
  var WithErrorCatch = function WithErrorCatch(props) {
    return /*#__PURE__*/React__namespace.createElement(ErrorCatch, errorCatchProps, /*#__PURE__*/React__namespace.createElement(Component, props));
  };
  WithErrorCatch.displayName = 'WithErrorCatch';
  return WithErrorCatch;
};

exports.ErrorCatch = ErrorCatch;
exports.setErrorCatchLog = setErrorCatchLog;
exports.withErrorCatch = withErrorCatch;

require('./style.development.css');
