'use client';

/**
 * PageRenderer - renders a list of pre-hydrated page-organizer modules.
 *
 * Receives LoadedModule[] from the server (data already fetched via loadPageForRender).
 * Maps each module to its Component from MODULE_REGISTRY. Skips unknown types.
 *
 * `data === null` is *not* a skip signal — config-only modules (page-hero, banner,
 * static-content) return null from loadData by design, and deal-row Components
 * handle null by coalescing to an empty list (and optionally rendering an
 * empty-row heading in preview mode).
 */

import { CLIENT_MODULE_REGISTRY } from '../client-registry';
import type { LoadedModule } from '../loader';
import { useT } from '@/lib/i18n/react';

export function PageRenderer({ modules }: { modules: LoadedModule[] }) {
  const t = useT('page_organizer');
  return (
    <>
      {modules.map((m) => {
        const def = CLIENT_MODULE_REGISTRY[m.type];
        if (!def) return null;
        const Cmp = def.Component;
        const rendered = <Cmp config={m.config} data={m.data} />;
        if (m.hiddenInProd) {
          return (
            <div
              key={m.instanceId}
              className="relative opacity-50"
              aria-label="Hidden in production"
            >
              <div className="pointer-events-none absolute end-2 top-2 z-10 rounded-full bg-neutral-900/80 px-2 py-0.5 text-xs font-medium text-white">
                {t('hidden_in_prod_badge')}
              </div>
              {rendered}
            </div>
          );
        }
        return <div key={m.instanceId}>{rendered}</div>;
      })}
    </>
  );
}
