import { describe, expect, it } from "vitest";
import {
  PASTE_COMPONENT_ARRAY_ERROR,
  PASTE_COMPONENT_EMPTY_ERROR,
  PASTE_COMPONENT_INVALID_JSON_ERROR,
  PASTE_COMPONENT_UNSUPPORTED_ERROR,
  extractImageUrlsFromContent,
  parsePastedComponentJson,
} from "@/lib/pasteComponent";

const components = [{ id: 1, name: "Hero", slug: "hero" }];

describe("parsePastedComponentJson", () => {
  it("rejects empty and whitespace-only input", () => {
    expect(parsePastedComponentJson("", components).ok).toBe(false);
    expect(parsePastedComponentJson("   ", components)).toMatchObject({
      ok: false,
      error: PASTE_COMPONENT_EMPTY_ERROR,
      category: "empty",
    });
    expect(parsePastedComponentJson("\n\t  \n", components)).toMatchObject({
      ok: false,
      error: PASTE_COMPONENT_EMPTY_ERROR,
      category: "empty",
    });
  });

  it("rejects plain text and emoji clipboard content", () => {
    expect(parsePastedComponentJson("Hello world", components)).toMatchObject({
      ok: false,
      error: PASTE_COMPONENT_UNSUPPORTED_ERROR,
      category: "unsupported_clipboard",
    });
    expect(parsePastedComponentJson("🎉 Party!", components)).toMatchObject({
      ok: false,
      error: PASTE_COMPONENT_UNSUPPORTED_ERROR,
      category: "unsupported_clipboard",
    });
  });

  it("rejects JSON arrays with a clear message", () => {
    expect(parsePastedComponentJson('[{"componentId":1}]', components)).toMatchObject({
      ok: false,
      error: PASTE_COMPONENT_ARRAY_ERROR,
      category: "invalid_json",
    });
  });

  it("rejects malformed JSON without raw parser errors", () => {
    expect(parsePastedComponentJson("{not valid json", components)).toMatchObject({
      ok: false,
      error: PASTE_COMPONENT_INVALID_JSON_ERROR,
      category: "invalid_json",
    });
  });

  it("rejects missing required fields with user-friendly categories", () => {
    expect(parsePastedComponentJson(JSON.stringify({ componentName: "Hero" }), components)).toMatchObject({
      ok: false,
      category: "missing_required",
      field: "componentId",
    });
  });

  it("accepts valid component JSON with preview summary", () => {
    const result = parsePastedComponentJson(
      JSON.stringify({
        componentId: 1,
        componentName: "Hero",
        componentSlug: "hero",
        content: { title: "Welcome", heroImage: "/images/hero.jpg" },
      }),
      components
    );
    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.block.componentName).toBe("Hero");
      expect(result.block.componentSlug).toBe("hero");
      expect(result.summary.fieldCount).toBeGreaterThan(0);
      expect(result.summary.statusLabel).toBe("Valid");
      expect(Array.isArray(result.summary.imageUrls)).toBe(true);
    }
  });

  it("rejects unsupported component types with no renderer and no library match", () => {
    const result = parsePastedComponentJson(
      JSON.stringify({
        componentId: 9999,
        componentName: "Unknown Widget",
        componentSlug: "unknown-widget-xyz",
        content: {},
      }),
      components
    );
    expect(result.ok).toBe(false);
    if (result.ok === false) {
      expect(result.category).toBe("unsupported_type");
    }
  });
});

describe("extractImageUrlsFromContent", () => {
  it("finds image-like strings in nested content", () => {
    const urls = extractImageUrlsFromContent({
      cards: [{ imageUrl: "https://example.com/photo.png" }],
    });
    expect(urls).toContain("https://example.com/photo.png");
  });
});
