# Cross-Application Shared Module Detection

Reference for detecting and extracting shared modules when `workflow_mode = "project"`.

## Detection Algorithm

Build module inventory per candidate application:

```javascript
// From candidateApplications (step-00) + enriched by cadrage phases 1-4
const modulesByApp = {};
for (const app of candidate_applications) {
  modulesByApp[app.name] = app.modules;  // e.g., { "RH": ["Employes", "Conges", "Temps"], "Projet": ["Projets", "Temps"] }
}
```

Detect modules appearing in 2+ applications:

```javascript
const allModules = Object.values(modulesByApp).flat();
const moduleCounts = {};
for (const mod of allModules) {
  const normalized = normalizeModuleName(mod);  // "Temps", "Saisie du temps", "Time Tracking" → same concept
  moduleCounts[normalized] = (moduleCounts[normalized] || 0) + 1;
}
const sharedModules = Object.entries(moduleCounts)
  .filter(([_, count]) => count >= 2)
  .map(([name, count]) => ({ name, appearsIn: count, applications: findAppsContaining(name) }));
```

## Extraction Logic

If shared modules found, propose extraction:

```javascript
for (const shared of sharedModules) {
  // Create new candidate application
  candidate_applications.push({
    name: shared.name,
    description: `Application transversale pour ${shared.name}`,
    modules: [shared.name],
    isTransversal: true,
    consumedBy: shared.applications
  });

  // Remove the shared module from original applications
  for (const appName of shared.applications) {
    const app = candidate_applications.find(a => a.name === appName);
    app.modules = app.modules.filter(m => normalizeModuleName(m) !== shared.name);
    app.dependencies = app.dependencies || [];
    app.dependencies.push({ target: shared.name, type: "data-dependency" });
  }
}
```

Update `candidate_applications` in the project feature.json.

## Question Template

```
question: "{language == 'fr' ? 'Souhaitez-vous extraire les modules partagés en application(s) dédiée(s) ?' : 'Do you want to extract shared modules as dedicated application(s)?'}"
header: "Extraction"
options:
  - label: "{language == 'fr' ? 'Oui, extraire' : 'Yes, extract'}"
    description: "{language == 'fr' ? 'Créer {sharedModules.length} application(s) transversale(s) supplémentaire(s)' : 'Create {sharedModules.length} additional cross-cutting application(s)'}"
  - label: "{language == 'fr' ? 'Non, garder en l\'état' : 'No, keep as-is'}"
    description: "{language == 'fr' ? 'Chaque application garde sa propre version du module' : 'Each application keeps its own version of the module'}"
```
