import { useEffect } from "react";

// NOTE: this hook locks `document.body.style.overflow` only. The app layout
// scrolls on `<html>` (GlobalStyles sets `html { overflow-x: hidden }`, which
// makes `<html>` the scroll container), so a body-only lock may not fully
// prevent background scroll. The kit's Modal/ConfirmDialog use
// `useDialogScrollLock`, which correctly locks `<html>`. This hook is retained
// only for the two non-kit in-page edit modals that still call it
// (AttendanceAdmin, WarehouseItemDetail); prefer `useDialogScrollLock` for new
// dialogs. Behavior intentionally left unchanged to avoid regressing those
// pages — see REVIEW_FINDINGS (useModalLock LOW).
let activeLocks = 0;

export default function useModalLock(isOpen: boolean): void {
  useEffect(() => {
    if (isOpen) {
      if (activeLocks === 0) document.body.style.overflow = "hidden";
      activeLocks++;
      return () => {
        activeLocks = Math.max(0, activeLocks - 1);
        if (activeLocks === 0) document.body.style.overflow = "";
      };
    }
  }, [isOpen]);
}
