type PrintableBackup = {
  label: string;
  address: string;
  privateKey: string;
  encryptionKey: string;
};

async function initialize(): Promise<void> {
  const id = new URLSearchParams(location.search).get("id");
  if (!id) throw new Error("Missing printable wallet backup.");

  const storageKey = `print-backup:${id}`;
  const stored = await chrome.storage.session.get(storageKey);
  await chrome.storage.session.remove(storageKey);
  const backup = stored[storageKey] as PrintableBackup | undefined;
  if (!backup) throw new Error("The printable wallet backup has expired.");

  document.getElementById("wallet-label")!.textContent = backup.label;
  document.getElementById("wallet-address")!.textContent = backup.address;
  document.getElementById("wallet-private-key")!.textContent = backup.privateKey;
  document.getElementById("wallet-encryption-key")!.textContent = backup.encryptionKey;

  setTimeout(() => {
    window.focus();
    window.print();
  }, 150);
}

initialize().catch((error: unknown) => {
  document.getElementById("print-error")!.textContent =
    error instanceof Error ? error.message : String(error);
});
