// SPA Router using History API
let routeHandler = null;

export function initRouter(handler) {
  routeHandler = handler;

  // Handle browser back/forward
  window.addEventListener('popstate', () => {
    if (routeHandler) {
      routeHandler(window.location.pathname);
    }
  });

  // Handle initial load
  const path = window.location.pathname;
  if (routeHandler && path !== '/') {
    routeHandler(path);
  }
}

export function navigate(path) {
  if (window.location.pathname !== path) {
    window.history.pushState({}, '', path);
    if (routeHandler) {
      routeHandler(path);
    }
  }
}

export function getCurrentRoute() {
  return window.location.pathname;
}

export function goBack() {
  window.history.back();
}
