import type { IModuleClient, ModuleSettingsEntryContribution } from "@erp/core/client"; import type { SettingsSectionCatalogItem } from "./settings-section-catalog"; export interface SettingsNavigationEntry extends ModuleSettingsEntryContribution { moduleName: string; } export interface SettingsNavigationSection extends SettingsSectionCatalogItem { entries: SettingsNavigationEntry[]; } const buildSectionMap = (sections: SettingsSectionCatalogItem[]) => { return new Map(sections.map((section) => [section.id, section])); }; export const buildSettingsNavigation = ({ modules, sections, }: { modules: IModuleClient[]; sections: SettingsSectionCatalogItem[]; }): SettingsNavigationSection[] => { const sectionMap = buildSectionMap(sections); const entriesBySection = new Map(); for (const module of modules) { if (!module.settings) { continue; } for (const entry of module.settings.entries) { const section = sectionMap.get(entry.sectionId); if (!section) { throw new Error( `[Settings] El módulo "${module.name}" declara la sección inexistente "${entry.sectionId}".` ); } const sectionEntries = entriesBySection.get(entry.sectionId) ?? []; sectionEntries.push({ ...entry, moduleName: module.name, }); entriesBySection.set(entry.sectionId, sectionEntries); } } return sections .map((section) => ({ ...section, entries: entriesBySection.get(section.id) ?? [], })) .filter((section) => section.entries.length > 0); };