import { describe, it, expect } from "vitest";
import { parseResultsText } from "../parse-results-text";

describe("parseResultsText", () => {
  describe("dot separator", () => {
    it("parses '1. Player Name'", () => {
      expect(parseResultsText("1. Gerwyn Price")).toEqual([
        { placement: 1, rawName: "Gerwyn Price" },
      ]);
    });

    it("parses '1.Player Name' (no space after dot)", () => {
      expect(parseResultsText("1.Gerwyn Price")).toEqual([
        { placement: 1, rawName: "Gerwyn Price" },
      ]);
    });
  });

  describe("comma separator", () => {
    it("parses '1, Player Name'", () => {
      expect(parseResultsText("1, Gerwyn Price")).toEqual([
        { placement: 1, rawName: "Gerwyn Price" },
      ]);
    });
  });

  describe("space-only separator", () => {
    it("parses '1 Player Name'", () => {
      expect(parseResultsText("1 Gerwyn Price")).toEqual([
        { placement: 1, rawName: "Gerwyn Price" },
      ]);
    });
  });

  describe("T-prefix ties", () => {
    it("parses 'T3. Player Name' as placement 3", () => {
      expect(parseResultsText("T3. Luke Littler")).toEqual([
        { placement: 3, rawName: "Luke Littler" },
      ]);
    });

    it("parses 'T3, Player Name' as placement 3", () => {
      expect(parseResultsText("T3, Luke Littler")).toEqual([
        { placement: 3, rawName: "Luke Littler" },
      ]);
    });

    it("parses two T-prefix lines with same rank as both placement 3", () => {
      const input = "T3. Luke Littler\nT3. Michael van Gerwen";
      expect(parseResultsText(input)).toEqual([
        { placement: 3, rawName: "Luke Littler" },
        { placement: 3, rawName: "Michael van Gerwen" },
      ]);
    });
  });

  describe("repeated-rank ties", () => {
    it("parses two lines with the same rank number as both tied", () => {
      const input = "3. Luke Littler\n3. Michael van Gerwen";
      expect(parseResultsText(input)).toEqual([
        { placement: 3, rawName: "Luke Littler" },
        { placement: 3, rawName: "Michael van Gerwen" },
      ]);
    });
  });

  describe("blank lines and whitespace", () => {
    it("ignores blank lines", () => {
      const input = "1. Gerwyn Price\n\n2. Luke Littler";
      expect(parseResultsText(input)).toEqual([
        { placement: 1, rawName: "Gerwyn Price" },
        { placement: 2, rawName: "Luke Littler" },
      ]);
    });

    it("trims leading and trailing whitespace from names", () => {
      expect(parseResultsText("1.  Gerwyn Price  ")).toEqual([
        { placement: 1, rawName: "Gerwyn Price" },
      ]);
    });

    it("handles \\r\\n line endings", () => {
      const input = "1. Gerwyn Price\r\n2. Luke Littler";
      expect(parseResultsText(input)).toEqual([
        { placement: 1, rawName: "Gerwyn Price" },
        { placement: 2, rawName: "Luke Littler" },
      ]);
    });
  });

  describe("multi-line mixed formats", () => {
    it("parses a realistic paste with multiple formats", () => {
      const input = [
        "1. Gerwyn Price",
        "2, Luke Littler",
        "T3. Michael van Gerwen",
        "T3. Peter Wright",
        "",
        "5 Damon Heta",
      ].join("\n");
      expect(parseResultsText(input)).toEqual([
        { placement: 1, rawName: "Gerwyn Price" },
        { placement: 2, rawName: "Luke Littler" },
        { placement: 3, rawName: "Michael van Gerwen" },
        { placement: 3, rawName: "Peter Wright" },
        { placement: 5, rawName: "Damon Heta" },
      ]);
    });
  });
});
