import { describe, it, expect } from "vitest";
import { collectQuestUrls } from "../store";
import type { Section } from "../types";

// Helpers pour construire des fixtures lisibles
function makeQuestItem(name: string, url: string | null = null) {
  return { name, completed: false, combat_indicators: [], note: null, url };
}

function makeQuestSection(items: Section["items"]): Section {
  return { name: "Section test", items };
}

describe("collectQuestUrls", () => {
  it("retourne [] pour un tableau de sections vides", () => {
    expect(collectQuestUrls([])).toEqual([]);
  });

  it("retourne [] si la quête n'a pas d'URL", () => {
    const sections: Section[] = [
      makeQuestSection([{ type: "Quest", ...makeQuestItem("Quête sans URL") }]),
    ];
    expect(collectQuestUrls(sections)).toEqual([]);
  });

  it("retourne l'URL d'une quête avec URL", () => {
    const sections: Section[] = [
      makeQuestSection([
        { type: "Quest", ...makeQuestItem("Quête avec URL", "https://example.com/quest/1") },
      ]),
    ];
    expect(collectQuestUrls(sections)).toEqual(["https://example.com/quest/1"]);
  });

  it("retourne toutes les URLs d'un Group", () => {
    const sections: Section[] = [
      makeQuestSection([
        {
          type: "Group",
          note: null,
          quests: [
            makeQuestItem("Q1", "https://example.com/q1"),
            makeQuestItem("Q2", "https://example.com/q2"),
            makeQuestItem("Q3 sans URL"),
          ],
        },
      ]),
    ];
    expect(collectQuestUrls(sections)).toEqual([
      "https://example.com/q1",
      "https://example.com/q2",
    ]);
  });

  it("collecte les URLs de Quest et Group mais pas des Instruction", () => {
    const sections: Section[] = [
      makeQuestSection([
        { type: "Quest", ...makeQuestItem("Q directe", "https://example.com/direct") },
        {
          type: "Group",
          note: null,
          quests: [makeQuestItem("Q groupe", "https://example.com/groupe")],
        },
        { type: "Instruction", text: "Allez au point de départ" },
      ]),
    ];
    expect(collectQuestUrls(sections)).toEqual([
      "https://example.com/direct",
      "https://example.com/groupe",
    ]);
  });

  it("inclut les deux occurrences si la même URL apparaît deux fois (pas de déduplication)", () => {
    const url = "https://example.com/shared";
    const sections: Section[] = [
      makeQuestSection([
        { type: "Quest", ...makeQuestItem("Q1", url) },
        { type: "Quest", ...makeQuestItem("Q2", url) },
      ]),
    ];
    const result = collectQuestUrls(sections);
    expect(result).toEqual([url, url]);
    expect(result).toHaveLength(2);
  });

  it("fonctionne avec plusieurs sections", () => {
    const sections: Section[] = [
      { name: "Section A", items: [{ type: "Quest", ...makeQuestItem("QA", "https://example.com/a") }] },
      { name: "Section B", items: [{ type: "Quest", ...makeQuestItem("QB", "https://example.com/b") }] },
    ];
    expect(collectQuestUrls(sections)).toEqual([
      "https://example.com/a",
      "https://example.com/b",
    ]);
  });
});
