// Beta / test-network disclosure.
//
// Derived from the chain id the backend reports (GET /api/chain via
// useWallet().chainConfig) rather than a hardcoded flag — the day this app is
// pointed at a real mainnet the notice has to disappear on its own, otherwise
// it turns into a false statement nobody remembers to delete.

const MAINNET_CHAIN_IDS = [1, 137]; // Ethereum, Polygon
const LOCAL_CHAIN_IDS = [1337, 31337]; // Hardhat / Ganache

function networkKind(chainId) {
  if (chainId === undefined || chainId === null) return null;
  const id = Number(chainId);
  if (MAINNET_CHAIN_IDS.includes(id)) return 'mainnet';
  if (LOCAL_CHAIN_IDS.includes(id)) return 'local';
  return 'testnet';
}

// null when there is nothing to disclose (mainnet, or chain not configured yet).
function useChainNotice() {
  const { chainConfig } = useWallet();
  const { t } = useI18n();
  if (!chainConfig || !chainConfig.configured) return null;
  const kind = networkKind(chainConfig.chainId);
  if (!kind || kind === 'mainnet') return null;
  return {
    kind,
    tag: t('notice.tag'),
    // A local Hardhat chain is a stronger caveat than a public testnet:
    // its history is gone the moment the node restarts.
    bar: kind === 'local' ? t('notice.local.bar') : t('notice.testnet.bar'),
    cert: kind === 'local' ? t('notice.local.cert') : t('notice.testnet.cert'),
    seal: kind === 'local' ? t('notice.local.seal') : t('notice.testnet.seal'),
  };
}

// Slim app-wide strip, mounted under the Nav.
function ChainNoticeBar() {
  const notice = useChainNotice();
  if (!notice) return null;
  return (
    <div className="chain-notice-bar" role="status">
      <span className="chain-notice-tag">{notice.tag}</span>
      <span className="chain-notice-text">{notice.bar}</span>
    </div>
  );
}

// Block version for inside a document (the certificate) or a confirmation
// step (sealing). `variant` picks which sentence to show.
function ChainNoticeInline({ variant = 'cert' }) {
  const notice = useChainNotice();
  if (!notice) return null;
  return (
    <div className="chain-notice-inline" role="note">
      <span className="chain-notice-tag">{notice.tag}</span>
      <span className="chain-notice-text">{variant === 'seal' ? notice.seal : notice.cert}</span>
    </div>
  );
}

Object.assign(window, { networkKind, useChainNotice, ChainNoticeBar, ChainNoticeInline });
