import { createRoot } from "solid-js";
import { afterEach, describe, expect, it } from "vitest";
import { createScrollLock } from "../../src/utilities/scroll-lock/create-scroll-lock";

describe("createScrollLock", () => {
  afterEach(() => {
    document.body.style.overflow = "";
    document.body.style.paddingRight = "";
  });

  it("sets overflow hidden on body when locked", () => {
    createRoot((dispose) => {
      const lock = createScrollLock();
      lock.lock();
      expect(document.body.style.overflow).toBe("hidden");
      lock.unlock();
      dispose();
    });
  });

  it("restores overflow on unlock", () => {
    document.body.style.overflow = "auto";
    createRoot((dispose) => {
      const lock = createScrollLock();
      lock.lock();
      lock.unlock();
      expect(document.body.style.overflow).toBe("auto");
      dispose();
    });
  });

  it("handles multiple locks — only unlocks when all release", () => {
    createRoot((dispose) => {
      const lockA = createScrollLock();
      const lockB = createScrollLock();
      lockA.lock();
      lockB.lock();
      lockA.unlock();
      expect(document.body.style.overflow).toBe("hidden"); // still locked by B
      lockB.unlock();
      expect(document.body.style.overflow).toBe(""); // now unlocked
      dispose();
    });
  });
});
