import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React from 'react';
import ProviderFactory from './provider-factory';
var ProviderFactoryContext = /*#__PURE__*/React.createContext(new ProviderFactory());
export var ProviderFactoryProvider = ProviderFactoryContext.Provider;

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var useProviderFactory = function useProviderFactory() {
  return React.useContext(ProviderFactoryContext);
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var useProvider = function useProvider(name) {
  var _React$useState = React.useState(),
    _React$useState2 = _slicedToArray(_React$useState, 2),
    provider = _React$useState2[0],
    setProvider = _React$useState2[1];
  var providerFactory = useProviderFactory();
  React.useEffect(function () {
    var providerHandler = function providerHandler(_, provider) {
      setProvider(provider);
    };
    providerFactory.subscribe(name, providerHandler);
    return function () {
      providerFactory.unsubscribe(name, providerHandler);
    };
  }, [name, providerFactory]);
  return provider;
};

// eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
export var useProviderLayout = function useProviderLayout(name) {
  var _React$useState3 = React.useState(),
    _React$useState4 = _slicedToArray(_React$useState3, 2),
    provider = _React$useState4[0],
    setProvider = _React$useState4[1];
  var providerFactory = useProviderFactory();
  React.useLayoutEffect(function () {
    var providerHandler = function providerHandler(_, provider) {
      setProvider(provider);
    };
    providerFactory.subscribe(name, providerHandler);
    return function () {
      providerFactory.unsubscribe(name, providerHandler);
    };
  }, [name, providerFactory]);
  return provider;
};