export type GroceryExportItem = {
  name: string;
  quantity: string | null;
  unit: string | null;
};

export type GroceryExportPayload = {
  listName: string;
  items: GroceryExportItem[];
};

type ShoppingListForExport = {
  name: string;
  items: Array<{ rawName: string; quantity: string | null; unit: string | null; checked: boolean }>;
};

/** Maps a shopping list into a provider-agnostic export shape any grocery-delivery adapter can consume. */
export function buildGroceryExportPayload(list: ShoppingListForExport): GroceryExportPayload {
  return {
    listName: list.name,
    items: list.items
      .filter((i) => !i.checked)
      .map((i) => ({ name: i.rawName, quantity: i.quantity, unit: i.unit })),
  };
}

export function groceryExportToText(payload: GroceryExportPayload): string {
  const lines = payload.items.map((i) => {
    const qty = [i.quantity, i.unit].filter(Boolean).join(" ");
    return qty ? `${qty} ${i.name}` : i.name;
  });
  return [payload.listName, "", ...lines].join("\n");
}
