import TextUtil from "./TextUtil";

describe("TextUtil", () => {
  it.each(["", " ", "  "])("isEmpty=true for value='%s'", (value) => {
    expect(TextUtil.isEmpty(value)).toEqual(true);
  });

  it.each([undefined, null, [], {}, 5])(
    "isEmpty=true for non-string value='%s'",
    (value) => {
      expect(TextUtil.isEmpty(value)).toEqual(true);
    },
  );

  it.each(["a", " Ab ", "Text"])("isEmpty=false for value='%s'", (value) => {
    expect(TextUtil.isEmpty(value)).toEqual(false);
  });

  it("sanitize", () => {
    expect(
      TextUtil.sanitizeUnescapedQuotesAndLoadJsonStr<object>(`{
  "answer": "A",
  "question": "Mida tähendab sõna "pankrot"?",
  "answers": {
    "A": "Maksujõuetus",
    "B": "Pangatöötaja",
    "C": "Pankurite pidustus",
    "D": "Rikas rott"
  },
  "reason": "The correct answer is A: Maksujõuetus because this is the proper legal and economic term for bankruptcy, describing a situation where an entity is unable to pay its debts. This is the formal definition used in business and legal contexts."
}`),
    ).toEqual({
      answer: "A",
      answers: {
        A: "Maksujõuetus",
        B: "Pangatöötaja",
        C: "Pankurite pidustus",
        D: "Rikas rott",
      },
      question: 'Mida tähendab sõna "pankrot"?',
      reason:
        "The correct answer is A: Maksujõuetus because this is the proper legal and economic term for bankruptcy, describing a situation where an entity is unable to pay its debts. This is the formal definition used in business and legal contexts.",
    });
  });

  describe("trim", () => {
    test("removes all whitespace from string", () => {
      expect(TextUtil.trim("hello world")).toBe("helloworld");
    });

    test("removes tabs and newlines", () => {
      expect(TextUtil.trim("hello\tworld\n")).toBe("helloworld");
    });

    test("returns undefined for undefined input", () => {
      expect(TextUtil.trim(undefined)).toBeUndefined();
    });

    test("returns undefined for null input", () => {
      expect(TextUtil.trim(null)).toBeUndefined();
    });

    test("returns empty string for empty string input", () => {
      expect(TextUtil.trim("")).toBe("");
    });

    test("removes all types of whitespace", () => {
      expect(TextUtil.trim(" \t\n\r\f\v ")).toBe("");
    });
  });

  describe("sanitizeUnescapedQuotesAndLoadJsonStr - edge cases", () => {
    test("handles valid JSON without unescaped quotes", () => {
      const validJson = '{"name": "test", "value": 123}';
      const result = TextUtil.sanitizeUnescapedQuotesAndLoadJsonStr<{
        name: string;
        value: number;
      }>(validJson);
      expect(result).toEqual({ name: "test", value: 123 });
    });

    test("throws error for completely invalid JSON", () => {
      const invalidJson = "not json at all";
      expect(() => {
        TextUtil.sanitizeUnescapedQuotesAndLoadJsonStr(invalidJson);
      }).toThrow();
    });

    test("handles multiple unescaped quotes", () => {
      const jsonWithMultipleQuotes = '{"text": "He said "hello" and "goodbye""}';
      const result = TextUtil.sanitizeUnescapedQuotesAndLoadJsonStr<{
        text: string;
      }>(jsonWithMultipleQuotes);
      expect(result.text).toBe('He said "hello" and "goodbye"');
    });

    test("handles empty object", () => {
      const emptyJson = "{}";
      const result = TextUtil.sanitizeUnescapedQuotesAndLoadJsonStr<object>(
        emptyJson,
      );
      expect(result).toEqual({});
    });

    test("handles array JSON", () => {
      const arrayJson = '["item1", "item2"]';
      const result = TextUtil.sanitizeUnescapedQuotesAndLoadJsonStr<string[]>(
        arrayJson,
      );
      expect(result).toEqual(["item1", "item2"]);
    });
  });
});
