"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormulaProvider = FormulaProvider;
exports.useFormulaContext = useFormulaContext;
exports.withFormulaProvider = void 0;

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _react = require("react");

var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));

var _client = require("./client");

var _jsxRuntime = require("react/jsx-runtime");

var _excluded = ["axios", "dateFnsLocale"];
var Context = /*#__PURE__*/(0, _react.createContext)();

function FormulaProvider(_ref) {
  var axios = _ref.axios,
      children = _ref.children,
      dateFnsLocale = _ref.dateFnsLocale;
  var contextRef = (0, _react.useRef)({});

  if (axios) {
    (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.t0 = axios;
              _context.next = 3;
              return _client.clientPromise;

            case 3:
              _context.t1 = _context.sent;
              (0, _context.t0)(_context.t1);

            case 5:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  }

  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Context.Provider, {
    value: (0, _react.useMemo)(function () {
      return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, contextRef.current), {}, {
        dateFnsLocale: dateFnsLocale
      });
    }, [dateFnsLocale]),
    children: children
  });
}

var withFormulaProvider = function withFormulaProvider(Form) {
  return /*#__PURE__*/(0, _react.forwardRef)(function (_ref3, ref) {
    var axios = _ref3.axios,
        dateFnsLocale = _ref3.dateFnsLocale,
        other = (0, _objectWithoutProperties2["default"])(_ref3, _excluded);
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(FormulaProvider, {
      axios: axios,
      dateFnsLocale: dateFnsLocale,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Form, (0, _objectSpread2["default"])({
        ref: ref
      }, other))
    });
  });
};

exports.withFormulaProvider = withFormulaProvider;

function useFormulaContext() {
  var context = (0, _react.useContext)(Context);
  (0, _tinyInvariant["default"])(context, 'You should not use Formula hooks outside a <FormulaProvider>');
  return context;
}
//# sourceMappingURL=Context.cjs.map