import { useEffect, useMemo, useState } from 'react' import { getCachedRemoteConfig, subscribeRemoteConfig } from '@/services/remoteConfig' import { DEFAULT_PROMO, MOCK_ROSTER, SEED_CAFES, SEED_EBOOKS, SEED_GIGS, SEED_MEETUPS, SEED_VIDEOS, SOCIAL_COPY, type SocialLandingCopy } from '@/data/socialSeed' import { isSocialButtonEnabled, isSocialFeatureEnabled, isSocialPageEnabled, isSocialSectionEnabled, resolveSocialOps, } from '@/utils/socialOps' function catalog(value: unknown, fallback: T[], valid: (row: any) => boolean): T[] { if (!Array.isArray(value)) return fallback const seen = new Set() const rows = value.filter((row) => { if (!row || typeof row.id !== 'string' || !row.id || seen.has(row.id) || !valid(row)) return false seen.add(row.id) return true }) return value.length && !rows.length ? fallback : rows as T[] } const text = (row: any, keys: string[]) => keys.every((key) => typeof row[key] === 'string') const tags = (row: any) => Array.isArray(row.tags) && row.tags.every((tag: unknown) => typeof tag === 'string') export function useSocialContent() { const [config, setConfig] = useState(getCachedRemoteConfig) useEffect(() => subscribeRemoteConfig((result) => setConfig(result.config)), []) return useMemo(() => { const source = { ...config.socialContent, ...config.copy.socialCatalogs } const copy = { ...SOCIAL_COPY } as SocialLandingCopy for (const key of Object.keys(copy) as (keyof SocialLandingCopy)[]) { const value = config.copy[key] ?? source.copy?.[key] if (typeof value === 'string') copy[key] = value } const ops = resolveSocialOps(config) const hiddenSections = (Object.keys(ops.sections) as Array).filter( (key) => !ops.sections[key], ) return { copy, pages: config.copy.socialPages || {}, promoUrl: typeof config.copy.socialPromoUrl === 'string' ? config.copy.socialPromoUrl : typeof source.promoUrl === 'string' ? source.promoUrl : DEFAULT_PROMO, hiddenSections, ops, pageOn: (key: Parameters[1]) => isSocialPageEnabled(ops, key), sectionOn: (key: Parameters[1]) => isSocialSectionEnabled(ops, key), featureOn: (key: Parameters[1]) => isSocialFeatureEnabled(ops, key), buttonOn: (key: Parameters[1]) => isSocialButtonEnabled(ops, key), roster: catalog(source.roster, MOCK_ROSTER, (r) => text(r, ['name', 'title', 'city', 'bio', 'lookingFor', 'avatar']) && tags(r) && (!r.gallery || (Array.isArray(r.gallery) && r.gallery.every((url: unknown) => typeof url === 'string'))) && (!r.languages || (Array.isArray(r.languages) && r.languages.every((value: unknown) => typeof value === 'string')))), gigs: catalog(source.gigs, SEED_GIGS, (r) => text(r, ['title', 'role', 'pay']) && tags(r)), meetups: catalog(source.meetups, SEED_MEETUPS, (r) => text(r, ['title', 'city', 'date', 'time', 'mirotalkRoom', 'channelId'])), cafes: catalog(source.cafes, SEED_CAFES, (r) => text(r, ['name', 'city', 'vibe']) && Number.isFinite(r.wifiMbps)), videos: catalog(source.videos, SEED_VIDEOS, (r) => text(r, ['title', 'city', 'summary', 'coverEmoji']) && Number.isFinite(r.minutes)).map((video) => ({ ...video, url: config.copy.socialVideoSources?.[video.id] || video.url })), ebooks: catalog(source.ebooks, SEED_EBOOKS, (r) => text(r, ['title', 'blurb']) && Number.isFinite(r.chapters)), } }, [config]) }