import type { SampleEntry } from '../../samples/registry';
import type { SampleModule } from '../../widgets/core';

const componentSampleModules = import.meta.glob('../../components/**/samples/*.tsx') as Record<
  string,
  () => Promise<SampleModule>
>;

const widgetSampleModules = import.meta.glob('../../widgets/**/samples/*.tsx') as Record<
  string,
  () => Promise<SampleModule>
>;

function createSampleEntries(
  modules: Record<string, () => Promise<SampleModule>>,
): SampleEntry[] {
  return Object.entries(modules)
    .map(([modulePath, load]) => ({
      modulePath,
      load,
    }))
    .sort((left, right) => left.modulePath.localeCompare(right.modulePath));
}

export const componentSampleEntries = createSampleEntries(componentSampleModules);
export const widgetSampleEntries = createSampleEntries(widgetSampleModules);
