import { loader } from 'fumadocs-core/source';
import { createMDXSource } from 'fumadocs-mdx';
import * as LucideIcons from 'lucide-react';
import { createElement } from 'react';
import { changelog, docs, pages } from '../../../.source';
import { docsI18nConfig } from './i18n';

/**
 * Turn a content source into a unified interface
 * .source folder is generated by `fumadocs-mdx`
 *
 * https://fumadocs.dev/docs/headless/source-api
 * https://fumadocs.dev/docs/headless/content-collections
 */
export const source = loader({
  baseUrl: '/docs',
  i18n: docsI18nConfig,
  source: docs.toFumadocsSource(),
  icon(iconName) {
    if (!iconName) {
      return undefined;
    }

    const IconComponent = (LucideIcons as Record<string, any>)[iconName];
    if (IconComponent) {
      return createElement(IconComponent);
    }

    console.warn(`Icon not found: ${iconName}`);
    return undefined;
  },
});

/**
 * Changelog source
 */
export const changelogSource = loader({
  baseUrl: '/changelog',
  i18n: docsI18nConfig,
  source: createMDXSource(changelog),
});

/**
 * Pages source
 */
export const pagesSource = loader({
  baseUrl: '/pages',
  i18n: docsI18nConfig,
  source: createMDXSource(pages),
});
