// Planu — Multi-Tenancy Detector (SPEC-027)
// Detects whether a project uses multi-tenant patterns by scanning
// data models, middleware, and config for tenant isolation indicators.
// Patterns that signal tenant ID fields in models/schemas
const TENANT_FIELD_PATTERNS = [
    /tenant[_-]id/i,
    /organization[_-]id/i,
    /org[_-]id/i,
    /workspace[_-]id/i,
    /account[_-]id/i,
    /company[_-]id/i,
    /client[_-]id/i,
    /team[_-]id/i,
];
// Patterns that signal tenant extraction middleware
const MIDDLEWARE_PATTERNS = [
    /extractTenant/i,
    /tenantMiddleware/i,
    /TenantResolver/i,
    /getCurrentTenant/i,
    /resolveTenant/i,
    /withTenant/i,
    /TenantContext/i,
    /tenant_from_/i,
];
// Patterns that indicate Row Level Security
const RLS_PATTERNS = [
    /row.level.security/i,
    /enable rls/i,
    /create policy/i,
    /using \(.*tenant/i,
    /using \(.*organization/i,
    /RLSPolicy/i,
    /row_security/i,
];
// Patterns that indicate separate schemas per tenant
const SCHEMA_PATTERNS = [
    /schema.*tenant/i,
    /tenant.*schema/i,
    /set search_path.*tenant/i,
    /SchemaPerTenant/i,
    /createSchema.*tenant/i,
];
/**
 * Detect multi-tenancy patterns from source file content.
 * Analyzes file contents for tenant isolation indicators.
 */
export function detectMultiTenancy(fileContents) {
    const combined = fileContents.join('\n');
    const tenantIdFields = extractTenantIdFields(combined);
    const hasTenantMiddleware = MIDDLEWARE_PATTERNS.some((p) => p.test(combined));
    const hasRLS = RLS_PATTERNS.some((p) => p.test(combined));
    const hasSeparateSchemas = SCHEMA_PATTERNS.some((p) => p.test(combined));
    const isMultiTenant = tenantIdFields.length > 0 || hasTenantMiddleware || hasRLS || hasSeparateSchemas;
    const strategy = determineStrategy(hasRLS, hasSeparateSchemas, tenantIdFields.length > 0);
    const confidence = computeConfidence(tenantIdFields.length, hasTenantMiddleware, hasRLS, hasSeparateSchemas);
    return {
        isMultiTenant,
        strategy,
        tenantIdFields,
        hasRLS,
        hasTenantMiddleware,
        hasSeparateSchemas,
        confidence,
    };
}
/**
 * Detect multi-tenancy from file path + content pairs.
 * Useful when you need to know which specific files contain tenant patterns.
 */
export function detectMultiTenancyFromFiles(files) {
    const indicatorFiles = [];
    const contents = [];
    for (const file of files) {
        const hasTenantSignal = TENANT_FIELD_PATTERNS.some((p) => p.test(file.content)) ||
            MIDDLEWARE_PATTERNS.some((p) => p.test(file.content)) ||
            RLS_PATTERNS.some((p) => p.test(file.content)) ||
            SCHEMA_PATTERNS.some((p) => p.test(file.content));
        if (hasTenantSignal) {
            indicatorFiles.push(file.path);
        }
        contents.push(file.content);
    }
    const base = detectMultiTenancy(contents);
    return { ...base, indicatorFiles };
}
// ── Internal helpers ─────────────────────────────────────────────────────────
function extractTenantIdFields(content) {
    const found = new Set();
    for (const pattern of TENANT_FIELD_PATTERNS) {
        const matches = content.match(new RegExp(pattern.source, 'gi'));
        if (matches) {
            for (const m of matches) {
                found.add(m.toLowerCase().replace(/[^a-z_]/g, '_'));
            }
        }
    }
    return Array.from(found);
}
function determineStrategy(hasRLS, hasSeparateSchemas, hasTenantId) {
    if (hasRLS) {
        return 'row-level-security';
    }
    if (hasSeparateSchemas) {
        return 'separate-schema';
    }
    if (hasTenantId) {
        return 'shared-schema';
    }
    return null;
}
function computeConfidence(tenantFieldCount, hasMiddleware, hasRLS, hasSeparateSchemas) {
    let score = 0;
    score += tenantFieldCount > 0 ? 1 : 0;
    score += hasMiddleware ? 2 : 0;
    score += hasRLS ? 2 : 0;
    score += hasSeparateSchemas ? 2 : 0;
    if (score >= 4) {
        return 'high';
    }
    if (score >= 2) {
        return 'medium';
    }
    return 'low';
}
//# sourceMappingURL=multi-tenancy-detector.js.map