import { useSiteSettings } from '../context/SiteSettingsContext.jsx'; import { useLiveEdit } from '../context/LiveEditContext.jsx'; import { SITE_IMAGE_SLOTS, INSIGHTS_DEFAULTS, PACKAGE_TIER_DEFAULTS, defaultInsightTokens, insightSrc, } from '../constants/siteImages.js'; // Liest die austauschbaren Bilder (siehe constants/siteImages.js) inkl. noch nicht // gespeicherter Aenderungen aus dem Live-Bearbeiten-Modus. Eintrag im Live-Modus: // entity "site_images", id = Schluessel ("craft") bzw. "liste.index" ("insights.2"), // field "path", Wert = Upload-Pfad oder leer (= Standardbild). export default function useSiteImages(settingsOverride) { const context = useSiteSettings(); const live = useLiveEdit(); const stored = (settingsOverride || context).site_images || {}; const value = (id, original) => live.getValue('site_images', id, 'path', 'de', original || '') || ''; /** Aktuelles Bild eines Einzelplatzes: { src, custom } - custom = eigener Upload-Pfad. */ const slot = (key) => { const custom = value(key, stored[key]); return { src: custom || SITE_IMAGE_SLOTS[key].src, custom }; }; /** "Einblicke"-Fotos: [{ id, src, width?, height?, custom }] */ const insights = () => { const list = Array.isArray(stored.insights) ? stored.insights : defaultInsightTokens(); return list .map((token, i) => { let current = value(`insights.${i}`, token); // Leer = wieder das eingebaute Foto an dieser Stelle (wie im Backend). if (!current) current = i < INSIGHTS_DEFAULTS.length ? `default:${i}` : ''; const builtIn = /^default:(\d+)$/.exec(current); const dims = builtIn ? INSIGHTS_DEFAULTS[Number(builtIn[1])] : {}; return { id: `insights.${i}`, src: insightSrc(current), width: dims.width, height: dims.height, custom: builtIn ? '' : current }; }) .filter((item) => item.src); }; /** Hintergrundbild einer Paketstufe (0-7). */ const packageTier = (index) => { const list = Array.isArray(stored.package_tiers) ? stored.package_tiers : []; const custom = value(`package_tiers.${index}`, list[index]); return { src: custom || PACKAGE_TIER_DEFAULTS[index], custom }; }; return { slot, insights, packageTier }; }