import { describe, it, expect } from "vitest";
import { describeBundleContents } from "../bundleContents";
import type { RawProduct } from "../productsServer";

const product = (overrides: Partial<RawProduct> = {}): RawProduct => ({
  id: 1,
  slug: "starter-set",
  name: "Starter-Set",
  sku: null,
  price: 29.9,
  active: true,
  image: null,
  taxRatePercent: null,
  noShippingCost: false,
  bundleItems: null,
  variants: null,
  trackInventory: false,
  stock: null,
  allowBackorder: false,
  ...overrides,
});

describe("describeBundleContents", () => {
  it("returns null for a regular (non-bundle) product", () => {
    expect(describeBundleContents(product())).toBeNull();
  });

  it("returns null for an empty bundleItems array", () => {
    expect(describeBundleContents(product({ bundleItems: [] }))).toBeNull();
  });

  it("formats a single bundle item as 'qty× name'", () => {
    const result = describeBundleContents(
      product({ bundleItems: [{ product: { id: 2, name: "ToDo-Karten" }, quantity: 2 }] }),
    );
    expect(result).toBe("2× ToDo-Karten");
  });

  it("joins multiple bundle items with a comma", () => {
    const result = describeBundleContents(
      product({
        bundleItems: [
          { product: { id: 2, name: "ToDo-Karten" }, quantity: 2 },
          { product: { id: 3, name: "Wochenplaner" }, quantity: 1 },
        ],
      }),
    );
    expect(result).toBe("2× ToDo-Karten, 1× Wochenplaner");
  });

  it("skips a line whose product didn't resolve to an object (depth miss)", () => {
    const result = describeBundleContents(
      product({
        bundleItems: [
          { product: 5, quantity: 1 },
          { product: { id: 3, name: "Wochenplaner" }, quantity: 1 },
        ],
      }),
    );
    expect(result).toBe("1× Wochenplaner");
  });
});
