import { docs } from '@/.source';
import { loader } from 'fumadocs-core/source';
import { createElement, type ComponentType } from 'react';
import { icons } from 'lucide-react';
import { attachFile } from './attach-file';
import { attachSeparator } from './attach-separator';

// Loosen types to avoid strict coupling to fumadocs internal types at build time
type PageShape = {
  data: {
    body: ComponentType<any>;
    toc?: unknown;
    full?: boolean;
    title: string;
    description?: string;
    author?: { name: string; url?: string } | undefined;
  };
};

export const source = loader({
  baseUrl: '/docs',
  source: docs.toFumadocsSource(),
  pageTree: {
    attachFile,
    attachSeparator,
  },
  icon(icon) {
    if (!icon) {
      return;
    }

    if (icon in icons) return createElement(icons[icon as keyof typeof icons]);
  },
}) as unknown as {
  getPage: (slug?: string[]) => PageShape | undefined;
  // Minimal shape used by sitemap: url + file.path
  getPages: () => Array<{ url: string; file: { path: string } }>;
  generateParams: () => Promise<any> | any;
  pageTree: unknown;
};
