/**
 * Drop-in replacement for the website's hand-maintained V2 address arrays.
 *
 * The nested site currently declares `BASE_CONTRACTS`/`AVALANCHE_CONTRACTS`/... in
 * `app/lib/contracts.ts` as literal address lists. Those literals are a second
 * source of truth for the same 134 deployments already inventoried in
 * `baseline/deployments.json`. This adapter produces the identical runtime shape
 * from the generated constants, so the site keeps its existing behavior, routes,
 * and visual design while dropping the duplicate map.
 *
 * ABI resolution stays in the website (`getABI` from `app/lib/abis/abi-map`) and
 * is injected here, so this module has no website-side imports.
 */

import {
  ARBITRUM_V2_ADDRESSES,
  AVALANCHE_V2_ADDRESSES,
  BASE_V2_ADDRESSES,
  POLYGON_V2_ADDRESSES,
  V2_ADDRESSES,
  type WebsiteAddressEntry,
  type WebsiteChain,
} from "../generated/website-addresses.generated";

/** Mirrors the existing website `Contract` interface exactly. */
export type LegacyContract = {
  name: string;
  address: `0x${string}`;
  chain: WebsiteChain;
  abi: readonly unknown[];
};

export type AbiResolver = (name: string) => readonly unknown[];

export function toLegacyContract(
  entry: WebsiteAddressEntry,
  getABI: AbiResolver,
): LegacyContract {
  return {
    name: entry.name,
    address: entry.address,
    chain: entry.chain,
    abi: getABI(entry.name),
  };
}

export function legacyContractsForChain(
  chain: WebsiteChain,
  getABI: AbiResolver,
): LegacyContract[] {
  const source: Record<WebsiteChain, readonly WebsiteAddressEntry[]> = {
    base: BASE_V2_ADDRESSES,
    avalanche: AVALANCHE_V2_ADDRESSES,
    arbitrum: ARBITRUM_V2_ADDRESSES,
    polygon: POLYGON_V2_ADDRESSES,
  };
  return source[chain].map((entry) => toLegacyContract(entry, getABI));
}

export function allLegacyContracts(getABI: AbiResolver): LegacyContract[] {
  return V2_ADDRESSES.map((entry) => toLegacyContract(entry, getABI));
}
