'use strict';

var noModal = require('@web3auth/no-modal');
var react = require('react');
var modalManager = require('../../modalManager.js');

const Web3AuthInnerContext = /*#__PURE__*/react.createContext(null);
function Web3AuthInnerProvider(params) {
  const {
    children,
    config,
    initialState
  } = params;
  const {
    web3AuthOptions
  } = config;
  const [chainId, setChainId] = react.useState(null);
  const [chainNamespace, setChainNamespace] = react.useState(null);
  const [isInitializing, setIsInitializing] = react.useState(false);
  const [initError, setInitError] = react.useState(null);
  const [provider, setProvider] = react.useState(null);
  const [isInitialized, setIsInitialized] = react.useState(false);
  const [isMFAEnabled, setIsMFAEnabled] = react.useState(false);
  const web3Auth = react.useMemo(() => {
    setProvider(null);
    return new modalManager.Web3Auth(web3AuthOptions, initialState);
  }, [web3AuthOptions, initialState]);
  const [isConnected, setIsConnected] = react.useState(web3Auth.status === noModal.CONNECTOR_STATUS.CONNECTED);
  const [status, setStatus] = react.useState(web3Auth.status);
  const getPlugin = react.useCallback(name => {
    if (!web3Auth) throw noModal.WalletInitializationError.notReady();
    return web3Auth.getPlugin(name);
  }, [web3Auth]);
  react.useEffect(() => {
    const controller = new AbortController();
    async function init() {
      try {
        var _web3Auth$currentChai;
        setInitError(null);
        setIsInitializing(true);
        web3Auth.setAnalyticsProperties({
          integration_type: noModal.ANALYTICS_INTEGRATION_TYPE.REACT_HOOKS
        });
        await web3Auth.init({
          signal: controller.signal
        });
        setChainId(web3Auth.currentChainId);
        setChainNamespace((_web3Auth$currentChai = web3Auth.currentChain) === null || _web3Auth$currentChai === void 0 ? void 0 : _web3Auth$currentChai.chainNamespace);
      } catch (error) {
        setInitError(error);
      } finally {
        setIsInitializing(false);
      }
    }
    if (web3Auth) init();
    return () => {
      controller.abort();
    };
  }, [web3Auth, config]);
  react.useEffect(() => {
    const handleChainChange = async chainId => {
      var _web3Auth$currentChai2;
      setChainId(chainId);
      setChainNamespace(web3Auth === null || web3Auth === void 0 || (_web3Auth$currentChai2 = web3Auth.currentChain) === null || _web3Auth$currentChai2 === void 0 ? void 0 : _web3Auth$currentChai2.chainNamespace);
    };
    if (provider) {
      provider.on("chainChanged", handleChainChange);
      return () => {
        provider.off("chainChanged", handleChainChange);
      };
    }
  }, [web3Auth, provider]);
  react.useEffect(() => {
    const notReadyListener = () => setStatus(web3Auth.status);
    const readyListener = () => {
      setStatus(web3Auth.status);
      setIsInitialized(true);
    };
    const connectedListener = data => {
      setStatus(web3Auth.status);
      // we do this because of rehydration issues. status connected is fired first but web3auth sdk is not ready yet.
      if (web3Auth.status === noModal.CONNECTOR_STATUS.CONNECTED) {
        setIsInitialized(true);
        setIsConnected(true);
        setProvider(data.provider);
      }
    };
    const disconnectedListener = () => {
      setStatus(web3Auth.status);
      setIsConnected(false);
      setProvider(null);
    };
    const connectingListener = () => {
      setStatus(web3Auth.status);
    };
    const errorListener = () => {
      setStatus(web3Auth.status);
    };
    const rehydrationErrorListener = () => {
      setStatus(web3Auth.status);
      setIsConnected(false);
      setProvider(null);
    };
    const mfaEnabledListener = isMFAEnabled => {
      if (typeof isMFAEnabled === "boolean") setIsMFAEnabled(isMFAEnabled);
    };
    if (web3Auth) {
      // web3Auth is initialized here.
      setStatus(web3Auth.status);
      web3Auth.on(noModal.CONNECTOR_EVENTS.NOT_READY, notReadyListener);
      web3Auth.on(noModal.CONNECTOR_EVENTS.READY, readyListener);
      web3Auth.on(noModal.CONNECTOR_EVENTS.CONNECTED, connectedListener);
      web3Auth.on(noModal.CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
      web3Auth.on(noModal.CONNECTOR_EVENTS.CONNECTING, connectingListener);
      web3Auth.on(noModal.CONNECTOR_EVENTS.ERRORED, errorListener);
      web3Auth.on(noModal.CONNECTOR_EVENTS.REHYDRATION_ERROR, rehydrationErrorListener);
      web3Auth.on(noModal.CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
    }
    return () => {
      if (web3Auth) {
        web3Auth.off(noModal.CONNECTOR_EVENTS.NOT_READY, notReadyListener);
        web3Auth.off(noModal.CONNECTOR_EVENTS.READY, readyListener);
        web3Auth.off(noModal.CONNECTOR_EVENTS.CONNECTED, connectedListener);
        web3Auth.off(noModal.CONNECTOR_EVENTS.DISCONNECTED, disconnectedListener);
        web3Auth.off(noModal.CONNECTOR_EVENTS.CONNECTING, connectingListener);
        web3Auth.off(noModal.CONNECTOR_EVENTS.ERRORED, errorListener);
        web3Auth.off(noModal.CONNECTOR_EVENTS.REHYDRATION_ERROR, rehydrationErrorListener);
        web3Auth.off(noModal.CONNECTOR_EVENTS.MFA_ENABLED, mfaEnabledListener);
        web3Auth.cleanup();
      }
    };
  }, [web3Auth]);
  const value = react.useMemo(() => {
    return {
      web3Auth,
      isConnected,
      isInitialized,
      provider,
      status,
      isInitializing,
      initError,
      isMFAEnabled,
      chainId,
      chainNamespace,
      getPlugin,
      setIsMFAEnabled
    };
  }, [web3Auth, isConnected, isMFAEnabled, setIsMFAEnabled, isInitialized, provider, status, getPlugin, isInitializing, initError, chainId, chainNamespace]);
  return /*#__PURE__*/react.createElement(Web3AuthInnerContext.Provider, {
    value
  }, children);
}

exports.Web3AuthInnerContext = Web3AuthInnerContext;
exports.Web3AuthInnerProvider = Web3AuthInnerProvider;
