'use strict';

var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
var noModal = require('@web3auth/no-modal');
var react = require('react');
var viem = require('viem');
var wagmi = require('wagmi');
var connectors = require('wagmi/connectors');
require('@babel/runtime/helpers/defineProperty');
require('@web3auth/auth');
require('deepmerge');
require('../../config.js');
require('../../ui/config.js');
require('../../ui/interfaces.js');
require('react/jsx-runtime');
require('../../ui/css/index.css.js');
require('bowser');
require('react-dom/client');
require('@toruslabs/http-helpers');
require('clsx');
require('tailwind-merge');
require('react-i18next');
require('../../ui/context/RootContext.js');
require('../../ui/localeImport.js');
require('classnames');
require('react-qrcode-logo');
require('../../ui/components/Login/Login.js');
var useWeb3Auth = require('../hooks/useWeb3Auth.js');
var useWeb3AuthDisconnect = require('../hooks/useWeb3AuthDisconnect.js');
var constants = require('./constants.js');

const _excluded = ["children"];
const WEB3AUTH_CONNECTOR_ID = "web3auth";
// Helper to initialize connectors for the given wallets
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function setupConnector(provider, config) {
  let connector = config.connectors.find(c => c.id === WEB3AUTH_CONNECTOR_ID);
  if (connector) return connector;
  // Create new connector if not already existing
  connector = connectors.injected({
    target: {
      provider: provider,
      id: WEB3AUTH_CONNECTOR_ID,
      name: "Web3Auth"
    }
  });
  const result = config._internal.connectors.setup(connector);
  config._internal.connectors.setState(current => [...current, result]);
  return result;
}
// Helper to connect a wallet and update wagmi state
async function connectWeb3AuthWithWagmi(connector, config) {
  var _config$storage, _config$storage2;
  await Promise.all([(_config$storage = config.storage) === null || _config$storage === void 0 ? void 0 : _config$storage.removeItem(`${connector.id}.disconnected`), (_config$storage2 = config.storage) === null || _config$storage2 === void 0 ? void 0 : _config$storage2.setItem("recentConnectorId", connector.id)]);
  let chainId = await connector.getChainId();
  if (!config.chains.find(c => c.id === chainId)) {
    chainId = config.chains[0].id;
  }
  const accounts = await connector.getAccounts();
  const connections = new Map([[connector.uid, {
    accounts: [accounts[0]],
    chainId,
    connector
  }]]);
  config.setState(state => _objectSpread(_objectSpread({}, state), {}, {
    chainId,
    connections,
    current: connector.uid,
    status: "connected"
  }));
}
async function disconnectWeb3AuthFromWagmi(config) {
  config._internal.connectors.setState(prev => prev.filter(c => c.id !== WEB3AUTH_CONNECTOR_ID));
  config.setState(state => _objectSpread(_objectSpread({}, state), {}, {
    chainId: state.chainId,
    connections: new Map(),
    current: undefined,
    status: "disconnected"
  }));
}
function Web3AuthWagmiProvider({
  children
}) {
  const {
    isConnected,
    provider
  } = useWeb3Auth.useWeb3Auth();
  const {
    disconnect
  } = useWeb3AuthDisconnect.useWeb3AuthDisconnect();
  const wagmiConfig = wagmi.useConfig();
  const {
    reconnect
  } = wagmi.useReconnect();
  wagmi.useAccountEffect({
    onDisconnect: async () => {
      noModal.log.info("Disconnected from wagmi");
      if (isConnected) await disconnect();
    }
  });
  react.useEffect(() => {
    (async () => {
      if (isConnected && provider) {
        const connector = await setupConnector(provider, wagmiConfig);
        if (!connector) {
          noModal.log.error("Failed to setup react wagmi connector");
          throw new Error("Failed to setup connector");
        }
        await connectWeb3AuthWithWagmi(connector, wagmiConfig);
        reconnect();
      } else if (!isConnected) {
        if (wagmiConfig.state.status === "connected") {
          await disconnectWeb3AuthFromWagmi(wagmiConfig);
        }
      }
    })();
  }, [isConnected, wagmiConfig, provider, reconnect]);
  return /*#__PURE__*/react.createElement(react.Fragment, null, children);
}
function WagmiProvider(_ref) {
  let {
      children
    } = _ref,
    props = _objectWithoutProperties(_ref, _excluded);
  const {
    config
  } = props;
  const {
    web3Auth,
    isInitialized
  } = useWeb3Auth.useWeb3Auth();
  const getTransport = chain => {
    const {
      wsTarget,
      rpcTarget,
      fallbackWsTargets = [],
      fallbackRpcTargets = []
    } = chain;
    const transports = [];
    if (wsTarget) transports.push(viem.webSocket(wsTarget));
    if (fallbackWsTargets.length > 0) transports.push(...fallbackWsTargets.map(target => viem.webSocket(target)));
    if (rpcTarget) transports.push(viem.http(rpcTarget));
    if (fallbackRpcTargets.length > 0) transports.push(...fallbackRpcTargets.map(target => viem.http(target)));
    return wagmi.fallback(transports);
  };
  const finalConfig = react.useMemo(() => {
    var _web3Auth$coreOptions;
    web3Auth === null || web3Auth === void 0 || web3Auth.setAnalyticsProperties({
      wagmi_enabled: true
    });
    if (!isInitialized) return constants.defaultWagmiConfig;
    const finalConfig = _objectSpread(_objectSpread({
      ssr: true
    }, config), {}, {
      chains: undefined,
      connectors: [],
      transports: {},
      multiInjectedProviderDiscovery: false,
      client: undefined
    });
    const wagmiChains = [];
    if (isInitialized && web3Auth !== null && web3Auth !== void 0 && (_web3Auth$coreOptions = web3Auth.coreOptions) !== null && _web3Auth$coreOptions !== void 0 && _web3Auth$coreOptions.chains) {
      var _web3Auth$currentChai;
      const defaultChainId = (_web3Auth$currentChai = web3Auth.currentChain) === null || _web3Auth$currentChai === void 0 ? void 0 : _web3Auth$currentChai.chainId;
      const chains = web3Auth.coreOptions.chains.filter(chain => chain.chainNamespace === noModal.CHAIN_NAMESPACES.EIP155);
      if (chains.length === 0) throw noModal.WalletInitializationError.invalidParams("No valid chains found in web3auth config for wagmi.");
      chains.forEach(chain => {
        const wagmiChain = viem.defineChain({
          id: Number.parseInt(chain.chainId, 16),
          // id in number form
          name: chain.displayName,
          rpcUrls: {
            default: {
              http: [chain.rpcTarget],
              webSocket: [chain.wsTarget]
            }
          },
          blockExplorers: chain.blockExplorerUrl ? {
            default: {
              name: "explorer",
              // TODO: correct name if chain config has it
              url: chain.blockExplorerUrl
            }
          } : undefined,
          nativeCurrency: {
            name: chain.tickerName,
            symbol: chain.ticker,
            decimals: chain.decimals || 18
          }
        });
        if (defaultChainId === chain.chainId) {
          wagmiChains.unshift(wagmiChain);
        } else {
          wagmiChains.push(wagmiChain);
        }
        finalConfig.transports[wagmiChain.id] = getTransport(chain);
      });
      finalConfig.chains = [wagmiChains[0], ...wagmiChains.slice(1)];
    }
    return wagmi.createConfig(finalConfig);
  }, [config, web3Auth, isInitialized]);
  return /*#__PURE__*/react.createElement(wagmi.WagmiProvider, // typecast to WagmiProviderPropsBase to avoid type error
  // as we are omitting the config prop from WagmiProviderProps
  // and creating a new config object with the finalConfig
  _objectSpread(_objectSpread({}, props), {}, {
    config: finalConfig,
    reconnectOnMount: false
  }), /*#__PURE__*/react.createElement(Web3AuthWagmiProvider, null, children));
}

exports.WagmiProvider = WagmiProvider;
