import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
var _excluded = ["axios", "dateFnsLocale"];
import { createContext, forwardRef, useContext, useMemo, useRef } from 'react';
import invariant from 'tiny-invariant';
import { clientPromise } from './client';
import { jsx as _jsx } from "react/jsx-runtime";
var Context = /*#__PURE__*/createContext();
export function FormulaProvider(_ref) {
  var axios = _ref.axios,
      children = _ref.children,
      dateFnsLocale = _ref.dateFnsLocale;
  var contextRef = useRef({});

  if (axios) {
    _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.t0 = axios;
              _context.next = 3;
              return clientPromise;

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

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

  return /*#__PURE__*/_jsx(Context.Provider, {
    value: useMemo(function () {
      return _objectSpread(_objectSpread({}, contextRef.current), {}, {
        dateFnsLocale: dateFnsLocale
      });
    }, [dateFnsLocale]),
    children: children
  });
}
export var withFormulaProvider = function withFormulaProvider(Form) {
  return /*#__PURE__*/forwardRef(function (_ref3, ref) {
    var axios = _ref3.axios,
        dateFnsLocale = _ref3.dateFnsLocale,
        other = _objectWithoutProperties(_ref3, _excluded);

    return /*#__PURE__*/_jsx(FormulaProvider, {
      axios: axios,
      dateFnsLocale: dateFnsLocale,
      children: /*#__PURE__*/_jsx(Form, _objectSpread({
        ref: ref
      }, other))
    });
  });
};
export function useFormulaContext() {
  var context = useContext(Context);
  invariant(context, 'You should not use Formula hooks outside a <FormulaProvider>');
  return context;
}
//# sourceMappingURL=Context.js.map