"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArchitectureDetector = void 0;
exports.detectArchitectureViaSkill = detectArchitectureViaSkill;
exports.createArchitectureDetector = createArchitectureDetector;
const skillExecutor_1 = require("../../services/skillEngine/skillExecutor");
const skillLoader_1 = require("../../services/skillEngine/skillLoader");
const teaching_types_1 = require("../../types/teaching.types");
// =============================================================================
// Pipeline ID -> ArchitectureInfo.type mapping
// =============================================================================
function mapPipelineToArchType(pipelineId) {
    if (pipelineId.startsWith('FLUTTER_'))
        return 'FLUTTER';
    if (pipelineId.startsWith('WEBVIEW_'))
        return 'WEBVIEW';
    if (pipelineId.startsWith('RN_'))
        return 'REACT_NATIVE'; // Phase E: S14 React Native
    if (pipelineId === 'COMPOSE_STANDARD')
        return 'COMPOSE';
    if (pipelineId === 'GAME_ENGINE')
        return 'GAME_ENGINE';
    if (pipelineId === 'CAMERA_PIPELINE')
        return 'CAMERA';
    if (pipelineId === 'VIDEO_OVERLAY_HWC')
        return 'VIDEO_OVERLAY';
    if (pipelineId === 'SURFACEVIEW_BLAST')
        return 'SURFACEVIEW';
    if (pipelineId.startsWith('OPENGL_') || pipelineId === 'ANGLE_GLES_VULKAN')
        return 'GLSURFACEVIEW';
    if (pipelineId === 'VULKAN_NATIVE')
        return 'GLSURFACEVIEW';
    if (pipelineId === 'ANDROID_VIEW_SOFTWARE')
        return 'SOFTWARE';
    if (pipelineId === 'ANDROID_VIEW_MIXED')
        return 'MIXED';
    if (pipelineId.startsWith('ANDROID_VIEW_'))
        return 'STANDARD';
    if (pipelineId === 'ANDROID_PIP_FREEFORM')
        return 'STANDARD';
    if (pipelineId === 'TEXTUREVIEW_STANDARD')
        return 'STANDARD';
    if (pipelineId === 'SURFACE_CONTROL_API')
        return 'STANDARD';
    if (pipelineId === 'HARDWARE_BUFFER_RENDERER')
        return 'STANDARD';
    if (pipelineId === 'VARIABLE_REFRESH_RATE')
        return 'STANDARD';
    return 'STANDARD';
}
// =============================================================================
// Core detection via YAML skill
// =============================================================================
/**
 * Detect rendering architecture by executing the `rendering_pipeline_detection`
 * YAML skill.  Falls back to STANDARD with 0.5 confidence on any error.
 */
async function detectArchitectureViaSkill(traceProcessorService, traceId, packageName) {
    try {
        const executor = (0, skillExecutor_1.createSkillExecutor)(traceProcessorService);
        await (0, skillLoader_1.ensureSkillRegistryInitialized)();
        executor.registerSkills(skillLoader_1.skillRegistry.getAllSkills());
        executor.setFragmentRegistry(skillLoader_1.skillRegistry.getFragmentCache());
        const result = await executor.execute('rendering_pipeline_detection', traceId, {
            package: packageName || '',
        });
        if (!result.success) {
            console.warn('[detectArchitectureViaSkill] Skill failed:', result.error);
            return createDefaultResult();
        }
        // Extract step results via rawResults (keyed by step id)
        const pipelineRow = extractFirstRow(result.rawResults, 'determine_pipeline');
        const subvariantRow = extractFirstRow(result.rawResults, 'subvariants');
        const pipelineId = pipelineRow?.primary_pipeline_id || 'ANDROID_VIEW_STANDARD_BLAST';
        const confidence = typeof pipelineRow?.primary_confidence === 'number'
            ? pipelineRow.primary_confidence
            : 0.5;
        const type = mapPipelineToArchType(pipelineId);
        const info = {
            type,
            confidence,
            evidence: (0, teaching_types_1.parseCandidates)(pipelineRow?.candidates_list).map(c => ({
                type: 'slice',
                value: c.id,
                weight: c.confidence,
            })),
            additionalInfo: {
                pipelineId,
                featuresList: pipelineRow?.features_list,
                docPath: pipelineRow?.doc_path,
            },
        };
        // Type-specific extras
        if (type === 'FLUTTER') {
            const flutterEngine = subvariantRow?.flutter_engine;
            info.flutter = {
                engine: flutterEngine === 'IMPELLER' ? 'IMPELLER'
                    : flutterEngine === 'SKIA' ? 'SKIA'
                        : 'UNKNOWN',
                surfaceType: pipelineId.includes('SURFACEVIEW') ? 'SURFACEVIEW'
                    : pipelineId.includes('TEXTUREVIEW') ? 'TEXTUREVIEW'
                        : 'UNKNOWN',
            };
        }
        if (type === 'WEBVIEW') {
            const webviewMode = subvariantRow?.webview_mode || '';
            info.webview = {
                engine: webviewMode === 'TEXTUREVIEW_CUSTOM' ? 'X5' : 'CHROMIUM',
                surfaceType: webviewMode === 'SURFACE_CONTROL' ? 'SURFACECONTROL'
                    : webviewMode === 'GL_FUNCTOR' ? 'TEXTUREVIEW'
                        : webviewMode === 'SURFACEVIEW_WRAPPER' ? 'SURFACEVIEW'
                            : 'UNKNOWN',
            };
        }
        if (type === 'COMPOSE') {
            info.compose = {
                hasRecomposition: true,
                hasLazyLists: false,
                isHybridView: false,
                features: [],
            };
        }
        console.log(`[detectArchitectureViaSkill] Detected: ${type} (${pipelineId}) confidence=${confidence.toFixed(2)}`);
        return info;
    }
    catch (err) {
        console.warn('[detectArchitectureViaSkill] Failed, returning default:', err.message);
        return createDefaultResult();
    }
}
// =============================================================================
// Helpers
// =============================================================================
/**
 * Extract the first row from a rawResults step as a key-value object.
 *
 * rawResults[stepId].data is an array of row objects when the skill executor
 * returns columnar data from SQL queries.
 */
function extractFirstRow(rawResults, stepId) {
    if (!rawResults)
        return undefined;
    const step = rawResults[stepId];
    if (!step || !step.data)
        return undefined;
    const data = step.data;
    if (Array.isArray(data) && data.length > 0) {
        return data[0];
    }
    return undefined;
}
function createDefaultResult() {
    return {
        type: 'STANDARD',
        confidence: 0.5,
        evidence: [
            {
                type: 'slice',
                value: 'Default assumption',
                weight: 0.5,
                source: 'No specific architecture detected, assuming standard Android',
            },
        ],
    };
}
// =============================================================================
// Public API (backward-compatible)
// =============================================================================
/**
 * Architecture detector that delegates to the YAML skill.
 *
 * Preserves the `createArchitectureDetector().detect(context)` call pattern
 * used by `claudeRuntime.ts` and `claudeMcpServer.ts`.
 */
class ArchitectureDetector {
    async detect(context) {
        return detectArchitectureViaSkill(context.traceProcessorService, context.traceId, context.packageName);
    }
}
exports.ArchitectureDetector = ArchitectureDetector;
/**
 * Create an architecture detector instance.
 */
function createArchitectureDetector() {
    return new ArchitectureDetector();
}
//# sourceMappingURL=architectureDetector.js.map