import { describe, expect, it } from "vitest";
import { EU_COUNTRIES, isFreeShipping, shippingFor, type ShippingConfig } from "@/lib/shipping-calc";

const config: ShippingConfig = { freeThreshold: 150000, morocco: 4500, europe: 25000, world: 40000 };

describe("shippingFor", () => {
  it("charges Morocco below the free threshold and ships free at the threshold", () => {
    expect(shippingFor("MA", 149999, config)).toBe(4500);
    expect(shippingFor("MA", 150000, config)).toBe(0);
  });

  it("uses the European rate for EU destinations", () => {
    for (const country of ["FR", "DE", "IT", "ES"]) {
      expect(EU_COUNTRIES).toContain(country);
      expect(shippingFor(country, 500000, config)).toBe(25000);
    }
  });

  it("uses the world rate elsewhere", () => {
    expect(shippingFor("US", 500000, config)).toBe(40000);
    expect(shippingFor("JP", 0, config)).toBe(40000);
  });
});

describe("isFreeShipping", () => {
  it("only applies to Morocco above the threshold", () => {
    expect(isFreeShipping("MA", 150000, config)).toBe(true);
    expect(isFreeShipping("MA", 149999, config)).toBe(false);
    expect(isFreeShipping("FR", 500000, config)).toBe(false);
  });
});
