import { renderLeaf } from "../render-leaf";
import { BlockTreeNode } from "../block-tree";
import { RenderContext } from "../render-context";

export function renderCardList(block: BlockTreeNode, ctx: RenderContext, parent: HTMLElement): void {
  const section = document.createElement("section");
  section.className = "lisio-cards-section";
  if (block.n != undefined) {
    const heading = document.createElement("h2");
    heading.textContent = block.n;
    section.append(heading);
  }
  const list = document.createElement("ul");
  list.className = "lisio-cards";
  for (const child of block.children) {
    if (child.k !== "card") continue;
    const li = document.createElement("li");
    renderCard(child, ctx, li);
    if (li.childNodes.length > 0) list.append(li);
  }
  if (list.children.length > 0) {
    section.append(list);
    parent.append(section);
  }
}

/**
 * Fixed slot order: image -> title (as an <h3><a>) -> summary -> meta ->
 * a "Lire la suite" link. Unslotted leaves are appended after, so nothing
 * from the source page is silently dropped even when slot detection
 * (card-slots.ts) misses something.
 *
 * The image (when present) and everything else are two separate flex
 * children — `.lisio-card-content` wraps every text slot in its own box so
 * the CSS can lay the card out as image-beside-text on a wide list and
 * image-above-text once it no longer fits, without caring which slots a
 * given card actually has. `.lisio-cards` still stacks cards one per row;
 * it's each card's own internal layout that adapts.
 */
export function renderCard(block: BlockTreeNode, ctx: RenderContext, parent: HTMLElement): void {
  const article = document.createElement("article");
  article.className = "lisio-card";

  const leaves = ctx.leavesOf(block);
  const byPosition = new Map(leaves.map((l) => [l.position, l]));
  const used = new Set<number>();
  const slots = block.s ?? {};

  const imageLeaf = slots.image != undefined ? byPosition.get(slots.image) : undefined;
  if (imageLeaf != undefined) {
    renderLeaf(imageLeaf, article, ctx.calendarLang);
    used.add(imageLeaf.position);
  }

  const content = document.createElement("div");
  content.className = "lisio-card-content";

  const titleLeaf = slots.title != undefined ? byPosition.get(slots.title) : undefined;
  if (titleLeaf != undefined) {
    const h3 = document.createElement("h3");
    renderLeaf(titleLeaf, h3, ctx.calendarLang);
    content.append(h3);
    used.add(titleLeaf.position);
  }

  const summaryLeaf = slots.summary != undefined ? byPosition.get(slots.summary) : undefined;
  if (summaryLeaf != undefined) {
    const p = document.createElement("p");
    p.className = "lisio-card-summary";
    renderLeaf(summaryLeaf, p, ctx.calendarLang);
    content.append(p);
    used.add(summaryLeaf.position);
  }

  if (slots.meta != undefined && slots.meta.length > 0) {
    const metaP = document.createElement("p");
    metaP.className = "lisio-card-meta";
    for (const pos of slots.meta) {
      const metaLeaf = byPosition.get(pos);
      if (metaLeaf == undefined) continue;
      renderLeaf(metaLeaf, metaP, ctx.calendarLang);
      used.add(pos);
    }
    if (metaP.childNodes.length > 0) content.append(metaP);
  }

  const linkLeaf =
    slots.link != undefined && !used.has(slots.link) ? byPosition.get(slots.link) : undefined;
  if (linkLeaf != undefined) {
    // Rendered via renderLeaf (not hand-built) so it keeps the exact same
    // dataset/click-listener wiring as every other leaf in reading mode.
    const wrapper = document.createElement("span");
    wrapper.className = "lisio-card-more";
    renderLeaf(linkLeaf, wrapper, ctx.calendarLang);
    if (wrapper.childNodes.length > 0) content.append(...wrapper.childNodes);
    used.add(linkLeaf.position);
  }

  for (const leaf of leaves) {
    if (used.has(leaf.position)) continue;
    renderLeaf(leaf, content, ctx.calendarLang);
  }

  if (content.childNodes.length > 0) article.append(content);
  if (article.childNodes.length > 0) parent.append(article);
}
