import { describe, expect, it } from "vitest";
import { createRoot } from "solid-js";
import { createVirtualizer } from "../../src/primitives/create-virtualizer";

describe("createVirtualizer", () => {
  it("calculates visible range for fixed-size items", () => {
    createRoot((dispose) => {
      const virtualizer = createVirtualizer({
        count: () => 1000,
        getScrollElement: () => null,
        estimateSize: () => 40,
      });
      expect(virtualizer.totalSize()).toBe(40000);
      dispose();
    });
  });

  it("returns virtual items with correct offsets", () => {
    createRoot((dispose) => {
      const virtualizer = createVirtualizer({
        count: () => 100,
        getScrollElement: () => null,
        estimateSize: () => 50,
        overscan: 0,
      });
      const items = virtualizer.virtualItems();
      expect(items.length).toBeGreaterThanOrEqual(0);
      dispose();
    });
  });

  it("handles zero count", () => {
    createRoot((dispose) => {
      const virtualizer = createVirtualizer({
        count: () => 0,
        getScrollElement: () => null,
        estimateSize: () => 40,
      });
      expect(virtualizer.totalSize()).toBe(0);
      expect(virtualizer.virtualItems()).toEqual([]);
      dispose();
    });
  });

  it("supports horizontal orientation", () => {
    createRoot((dispose) => {
      const virtualizer = createVirtualizer({
        count: () => 50,
        getScrollElement: () => null,
        estimateSize: () => 100,
        horizontal: true,
      });
      expect(virtualizer.totalSize()).toBe(5000);
      dispose();
    });
  });
});
