import { readFileSync } from "node:fs";
import ts from "typescript";
import { describe, expect, it } from "vitest";
import * as publicApi from "../src/index.js";

function typeOnlyExports(): string[] {
  const source = ts.createSourceFile("index.ts", readFileSync(new URL("../src/index.ts", import.meta.url), "utf8"), ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
  const names: string[] = [];
  for (const statement of source.statements) {
    if (!ts.isExportDeclaration(statement) || !statement.exportClause || !ts.isNamedExports(statement.exportClause)) continue;
    for (const element of statement.exportClause.elements) {
      if (statement.isTypeOnly || element.isTypeOnly) names.push(element.name.text);
    }
  }
  return names.sort();
}

describe("public export surface", () => {
  it("matches the reviewed runtime exports", () => {
    expect(Object.keys(publicApi).sort()).toEqual([
      "ARC_MAINNET", "ARC_TESTNET", "AmountPrecisionError", "AttestationNotReadyError",
      "IrisRequestError", "Keypair", "STELLAR_MAINNET", "STELLAR_TESTNET",
      "SubmissionTimeoutError", "TransferError", "arcMainnetChain", "arcTestnetChain",
      "checkStellarRecipientReady", "completeMint", "convertPrecision", "decimalsForChain",
      "decodeCctpMessage", "decodeStellarForwardHook", "estimateFee", "getAttestation",
      "resolveBurn", "transfer",
    ]);
  });

  it("matches the reviewed type-only exports", () => {
    expect(typeOnlyExports()).toEqual([
      "ApproveProgressEvent", "ArcDestinationOptions", "ArcSigner", "AttestationProgressEvent",
      "BurnConfirmedProgressEvent", "BurnSubmittedProgressEvent", "CctpBurnMessage", "ChainId",
      "CompleteMintOptions", "CompleteMintParams", "CompleteMintResult", "EstimateFeeParams",
      "FeeEstimate", "MintConfirmedProgressEvent", "MintSubmittedProgressEvent", "Network",
      "RecipientCheckProgressEvent", "ResolveBurnParams", "ResolvedBurn", "Signer",
      "StellarDestinationOptions", "StellarRecipientStatus", "StellarRetryOptions",
      "StellarSignFunction", "StellarSigner", "TransferErrorCode", "TransferMode",
      "TransferOptions", "TransferOptionsBase", "TransferParams", "TransferProgressEvent",
      "TransferProgressStage", "TransferResult", "TransferSpeed", "TransferStatus",
    ]);
  });
});
