import type { NavigateFunction } from 'react-router-dom';

// Offset to account for sticky navbar height (65px) plus some padding
const SCROLL_OFFSET = 65;

function isScrollablePath(pathname: string): boolean {
  return (
    pathname === '/de/' ||
    pathname === '/en/' ||
    pathname === '/de/technical' ||
    pathname === '/en/technical' ||
    // legacy fallbacks
    pathname === '/' ||
    pathname === '/technical'
  );
}

/** Returns /de/technical or /en/technical depending on the current path prefix. */
function getTechnicalPath(currentPath: string): string {
  if (currentPath.startsWith('/en')) return '/en/technical';
  return '/de/technical';
}

export function scrollToSection(
  sectionId: string,
  currentPath: string,
  navigate?: NavigateFunction
): void {
  if (isScrollablePath(currentPath)) {
    const element = document.getElementById(sectionId);
    if (element) {
      const elementPosition = element.getBoundingClientRect().top;
      const offsetPosition = elementPosition + window.scrollY - SCROLL_OFFSET;

      window.scrollTo({
        top: offsetPosition,
        behavior: 'smooth'
      });
    }
  } else if (navigate) {
    navigate(`${getTechnicalPath(currentPath)}#${sectionId}`);
  } else {
    globalThis.location.href = `${getTechnicalPath(currentPath)}#${sectionId}`;
  }
}

export function scrollToProjects(
  currentPath: string,
  navigate?: NavigateFunction
): void {
  scrollToSection('projects', currentPath, navigate);
}
