/**
 * Market Category Detector - Rule-Based Classification
 *
 * Classifies product vision into market categories using intelligent
 * rule-based matching against domain patterns. No external LLM calls -
 * all classification logic is encoded at spec-time.
 *
 * **Approach**:
 * 1. Match vision against domain patterns
 * 2. Score each market category based on pattern matches
 * 3. Calculate confidence score
 * 4. Return best-fit category with confidence
 *
 * **Benefits**:
 * - Zero external dependencies
 * - Instant classification (no API latency)
 * - 100% deterministic and testable
 * - Works offline
 */
import { DOMAIN_PATTERNS } from './keyword-extractor.js';
/**
 * Market category mappings with intelligent rules
 *
 * This encodes business intelligence about what domains correlate
 * with which market categories.
 */
const MARKET_MAPPINGS = [
    {
        category: 'productivity-saas',
        primaryDomains: ['productivity', 'saas', 'collaboration'],
        secondaryDomains: ['enterprise', 'design'],
        minConfidence: 0.6
    },
    {
        category: 'healthcare',
        primaryDomains: ['healthcare'],
        secondaryDomains: ['ai-ml', 'saas', 'enterprise'],
        minConfidence: 0.7
    },
    {
        category: 'fintech',
        primaryDomains: ['fintech'],
        secondaryDomains: ['ecommerce', 'enterprise', 'blockchain'],
        minConfidence: 0.7
    },
    {
        category: 'e-commerce',
        primaryDomains: ['ecommerce'],
        secondaryDomains: ['fintech', 'ai-ml', 'social'],
        minConfidence: 0.6
    },
    {
        category: 'education',
        primaryDomains: ['education'],
        secondaryDomains: ['ai-ml', 'saas', 'collaboration'],
        minConfidence: 0.6
    },
    {
        category: 'gaming',
        primaryDomains: ['gaming'],
        secondaryDomains: ['social', 'ai-ml'],
        minConfidence: 0.7
    },
    {
        category: 'social-network',
        primaryDomains: ['social'],
        secondaryDomains: ['collaboration', 'ecommerce', 'gaming'],
        minConfidence: 0.6
    },
    {
        category: 'enterprise-b2b',
        primaryDomains: ['enterprise'],
        secondaryDomains: ['productivity', 'saas', 'collaboration'],
        minConfidence: 0.6
    },
    {
        category: 'consumer-b2c',
        primaryDomains: ['social', 'ecommerce'],
        secondaryDomains: ['gaming', 'education'],
        minConfidence: 0.5
    },
    {
        category: 'marketplace',
        primaryDomains: ['ecommerce'],
        secondaryDomains: ['social', 'fintech'],
        minConfidence: 0.6
    },
    {
        category: 'iot',
        primaryDomains: ['iot'],
        secondaryDomains: ['ai-ml', 'healthcare'],
        minConfidence: 0.7
    },
    {
        category: 'blockchain',
        primaryDomains: ['blockchain'],
        secondaryDomains: ['fintech', 'gaming'],
        minConfidence: 0.7
    },
    {
        category: 'ai-ml',
        primaryDomains: ['ai-ml'],
        secondaryDomains: ['healthcare', 'fintech', 'ecommerce'],
        minConfidence: 0.7
    }
];
/**
 * Detect which domains are present in the vision text
 */
function detectPresentDomains(vision) {
    const visionLower = vision.toLowerCase();
    const presentDomains = new Set();
    for (const domainPattern of DOMAIN_PATTERNS) {
        const regex = new RegExp(domainPattern.pattern);
        if (regex.test(visionLower)) {
            presentDomains.add(domainPattern.domain);
        }
    }
    return presentDomains;
}
/**
 * Score market categories based on present domains
 */
function scoreMarketCategories(presentDomains) {
    const results = [];
    for (const mapping of MARKET_MAPPINGS) {
        let score = 0;
        const matchedDomains = [];
        for (const primaryDomain of mapping.primaryDomains) {
            if (presentDomains.has(primaryDomain)) {
                score += 3.0;
                matchedDomains.push(primaryDomain);
            }
        }
        for (const secondaryDomain of mapping.secondaryDomains) {
            if (presentDomains.has(secondaryDomain)) {
                score += 1.0;
                matchedDomains.push(secondaryDomain);
            }
        }
        const maxPossibleScore = mapping.primaryDomains.length * 3.0 +
            mapping.secondaryDomains.length * 1.0;
        const confidence = score / maxPossibleScore;
        results.push({
            category: mapping.category,
            score,
            confidence,
            matchedDomains
        });
    }
    return results.sort((a, b) => b.score - a.score);
}
/**
 * Detect market category from vision text
 *
 * Uses rule-based classification against domain patterns to determine
 * the best-fit market category with confidence score.
 *
 * @param vision - Product vision text
 * @returns Market classification with category and confidence
 *
 * @example
 * ```typescript
 * const result = detectMarketCategory('A project management tool for agile teams');
 * // Returns: { category: 'productivity-saas', confidence: 0.85 }
 * ```
 */
export function detectMarketCategory(vision) {
    if (!vision || vision.trim().length === 0) {
        return { category: 'unknown', confidence: 0 };
    }
    const presentDomains = detectPresentDomains(vision);
    if (presentDomains.size === 0) {
        return { category: 'unknown', confidence: 0 };
    }
    const categoryScores = scoreMarketCategories(presentDomains);
    const qualifyingScores = categoryScores.filter((cs, idx) => {
        const mapping = MARKET_MAPPINGS.find(m => m.category === cs.category);
        return mapping && cs.confidence >= mapping.minConfidence;
    });
    if (qualifyingScores.length === 0) {
        return { category: 'unknown', confidence: 0 };
    }
    const best = qualifyingScores[0];
    const result = {
        category: best.category,
        confidence: best.confidence
    };
    if (qualifyingScores.length > 1) {
        const runnerUp = qualifyingScores[1];
        if (runnerUp.confidence >= best.confidence * 0.8) {
            result.runnerUp = {
                category: runnerUp.category,
                confidence: runnerUp.confidence
            };
        }
    }
    return result;
}
/**
 * Get all potential market categories with scores (useful for debugging)
 *
 * @param vision - Product vision text
 * @returns Array of all categories with scores, sorted by confidence
 */
export function analyzeMarketCategories(vision) {
    if (!vision || vision.trim().length === 0) {
        return [];
    }
    const presentDomains = detectPresentDomains(vision);
    const scored = scoreMarketCategories(presentDomains);
    return scored.map(({ category, confidence, matchedDomains }) => ({
        category,
        confidence,
        matchedDomains
    }));
}
//# sourceMappingURL=MarketDetector.js.map