import assert from "node:assert/strict"
import { describe, it } from "node:test"
import {
  buildDestinationMailboxes,
  destinationEmailFromStoredKey,
  normalizeAutoImportDestinationKeys,
  normalizeAutoImportInvitationSources,
  normalizeInvitationImportExclusions,
} from "./agenda-destination-identities.ts"

describe("destinationEmailFromStoredKey", () => {
  it("extrait l'email d'une clé legacy accountId:email", () => {
    assert.equal(
      destinationEmailFromStoredKey("acc-1:Alias@Example.com"),
      "alias@example.com",
    )
  })

  it("normalise un email seul", () => {
    assert.equal(destinationEmailFromStoredKey(" User@Mail.fr "), "user@mail.fr")
  })
})

describe("buildDestinationMailboxes", () => {
  it("déduplique comptes et identités par email", () => {
    const mailboxes = buildDestinationMailboxes(
      [
        { name: "A", email: "same@x.com", accountId: "1", defaultSignatureId: null },
        { name: "B", email: "Same@X.com", accountId: "2", defaultSignatureId: null },
        { name: "C", email: "other@y.com", accountId: "1", defaultSignatureId: null },
      ],
      ["same@x.com", "other@y.com"],
    )
    assert.equal(mailboxes.length, 2)
    assert.deepEqual(
      mailboxes.map((m) => m.key).sort(),
      ["other@y.com", "same@x.com"],
    )
  })
})

describe("normalizeAutoImportDestinationKeys", () => {
  it("fusionne les clés legacy pointant vers le même email", () => {
    assert.deepEqual(
      normalizeAutoImportDestinationKeys([
        "acc1:user@x.com",
        "acc2:user@x.com",
        "user@x.com",
      ]),
      ["user@x.com"],
    )
  })
})

describe("normalizeAutoImportInvitationSources", () => {
  it("accepte mails de destination et contacts expéditeurs", () => {
    const items = normalizeAutoImportInvitationSources([
      { type: "identity", value: "acc1:inbox@x.com", label: "inbox@x.com" },
      {
        type: "contact",
        value: "alice@corp.com",
        label: "Alice <alice@corp.com>",
      },
      {
        type: "contact",
        value: "Alice@corp.com",
        label: "Alice <alice@corp.com>",
      },
      { type: "folder", value: "spam", label: "Dossier · spam" },
    ])
    assert.equal(items.length, 2)
    assert.equal(items[0]?.type, "identity")
    assert.equal(items[0]?.value, "inbox@x.com")
    assert.equal(items[1]?.type, "contact")
    assert.equal(items[1]?.value, "alice@corp.com")
  })
})

describe("normalizeInvitationImportExclusions", () => {
  it("déduplique les exclusions identité par mail de destination", () => {
    const items = normalizeInvitationImportExclusions([
      {
        type: "identity",
        value: "acc1:foo@bar.com",
        label: "Foo <foo@bar.com>",
      },
      {
        type: "identity",
        value: "foo@bar.com",
        label: "foo@bar.com",
      },
      { type: "email", value: "spam@x.com", label: "spam@x.com" },
    ])
    assert.equal(items.filter((i) => i.type === "identity").length, 1)
    assert.equal(items.find((i) => i.type === "identity")?.value, "foo@bar.com")
    assert.equal(items.filter((i) => i.type === "email").length, 1)
  })
})
