---
import { Image } from "astro:assets";
// Header component with scroll-based background and enhanced mobile animations
// Use local backend in development, production URL from dev.vars/wrangler
const isDev = import.meta.env.DEV;
const PAYLOAD_CMS_URL = isDev
? "http://localhost:3000" // Local backend in dev (port may vary)
: import.meta.env.PAYLOAD_CMS_URL || "https://enchun-admin.anlstudio.cc";
// Fetch navigation data from Payload CMS server-side
let navItems: any[] = [];
try {
const response = await fetch(
`${PAYLOAD_CMS_URL}/api/globals/header?depth=2&draft=false&locale=undefined&trash=false`,
);
if (response.ok) {
const data = await response.json();
navItems = data?.navItems || data || [];
}
} catch (error) {
console.error("[Header SSR] Failed to fetch navigation:", error);
}
// Helper to get link URL
function getLinkUrl(link: any): string {
if (!link) return "#";
if (link.type === "custom" && link.url) {
return link.url;
}
if (link.type === "reference" && link.reference?.value) {
if (typeof link.reference.value === "string") {
// It's an ID, construct URL based on relationTo
return `/${link.reference.relationTo || "pages"}/${link.reference.value}`;
} else if (link.reference.value.slug) {
// It's a populated object with slug
return `/${link.reference.value.slug}`;
}
}
return "#";
}
// Check if label should have a badge
function getBadgeForLabel(label: string): string {
if (label.includes("行銷方案")) {
return `Hot`;
}
if (label.includes("行銷放大鏡")) {
return `New`;
}
return "";
}
// Check if link is active
function isLinkActive(url: string): boolean {
const currentPath = Astro.url.pathname;
return currentPath === url || (url === "/" && currentPath === "/");
}
---