/**
 * @jest-environment jsdom
 *
 * Regression test for the nested-nav collapsing fix in segment-page.ts.
 *
 * Fixture below is a trimmed-down but structurally faithful excerpt of
 * https://www.albi-tourisme.fr/'s off-canvas menu: BEM classes repeat the
 * "menu" token at every wrapper level (.menu > .menu__wrapper >
 * ul.menu__wrapper__pages > li.menu__item > ...), which independently
 * re-triggers the `nav` classification rule at each level. Before the fix,
 * that produced a chain of nested `nav` blocks; since renderNav() only ever
 * renders a block's OWN direct leaves (see reader-view/renderers/chrome.ts),
 * the real menu links — bucketed several nav-levels deep — never reached
 * the rendered output.
 */
import { segmentPage } from "../../../src/adapters/reader/structure/segment-page";

const MENU_HTML = `
<main>
  <h1>Découvrir Albi</h1>
  <p>Albi, cité épiscopale classée à l'UNESCO, invite à découvrir son patrimoine, ses musées et ses ruelles chargées d'histoire au bord du Tarn.</p>
  <p>Entre visites guidées, dégustations locales et animations culturelles, la ville offre un large éventail d'activités pour petits et grands tout au long de l'année.</p>
</main>
<div class="menu">
  <div class="menu__heading">
    <div class="tools">
      <div class="tools__section">
        <div class="tools__item">
          <a href="https://www.albi-tourisme.fr/groupes/">Visites groupes</a>
        </div>
      </div>
    </div>
  </div>
  <div class="menu__wrapper container">
    <ul class="menu__wrapper__pages not-list-style">
      <li class="menu__item">
        <ul class="menu__item__parent-pages not-list-style">
          <li class="menu__item__parent-pages__page" data-target="menu-list-item">
            <a href="https://www.albi-tourisme.fr/decouvrir-albi/" class="menu__item__link not-link-style">Visiter Albi</a>
            <span class="menu__item__more fi fi--chevron-right" data-trigger="wpiris-menu-list-toggle"></span>
            <ul class="menu__item__child-pages not-list-style">
              <p class="menu__item__child-pages__heading">
                <span class="menu__item__back fi fi--chevron-left" data-trigger="wpiris-menu-list-toggle"></span>
                <a class="not-link-style" href="https://www.albi-tourisme.fr/decouvrir-albi/">Visiter Albi</a>
              </p>
              <li class="menu__item__child-pages__page">
                <a href="https://www.albi-tourisme.fr/decouvrir-albi/les-incontournables/">Les incontournables</a>
              </li>
              <li class="menu__item__child-pages__page">
                <a href="https://www.albi-tourisme.fr/decouvrir-albi/patrimoine-unesco/">Un riche patrimoine UNESCO</a>
              </li>
              <li class="menu__item__child-pages__page">
                <a href="https://www.albi-tourisme.fr/decouvrir-albi/musees/">Musées et expositions</a>
              </li>
            </ul>
          </li>
        </ul>
      </li>
      <li class="menu__item">
        <ul class="menu__item__parent-pages not-list-style">
          <li class="menu__item__parent-pages__page" data-target="menu-list-item">
            <a href="https://www.albi-tourisme.fr/sejourner/" class="menu__item__link not-link-style">Organiser mon séjour</a>
            <span class="menu__item__more fi fi--chevron-right" data-trigger="wpiris-menu-list-toggle"></span>
            <ul class="menu__item__child-pages not-list-style">
              <p class="menu__item__child-pages__heading">
                <span class="menu__item__back fi fi--chevron-left" data-trigger="wpiris-menu-list-toggle"></span>
                <a class="not-link-style" href="https://www.albi-tourisme.fr/sejourner/">Organiser mon séjour</a>
              </p>
              <li class="menu__item__child-pages__page">
                <a href="https://www.albi-tourisme.fr/sejourner/sortir/">Agenda des animations</a>
              </li>
              <li class="menu__item__child-pages__page">
                <a href="https://www.albi-tourisme.fr/sejourner/hebergements/">Hébergements</a>
              </li>
              <li class="menu__item__child-pages__page">
                <a href="https://www.albi-tourisme.fr/sejourner/shopping/">Shopping</a>
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
    <div class="menu__wrapper__accelerators">
      <div class="iris-card iris-card--inner iris-card--hover-effect-fade" data-id="7">
        <div class="iris-card__wrapper">
          <div class="iris-card__content">
            <h3 class="iris-card__content__title">
              <a class="stretched-link not-link-style" href="https://www.albi-tourisme.fr/le-blog/">Le blog</a>
            </h3>
          </div>
        </div>
      </div>
      <div class="iris-card iris-card--inner iris-card--hover-effect-fade" data-id="2056">
        <div class="iris-card__wrapper">
          <div class="iris-card__content">
            <h3 class="iris-card__content__title">
              <a class="stretched-link not-link-style" href="https://www.albi-tourisme.fr/reserver-en-ligne/">Réserver en ligne</a>
            </h3>
          </div>
        </div>
      </div>
    </div>
    <ul class="menu__wrapper__accelerators--mobile not-list-style">
      <li><a class="not-link-style" href="https://www.albi-tourisme.fr/le-blog/">Le blog</a></li>
      <li><a class="not-link-style" href="https://www.albi-tourisme.fr/reserver-en-ligne/">Réserver en ligne</a></li>
    </ul>
  </div>
</div>
`;

describe("segmentPage: nested BEM 'menu' wrappers (albi-tourisme.fr shape)", () => {
  it("collapses .menu > .menu__wrapper > ul.menu__wrapper__pages into a SINGLE nav block", () => {
    document.body.innerHTML = MENU_HTML;
    const leaves = Array.from(document.body.querySelectorAll<HTMLAnchorElement>("a"));

    // segmentPage() budgets itself against wall-clock time (MAX_MS), which
    // is meant to protect a real browser's paint from a pathological page —
    // jsdom's getComputedStyle is slow enough that even this small fixture
    // can blow that budget for reasons that have nothing to do with the
    // logic under test. Freeze the clock so the test exercises the
    // classification/collapsing logic, not jsdom's performance profile.
    jest.spyOn(performance, "now").mockReturnValue(0);

    const result = segmentPage(document.body, leaves);

    expect(result.stats.bailed).toBeUndefined();

    const navBlocks = result.blocks.filter((b) => b.k === "nav");
    // Before the fix: .menu, .menu__wrapper AND ul.menu__wrapper__pages each
    // independently cleared the nav threshold, nesting 3 separate nav
    // blocks. The fix flattens all of that into exactly one.
    expect(navBlocks).toHaveLength(1);

    const nav = navBlocks[0];
    const ownLeafTexts = nav.l.map((pos) => leaves[pos]?.textContent?.trim());

    // The real menu links must be DIRECT leaves of that single nav block —
    // i.e. reachable by renderNav()'s leavesOf(block), not stranded on a
    // nested child block it never visits.
    expect(ownLeafTexts).toEqual(
      expect.arrayContaining([
        "Visiter Albi",
        "Organiser mon séjour",
        "Les incontournables",
        "Agenda des animations",
        "Le blog",
        "Réserver en ligne",
      ]),
    );
  });
});
