import { describe, expect, it } from "vitest";
import { centsToInput, discountPercent, formatPrice, parsePriceInput } from "@/lib/money";

const compact = (value: string) => value.replace(/[\s\u00a0\u202f]/g, " ");

describe("formatPrice", () => {
  it("formats integer centimes in French with the DH suffix", () => {
    expect(compact(formatPrice(125000, "fr"))).toBe("1 250 DH");
  });

  it("formats decimals and defaults to French", () => {
    expect(compact(formatPrice(125050))).toBe("1 250,5 DH");
  });

  it("formats English prices with a comma grouping", () => {
    expect(compact(formatPrice(125000, "en"))).toBe("1,250 DH");
  });

  it("keeps the DH suffix for Arabic", () => {
    expect(formatPrice(850000, "ar")).toMatch(/DH$/);
  });
});

describe("parsePriceInput", () => {
  it("parses spaces, commas and dots", () => {
    expect(parsePriceInput("1 250")).toBe(125000);
    expect(parsePriceInput("1250,50")).toBe(125050);
    expect(parsePriceInput("1250.50")).toBe(125050);
    expect(parsePriceInput(42)).toBe(4200);
  });

  it("rejects empty, negative or non numeric values", () => {
    expect(parsePriceInput("")).toBeNull();
    expect(parsePriceInput("abc")).toBeNull();
    expect(parsePriceInput("-5")).toBeNull();
    expect(parsePriceInput(Number.NaN)).toBeNull();
  });
});

describe("discountPercent and centsToInput", () => {
  it("computes a rounded markdown percentage", () => {
    expect(discountPercent(8000, 10000)).toBe(20);
    expect(discountPercent(10000, 10000)).toBe(0);
    expect(discountPercent(10000, null)).toBe(0);
  });

  it("renders centimes as a plain input value", () => {
    expect(centsToInput(125050)).toBe("1250.5");
  });
});
