type WalletKind = "tokenPocket" | "metaMask" | "imToken";

function currentDappUrl(): string {
  if (typeof window === "undefined") return "https://ark-library.com";
  return window.location.href;
}

export function walletDeepLink(
  kind: WalletKind,
  dappUrl = currentDappUrl(),
): string {
  switch (kind) {
    case "tokenPocket":
      return `tpdapp://open?params=${encodeURIComponent(
        JSON.stringify({ url: dappUrl, chain: "BSC" }),
      )}`;
    case "metaMask":
      return `https://metamask.app.link/dapp/${encodeURIComponent(
        dappUrl.replace(/^https?:\/\//, ""),
      )}`;
    case "imToken":
      return `imtokenv2://navigate/DappView?url=${encodeURIComponent(dappUrl)}`;
    default:
      return dappUrl;
  }
}

export function openWalletDeepLink(kind: WalletKind): void {
  if (typeof window === "undefined") return;
  window.location.href = walletDeepLink(kind);
}

const downloadUrls: Record<WalletKind, string> = {
  tokenPocket: "https://extension.tokenpocket.pro/",
  metaMask: "https://metamask.io/download/",
  imToken: "https://token.im/download",
};

export function walletDownloadUrl(kind: WalletKind): string {
  return downloadUrls[kind];
}
