'use strict';

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

var React = require('react');
var components = require('@apollo/client/react/components');
var errcatch = require('@evo/errcatch');
var Box = require('@prom-ui/core/Box');
var Button = require('@prom-ui/core/Button');
var Text = require('@prom-ui/core/Text');
var useBodyTTag = require('@prom-ui/core/useBodyTTag');

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 React__namespace = /*#__PURE__*/_interopNamespaceCompat(React);

function _iterableToArrayLimit(arr, i) {
  var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
  if (null != _i) {
    var _s,
      _e,
      _x,
      _r,
      _arr = [],
      _n = !0,
      _d = !1;
    try {
      if (_x = (_i = _i.call(arr)).next, 0 === i) {
        if (Object(_i) !== _i) return;
        _n = !1;
      } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0);
    } catch (err) {
      _d = !0, _e = err;
    } finally {
      try {
        if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return;
      } finally {
        if (_d) throw _e;
      }
    }
    return _arr;
  }
}
function _objectWithoutPropertiesLoose(source, excluded) {
  if (source == null) return {};
  var target = {};
  var sourceKeys = Object.keys(source);
  var key, i;
  for (i = 0; i < sourceKeys.length; i++) {
    key = sourceKeys[i];
    if (excluded.indexOf(key) >= 0) continue;
    target[key] = source[key];
  }
  return target;
}
function _objectWithoutProperties(source, excluded) {
  if (source == null) return {};
  var target = _objectWithoutPropertiesLoose(source, excluded);
  var key, i;
  if (Object.getOwnPropertySymbols) {
    var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
    for (i = 0; i < sourceSymbolKeys.length; i++) {
      key = sourceSymbolKeys[i];
      if (excluded.indexOf(key) >= 0) continue;
      if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
      target[key] = source[key];
    }
  }
  return target;
}
function _taggedTemplateLiteral(strings, raw) {
  if (!raw) {
    raw = strings.slice(0);
  }
  return Object.freeze(Object.defineProperties(strings, {
    raw: {
      value: Object.freeze(raw)
    }
  }));
}
function _slicedToArray(arr, i) {
  return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
}
function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === "string") return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === "Object" && o.constructor) n = o.constructor.name;
  if (n === "Map" || n === "Set") return Array.from(o);
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
function _nonIterableRest() {
  throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}

var css = {"root":"SafeQuery__root___IHinP"};

var _templateObject, _templateObject2, _templateObject3;
var SafeQueryError = function SafeQueryError(_ref) {
  var componentErrorName = _ref.componentErrorName,
    onReload = _ref.onReload;
  var _useBodyTTag = useBodyTTag.useBodyTTag(),
    t = _useBodyTTag.t;
  var _useState = React.useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isHidden = _useState2[0],
    setHidden = _useState2[1];
  if (isHidden) {
    return null;
  }
  if (process.env.NODE_ENV !== 'production') {
    if (typeof window === 'undefined') return null;
    return /*#__PURE__*/React__namespace.default.createElement(Box.Box, {
      className: css.root,
      "box-background": "red-500",
      "box-padding": ['s m', 'm@large l@large'],
      "box-position": "relative",
      "box-round": "m",
      "data-qaid": "err_catch_trace"
    }, /*#__PURE__*/React__namespace.default.createElement(Box.Box, {
      "box-margin-right": "5xl"
    }, /*#__PURE__*/React__namespace.default.createElement(Text.Text, {
      "text-color": "white"
    }, t(_templateObject || (_templateObject = _taggedTemplateLiteral(["\u0412\u043E\u0437\u043D\u0438\u043A\u043B\u0430 \u043E\u0448\u0438\u0431\u043A\u0430:"]))), ' ', /*#__PURE__*/React__namespace.default.createElement(Text.Text, {
      "text-weight": "bold",
      style: {
        fontFamily: 'Consolas, "Liberation Mono", Menlo, monospace',
        userSelect: 'all'
      }
    }, componentErrorName))), /*#__PURE__*/React__namespace.default.createElement(Box.Box, {
      "box-position": "absolute center-right",
      "box-margin-right": "s"
    }, /*#__PURE__*/React__namespace.default.createElement(Button.Button, {
      "button-padding": "xxs",
      "button-theme": "white-inherit",
      onClick: onReload,
      title: t(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\u041E\u0431\u043D\u043E\u0432\u0438\u0442\u044C"])))
    }, /*#__PURE__*/React__namespace.default.createElement("svg", {
      xmlns: "http://www.w3.org/2000/svg",
      viewBox: "0 0 18 18",
      width: 18,
      height: 18
    }, /*#__PURE__*/React__namespace.default.createElement("path", {
      fill: "#fff",
      d: "M9 13.5c-2.49 0-4.5-2.01-4.5-4.5S6.51 4.5 9 4.5c1.24 0 2.36.52 3.17 1.33L10 8h5V3l-1.76 1.76C12.15 3.68 10.66 3 9 3 5.69 3 3.01 5.69 3.01 9S5.69 15 9 15c2.97 0 5.43-2.16 5.9-5h-1.52c-.46 2-2.24 3.5-4.38 3.5z"
    }))), /*#__PURE__*/React__namespace.default.createElement(Button.Button, {
      "button-padding": "xxs",
      "button-theme": "white-inherit",
      onClick: function onClick() {
        return setHidden(true);
      },
      title: t(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\u0421\u043A\u0440\u044B\u0442\u044C"])))
    }, /*#__PURE__*/React__namespace.default.createElement("svg", {
      xmlns: "http://www.w3.org/2000/svg",
      viewBox: "0 0 24 24",
      width: 18,
      height: 18
    }, /*#__PURE__*/React__namespace.default.createElement("path", {
      fill: "#fff",
      d: "M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"
    })))));
  }
  return null;
};

var _excluded = ["componentErrorName", "children"];
var SafeQuery = function SafeQuery(_ref) {
  var _ref$componentErrorNa = _ref.componentErrorName,
    componentErrorName = _ref$componentErrorNa === void 0 ? 'SafeQuery' : _ref$componentErrorNa,
    children = _ref.children,
    props = _objectWithoutProperties(_ref, _excluded);
  var _React$useState = React__namespace.useState(false),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    isReload = _React$useState2[0],
    setReload = _React$useState2[1];
  React__namespace.useEffect(function () {
    if (isReload) {
      setReload(false);
    }
  }, [isReload]);
  if (isReload) {
    return null;
  }
  var errorComponent = /*#__PURE__*/React__namespace.createElement(SafeQueryError, {
    componentErrorName: componentErrorName,
    onReload: function onReload() {
      return setReload(true);
    }
  });
  return /*#__PURE__*/React__namespace.createElement(errcatch.ErrCatch, {
    name: componentErrorName,
    errorComponent: errorComponent
  }, /*#__PURE__*/React__namespace.createElement(components.Query, props, function (queryResult) {
    var error = queryResult.error;
    if (error) throw error;
    return children(queryResult);
  }));
};
SafeQuery.displayName = 'SafeQuery';

exports.SafeQuery = SafeQuery;

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