import { describe, expect, it } from "vitest";
import { normalizePhone } from "@/lib/phone";
import { parseTypedDate } from "@/lib/dates";

describe("Ethiopian phone numbers", () => {
  it.each([
    "0911223344",
    "911223344",
    "+251911223344",
    "251911223344",
    "0711 22-33-44",
    "711223344",
  ])("accepts %s", (p) => {
    expect(normalizePhone(p).ok).toBe(true);
  });
  it("normalises to display form", () => {
    const r = normalizePhone("+251911223344");
    expect(r.ok && r.display).toBe("0911 22 33 44");
  });
  it("rejects Excel scientific notation", () => {
    expect(normalizePhone("9.11E+08")).toEqual({ ok: false, code: "PHONE_SCIENTIFIC_NOTATION" });
  });
  it("rejects non-mobile numbers", () => {
    expect(normalizePhone("0111234567").ok).toBe(false);
  });
});

describe("typed DD/MM/YYYY dates", () => {
  it("parses an unambiguous date", () => {
    expect(parseTypedDate("15/05/2026")).toMatchObject({ kind: "ok", iso: "2026-05-15" });
  });
  it("always reads the day first", () => {
    expect(parseTypedDate("05/04/2026")).toMatchObject({ kind: "ok", iso: "2026-04-05" });
  });
  it("rejects impossible dates", () => {
    expect(parseTypedDate("31/02/2026").kind).toBe("invalid");
  });
});
