import React from 'react';
import ProviderFactory from './provider-factory';
const ProviderFactoryContext = /*#__PURE__*/React.createContext(new ProviderFactory());
export const ProviderFactoryProvider = ProviderFactoryContext.Provider;
export const useProviderFactory = () => React.useContext(ProviderFactoryContext);
export const useProvider = name => {
  const [provider, setProvider] = React.useState();
  const providerFactory = useProviderFactory();
  React.useEffect(() => {
    const providerHandler = (_, provider) => {
      setProvider(provider);
    };
    providerFactory.subscribe(name, providerHandler);
    return () => {
      providerFactory.unsubscribe(name, providerHandler);
    };
  }, [name, providerFactory]);
  return provider;
};
export const useProviderLayout = name => {
  const [provider, setProvider] = React.useState();
  const providerFactory = useProviderFactory();
  React.useLayoutEffect(() => {
    const providerHandler = (_, provider) => {
      setProvider(provider);
    };
    providerFactory.subscribe(name, providerHandler);
    return () => {
      providerFactory.unsubscribe(name, providerHandler);
    };
  }, [name, providerFactory]);
  return provider;
};