import { allowedRateUnits, isBulkQuantityUnit } from "./rate-unit.util";

/**
 * A bulk rate's weighting unit follows how its commodity is counted: wheat is
 * weighed (per ton), machinery is counted (per item). Per-wagon is offered
 * either way.
 */
describe("allowedRateUnits — bulk unit of measure", () => {
  it("offers per-ton for a weighed commodity", () => {
    expect(
      allowedRateUnits({
        appliesTo: "BULK",
        trigger: "ALWAYS",
        cargoUnitOfMeasure: "PER_TON",
      }),
    ).toEqual(["PER_TON", "PER_WAGON"]);
  });

  it("offers per-item for a counted commodity", () => {
    expect(
      allowedRateUnits({
        appliesTo: "BULK",
        trigger: "ALWAYS",
        cargoUnitOfMeasure: "PER_ITEM",
      }),
    ).toEqual(["PER_ITEM", "PER_WAGON"]);
  });

  it("falls back to per-ton when the rate is not scoped to a commodity", () => {
    expect(allowedRateUnits({ appliesTo: "BULK", trigger: "ALWAYS" })).toEqual([
      "PER_TON",
      "PER_WAGON",
    ]);
  });

  it("swaps the per-ton slot for counted commodities on every bulk-capable shape", () => {
    expect(
      allowedRateUnits({
        appliesTo: "OTHER",
        trigger: "CUSTOMS_CLEARANCE",
        cargoKind: "BULK",
        cargoUnitOfMeasure: "PER_ITEM",
      }),
    ).toEqual(["PER_ITEM", "PER_WAGON"]);
    expect(
      allowedRateUnits({
        appliesTo: "INTERCITY",
        trigger: "ALWAYS",
        cargoUnitOfMeasure: "PER_ITEM",
      }),
    ).toEqual(["PER_CONTAINER", "PER_ITEM", "PER_WAGON", "PER_KM"]);
  });

  it("never offers per-item for overweight, which is always per excess ton", () => {
    expect(
      allowedRateUnits({
        appliesTo: "OTHER",
        trigger: "OVERWEIGHT",
        cargoUnitOfMeasure: "PER_TON",
      }),
    ).toEqual(["PER_TON"]);
  });

  it("treats per-ton and per-item as the same booking quantity", () => {
    expect(isBulkQuantityUnit("PER_TON")).toBe(true);
    expect(isBulkQuantityUnit("PER_ITEM")).toBe(true);
    expect(isBulkQuantityUnit("PER_WAGON")).toBe(false);
    expect(isBulkQuantityUnit("FLAT")).toBe(false);
  });
});
