"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useProviderLayout = exports.useProviderFactory = exports.useProvider = exports.ProviderFactoryProvider = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = _interopRequireDefault(require("react"));
var _providerFactory = _interopRequireDefault(require("./provider-factory"));
var ProviderFactoryContext = /*#__PURE__*/_react.default.createContext(new _providerFactory.default());
var ProviderFactoryProvider = exports.ProviderFactoryProvider = ProviderFactoryContext.Provider;
var useProviderFactory = exports.useProviderFactory = function useProviderFactory() {
  return _react.default.useContext(ProviderFactoryContext);
};
var useProvider = exports.useProvider = function useProvider(name) {
  var _React$useState = _react.default.useState(),
    _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
    provider = _React$useState2[0],
    setProvider = _React$useState2[1];
  var providerFactory = useProviderFactory();
  _react.default.useEffect(function () {
    var providerHandler = function providerHandler(_, provider) {
      setProvider(provider);
    };
    providerFactory.subscribe(name, providerHandler);
    return function () {
      providerFactory.unsubscribe(name, providerHandler);
    };
  }, [name, providerFactory]);
  return provider;
};
var useProviderLayout = exports.useProviderLayout = function useProviderLayout(name) {
  var _React$useState3 = _react.default.useState(),
    _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2),
    provider = _React$useState4[0],
    setProvider = _React$useState4[1];
  var providerFactory = useProviderFactory();
  _react.default.useLayoutEffect(function () {
    var providerHandler = function providerHandler(_, provider) {
      setProvider(provider);
    };
    providerFactory.subscribe(name, providerHandler);
    return function () {
      providerFactory.unsubscribe(name, providerHandler);
    };
  }, [name, providerFactory]);
  return provider;
};