// engine/doc-generator/mobile-distribution-guide.ts — SPEC-013 tech-debt criterion 15
// Orchestrator: assembles the mobile distribution guide from platform-specific builders.
// SRP: detection + assembly only. Platform content lives in sibling modules.
import { buildIOSDistributionSection } from './mobile-distribution-ios.js';
import { buildAndroidDistributionSection } from './mobile-distribution-android.js';
// --- Platform detection helpers ---
function detectFromApps(knowledge, platforms) {
    for (const app of knowledge.apps) {
        if (app.mobilePlatform === 'ios') {
            platforms.add('ios');
        }
        if (app.mobilePlatform === 'android') {
            platforms.add('android');
        }
        if (app.mobileFramework === 'flutter') {
            platforms.add('flutter');
        }
        if (app.mobileFramework === 'react-native') {
            platforms.add('react-native');
        }
    }
}
function detectFromFramework(knowledge, platforms) {
    const fw = (knowledge.framework ?? '').toLowerCase();
    if (fw.includes('flutter')) {
        platforms.add('flutter');
    }
    if (fw.includes('react-native') || fw.includes('reactnative')) {
        platforms.add('react-native');
    }
    if (fw.includes('expo')) {
        platforms.add('expo');
        platforms.add('react-native');
    }
}
function detectFromStack(knowledge, platforms) {
    for (const tech of knowledge.stack) {
        const lower = tech.toLowerCase();
        if (lower === 'flutter') {
            platforms.add('flutter');
        }
        if (lower === 'react-native') {
            platforms.add('react-native');
        }
        if (lower === 'expo') {
            platforms.add('expo');
        }
    }
}
function detectFromSpecs(specs, platforms) {
    for (const spec of specs) {
        if (spec.target === 'ios') {
            platforms.add('ios');
        }
        if (spec.target === 'android') {
            platforms.add('android');
        }
        const tags = spec.tags.join(' ').toLowerCase();
        if (tags.includes('flutter')) {
            platforms.add('flutter');
        }
        if (tags.includes('react-native') || tags.includes('expo')) {
            platforms.add('react-native');
        }
    }
}
function detectMobilePlatforms(knowledge, specs) {
    const platforms = new Set();
    detectFromApps(knowledge, platforms);
    detectFromFramework(knowledge, platforms);
    detectFromStack(knowledge, platforms);
    detectFromSpecs(specs, platforms);
    return Array.from(platforms);
}
// --- Cross-platform section builders (Flutter / React Native / Expo / Bare RN) ---
function buildFlutterDistributionSection() {
    return {
        title: 'Flutter — Multi-Platform Build & Submit',
        level: 2,
        content: [
            '### iOS Build',
            '```bash',
            'flutter build ipa --release',
            '# Output: build/ios/ipa/MyApp.ipa',
            '# Then follow iOS → TestFlight steps below',
            '```',
            '',
            '### Android Build',
            '```bash',
            'flutter build appbundle --release',
            '# Output: build/app/outputs/bundle/release/app-release.aab',
            '# Then follow Android → Google Play steps below',
            'flutter build apk --release  # for direct APK distribution',
            '```',
            '',
            '### CI/CD with Fastlane + Flutter',
            '```bash',
            'lane :deploy_ios do',
            '  sh "flutter build ipa --release"',
            '  pilot(ipa: "build/ios/ipa/MyApp.ipa")',
            'end',
            'lane :deploy_android do',
            '  sh "flutter build appbundle --release"',
            '  supply(aab: "build/app/outputs/bundle/release/app-release.aab", track: "internal")',
            'end',
            '```',
            '',
            '> **Verification:** `flutter build` exits 0. No deprecated APIs in `flutter analyze`.',
        ].join('\n'),
    };
}
function buildExpoDistributionSection() {
    return {
        title: 'React Native (Expo EAS) — Build & Submit',
        level: 2,
        content: [
            '### Setup EAS CLI',
            '```bash',
            'npm install -g eas-cli',
            'eas login',
            'eas build:configure  # creates eas.json',
            '```',
            '',
            '### Build with EAS Build',
            '```bash',
            'eas build --platform ios --profile production',
            'eas build --platform android --profile production',
            'eas build --platform all --profile production',
            '```',
            '',
            '### Submit with EAS Submit',
            '```bash',
            'eas submit --platform ios    # → TestFlight',
            'eas submit --platform android  # → Google Play internal track',
            'eas submit --platform all',
            '```',
            '',
            '### OTA Updates with EAS Update',
            '```bash',
            'eas update --branch production --message "Hotfix: fix login flow"',
            '```',
            '',
            '### eas.json example',
            '```json',
            '{',
            '  "build": {',
            '    "production": {',
            '      "ios": { "buildType": "appstore" },',
            '      "android": { "buildType": "app-bundle" }',
            '    }',
            '  }',
            '}',
            '```',
            '',
            '> **Verification:** EAS Build shows "Finished". EAS Submit shows "Submitted".',
        ].join('\n'),
    };
}
function buildBareReactNativeDistributionSection() {
    return {
        title: 'React Native (Bare Workflow) — Build & Submit',
        level: 2,
        content: [
            '### iOS Build',
            '```bash',
            'cd ios && pod install && cd ..',
            'xcodebuild -workspace ios/MyApp.xcworkspace -scheme MyApp \\',
            '  -configuration Release -archivePath build/MyApp.xcarchive archive',
            '# Or via Fastlane:',
            'fastlane gym --workspace ios/MyApp.xcworkspace --scheme MyApp',
            '```',
            '',
            '### Android Build',
            '```bash',
            'cd android && ./gradlew bundleRelease',
            '# Output: android/app/build/outputs/bundle/release/app-release.aab',
            '```',
            '',
            '### Submit',
            '- iOS: `fastlane pilot upload` or Transporter.app',
            '- Android: `fastlane supply` or Play Console manually',
            '',
            '### Metro bundler release check',
            '```bash',
            'react-native bundle --platform ios --dev false \\',
            '  --entry-file index.js --bundle-output ios/main.jsbundle',
            '```',
            '',
            '> **Verification:** Release build launches without Metro bundler. No yellow-box warnings.',
        ].join('\n'),
    };
}
function buildReactNativeDistributionSection(isExpo) {
    if (isExpo) {
        return buildExpoDistributionSection();
    }
    return buildBareReactNativeDistributionSection();
}
function buildPhasedRolloutSection() {
    return {
        title: 'Phased Rollout Strategy',
        level: 2,
        content: [
            '| Day | iOS (Phased Release) | Android (Play Console) |',
            '|-----|---------------------|------------------------|',
            '| 1   | 1% of users         | 1% rollout             |',
            '| 2   | 2%                  | 5%                     |',
            '| 3   | 5%                  | 10%                    |',
            '| 4   | 10%                 | 20%                    |',
            '| 5   | 20%                 | 50%                    |',
            '| 6   | 50%                 | 100%                   |',
            '| 7   | 100%                | (monitor)              |',
            '',
            '**Halt criteria:** Crash-free rate drops below 99.5% OR critical issues exceed 0.1%.',
            '',
            '**Rollback procedure:**',
            '- iOS: halt phased release in App Store Connect',
            '- Android: halt rollout in Play Console, push fix via expedited review',
        ].join('\n'),
    };
}
function addStoreSectionsIfMissing(platforms, sections) {
    const isCrossPlatform = platforms.includes('flutter') ||
        platforms.includes('react-native') ||
        platforms.includes('expo');
    if (isCrossPlatform) {
        if (!sections.some((s) => s.title.includes('TestFlight'))) {
            sections.push(buildIOSDistributionSection());
        }
        if (!sections.some((s) => s.title.includes('Google Play'))) {
            sections.push(buildAndroidDistributionSection());
        }
    }
}
// --- Main generator ---
/**
 * Generates a developer-facing mobile distribution guide.
 * Covers iOS TestFlight, Google Play Internal Testing, EAS Build/Submit, and Flutter commands.
 */
export function generateMobileDistributionGuide(knowledge, specs, _language) {
    const platforms = detectMobilePlatforms(knowledge, specs);
    const sections = [];
    const isExpo = platforms.includes('expo') || knowledge.stack.some((t) => t.toLowerCase() === 'expo');
    sections.push({
        title: 'Mobile Distribution Guide',
        level: 1,
        content: [
            'This guide covers the full distribution pipeline for mobile applications:',
            'build signing, beta testing, store submission, and phased rollout.',
            '',
            `**Detected platforms:** ${platforms.length > 0 ? platforms.join(', ') : 'none detected — configure manually'}`,
        ].join('\n'),
    });
    if (platforms.includes('flutter')) {
        sections.push(buildFlutterDistributionSection());
    }
    if (platforms.includes('react-native') || platforms.includes('expo')) {
        sections.push(buildReactNativeDistributionSection(isExpo));
    }
    if (platforms.includes('ios') &&
        !platforms.includes('flutter') &&
        !platforms.includes('react-native')) {
        sections.push(buildIOSDistributionSection());
    }
    if (platforms.includes('android') &&
        !platforms.includes('flutter') &&
        !platforms.includes('react-native')) {
        sections.push(buildAndroidDistributionSection());
    }
    addStoreSectionsIfMissing(platforms, sections);
    sections.push(buildPhasedRolloutSection());
    const content = sections
        .map((s) => `${'#'.repeat(s.level)} ${s.title}\n\n${s.content}`)
        .join('\n\n');
    return {
        type: 'mobile-distribution-guide',
        title: 'Mobile Distribution Guide',
        path: 'docs/mobile-distribution.md',
        content,
        audience: 'developer',
        sections,
        diagrams: [],
        relatedSpecs: specs
            .filter((s) => s.target === 'ios' || s.target === 'android')
            .map((s) => s.id),
    };
}
//# sourceMappingURL=mobile-distribution-guide.js.map