import {
  classSignature,
  findRepeatGroups,
  FingerprintNode,
  structuralFingerprint,
} from "../../../src/adapters/reader/structure/fingerprint";

function node(localName: string, className = "", children: FingerprintNode[] = []): FingerprintNode {
  return { localName, className, children };
}

function card(_title: string, className = "card"): FingerprintNode {
  return node("div", className, [
    node("img", "thumb"),
    node("h3", "title", [node("a", "link")]),
    node("p", "summary"),
  ]);
}

describe("classSignature", () => {
  it("drops classes containing digits", () => {
    expect(classSignature("col-4 item-2 mt-8")).toBe("");
  });

  it("drops state prefixes and state suffixes", () => {
    expect(classSignature("is-active js-toggle has-icon no-border current open")).toBe("");
  });

  it("drops CSS-module / emotion-style hashes", () => {
    expect(classSignature("cardTitle3xK9z")).toBe("");
  });

  it("keeps up to 3 structurally meaningful classes, sorted", () => {
    expect(classSignature("card featured wide")).toBe("card.featured.wide");
  });
});

describe("structuralFingerprint", () => {
  it("is equal for the same structure with different text content", () => {
    const a = node("div", "card", [node("h3", "title"), node("p", "body")]);
    const b = node("div", "card", [node("h3", "title"), node("p", "body")]);
    expect(structuralFingerprint(a)).toBe(structuralFingerprint(b));
  });

  it("differs when a child is added", () => {
    const a = node("div", "card", [node("h3", "title")]);
    const b = node("div", "card", [node("h3", "title"), node("p", "body")]);
    expect(structuralFingerprint(a)).not.toBe(structuralFingerprint(b));
  });

  it("ignores utility/index classes so index-varying siblings still match", () => {
    const a = node("div", "card item-1", [node("h3", "title")]);
    const b = node("div", "card item-2", [node("h3", "title")]);
    expect(structuralFingerprint(a)).toBe(structuralFingerprint(b));
  });

  it("ignores state classes so active/inactive siblings still match", () => {
    const a = node("div", "card is-active", [node("h3", "title")]);
    const b = node("div", "card", [node("h3", "title")]);
    expect(structuralFingerprint(a)).toBe(structuralFingerprint(b));
  });
});

describe("findRepeatGroups", () => {
  it("finds a group of >=3 structurally identical siblings", () => {
    const siblings = [card("A"), card("B"), card("C"), card("D")];
    const groups = findRepeatGroups(siblings, {
      textLengthOf: () => 50,
      wordCountOf: () => 10,
      linkDensityOf: () => 0.3,
    });
    expect(groups.length).toBeGreaterThan(0);
    expect(groups[0].members.length).toBe(4);
  });

  it("rejects a group below the similarity threshold", () => {
    const siblings = [card("A"), card("B"), node("div", "different", [node("span")])];
    const groups = findRepeatGroups(siblings, {
      minSimilarity: 0.9,
      textLengthOf: () => 50,
      wordCountOf: () => 10,
      linkDensityOf: () => 0.3,
    });
    expect(groups.find((g) => g.members.length >= 2)).toBeUndefined();
  });

  it("rejects an icon row (median text below minMedianText)", () => {
    const siblings = [card("A"), card("B"), card("C")];
    const groups = findRepeatGroups(siblings, {
      textLengthOf: () => 2, // icon labels only
      wordCountOf: () => 1,
      linkDensityOf: () => 0.1,
    });
    expect(groups.length).toBe(0);
  });

  it("rejects a link list (high link density, very short words)", () => {
    const siblings = [card("A"), card("B"), card("C")];
    const groups = findRepeatGroups(siblings, {
      textLengthOf: () => 20,
      wordCountOf: () => 2,
      linkDensityOf: () => 0.95,
    });
    expect(groups.length).toBe(0);
  });

  it("returns no groups for fewer than 3 non-identical eligible siblings", () => {
    const siblings = [card("A"), node("div", "other", [node("span")])];
    const groups = findRepeatGroups(siblings, {
      textLengthOf: () => 50,
      wordCountOf: () => 10,
      linkDensityOf: () => 0.3,
    });
    expect(groups.length).toBe(0);
  });

  /** A `layout_latest` press-clipping teaser: figure>a, p.info>time, h2>a, a variable summary slot, p.more>a. */
  function pressTeaser(summary: FingerprintNode[]): FingerprintNode {
    return node("div", "layout_latest arc_4 block", [
      node("figure", "image_container", [node("a", "", [node("img", "")])]),
      node("p", "info", [node("time", "")]),
      node("h2", "", [node("a", "")]),
      node("div", "ce_text", summary),
      node("p", "more", [node("a", "")]),
    ]);
  }

  it("falls back to a shallower (depth-1) comparison when every card's summary slot varies enough to break the depth-2 fingerprint (real handilab.com press-review shape)", () => {
    const siblings = [
      pressTeaser([]), // no summary at all
      pressTeaser([node("p", "article__intro"), node("div", "optin-newsletter-main")]), // summary + injected embed
      pressTeaser([node("p", "")]), // bare paragraph
      pressTeaser([node("header", "article-top", [node("p", "chapo")]), node("div", "flex")]), // different wrapper tag
      pressTeaser([node("div", "t-content__above-sidebar"), node("div", "t-content__with-sidebar")]),
      pressTeaser([node("p", "")]), // bare paragraph, matches a depth-2 sibling but only 2/6
      pressTeaser([node("p", "col subheadline widescreen-12")]),
      pressTeaser([node("p", "text font-primary text--lead")]),
    ];

    const groups = findRepeatGroups(siblings, {
      textLengthOf: () => 50,
      wordCountOf: () => 10,
      linkDensityOf: () => 0.3,
    });

    expect(groups.length).toBeGreaterThan(0);
    expect(groups[0].members.length).toBe(siblings.length);
  });
});
