import { TENDER_SECTION_SCROLL_OFFSET_PX } from "./constants";

const SECTION_SCROLL_GAP_PX = 8;

/** Pixels to leave above the section top (sticky header + small gap). */
export function getTenderSectionScrollOffset(): number {
  if (typeof document === "undefined") {
    return TENDER_SECTION_SCROLL_OFFSET_PX + SECTION_SCROLL_GAP_PX;
  }
  const header = document.querySelector("header");
  const headerHeight = header?.getBoundingClientRect().height ?? 0;
  return (
    Math.max(TENDER_SECTION_SCROLL_OFFSET_PX, headerHeight) +
    SECTION_SCROLL_GAP_PX
  );
}

export function scrollToTenderSection(id: string) {
  const el = document.getElementById(id);
  if (!el) return;

  const reduceMotion =
    typeof window !== "undefined" &&
    window.matchMedia("(prefers-reduced-motion: reduce)").matches;

  const offset = getTenderSectionScrollOffset();
  const top =
    el.getBoundingClientRect().top + window.scrollY - offset;

  window.scrollTo({
    top: Math.max(0, top),
    behavior: reduceMotion ? "auto" : "smooth",
  });
}
