"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.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.detectAdbContext = detectAdbContext;
const os_1 = __importDefault(require("os"));
const adbService_1 = require("./adbService");
const traceDeviceMatcher_1 = require("./traceDeviceMatcher");
function normalizeMode(mode) {
    const m = (mode || '').trim().toLowerCase();
    if (m === 'off' || m === 'false' || m === '0' || m === 'disabled')
        return 'off';
    if (m === 'auto')
        return 'auto';
    if (m === 'read_only' || m === 'readonly' || m === 'read-only')
        return 'read_only';
    if (m === 'full' || m === 'write' || m === 'rw')
        return 'full';
    return 'auto';
}
function resolveConfig(input) {
    const envMode = process.env.SMARTPERFETTO_ADB_MODE;
    const envSerial = process.env.SMARTPERFETTO_ADB_SERIAL;
    const envRequireMatch = process.env.SMARTPERFETTO_ADB_REQUIRE_TRACE_MATCH;
    const mode = normalizeMode(input?.mode || envMode);
    const serial = (input?.serial || envSerial || '').trim() || undefined;
    const requireTraceMatch = typeof input?.requireTraceMatch === 'boolean'
        ? input.requireTraceMatch
        : (envRequireMatch ? !['0', 'false', 'no'].includes(envRequireMatch.trim().toLowerCase()) : true);
    return { mode, serial, requireTraceMatch };
}
async function detectAdbContext(config, traceProcessorService, traceId) {
    const resolved = resolveConfig(config);
    const adb = (0, adbService_1.getAdbService)();
    const warnings = [];
    const problems = [];
    const version = await adb.getVersion();
    const installed = !!version;
    if (!installed) {
        return {
            mode: resolved.mode,
            enabled: false,
            availability: { installed: false, devices: [], problems: ['adb 未安装或不可用'] },
            warnings: [],
        };
    }
    let devices = [];
    try {
        devices = await adb.listDevices();
    }
    catch (e) {
        problems.push(`adb devices 失败: ${e?.message || 'unknown error'}`);
        devices = [];
    }
    const deviceCandidates = devices.filter((d) => d.state === 'device');
    let selectedSerial = undefined;
    if (resolved.serial) {
        const exists = devices.some((d) => d.serial === resolved.serial);
        if (exists) {
            selectedSerial = resolved.serial;
        }
        else {
            warnings.push(`指定的 adb serial 未找到: ${resolved.serial}`);
        }
    }
    else if (deviceCandidates.length === 1) {
        selectedSerial = deviceCandidates[0].serial;
    }
    else if (deviceCandidates.length > 1) {
        warnings.push(`检测到多个设备 (${deviceCandidates.length})，需要指定 adb serial 才能启用协同`);
    }
    else {
        // No usable device.
    }
    const availability = {
        installed,
        version: version || undefined,
        devices,
        selectedSerial,
        problems: problems.length > 0 ? problems : undefined,
    };
    // Mode=off: we still report availability but don't fetch more.
    if (resolved.mode === 'off') {
        return {
            mode: resolved.mode,
            enabled: false,
            availability,
            warnings,
        };
    }
    let deviceInfo = undefined;
    let traceMatch = undefined;
    if (selectedSerial) {
        try {
            deviceInfo = await adb.getDeviceInfo(selectedSerial);
        }
        catch (e) {
            warnings.push(`获取设备信息失败: ${e?.message || 'unknown error'}`);
        }
    }
    // Compute trace match (best-effort).
    if (deviceInfo) {
        try {
            const traceProfile = await (0, traceDeviceMatcher_1.getTraceDeviceProfile)(traceProcessorService, traceId);
            traceMatch = (0, traceDeviceMatcher_1.matchTraceToDevice)(traceProfile, deviceInfo);
        }
        catch (e) {
            warnings.push(`trace/device 匹配失败: ${e?.message || 'unknown error'}`);
        }
    }
    // Decide whether ADB collaboration is enabled.
    let enabled = false;
    if (selectedSerial) {
        if (resolved.mode === 'full' || resolved.mode === 'read_only') {
            enabled = true;
        }
        else if (resolved.mode === 'auto') {
            if (!resolved.requireTraceMatch) {
                enabled = true;
            }
            else if (traceMatch?.status === 'match' && traceMatch.confidence >= 0.7) {
                enabled = true;
            }
            else if (traceMatch?.status === 'mismatch') {
                enabled = false;
                warnings.push('自动模式下检测到 trace/device 不匹配，已禁用 adb 协同');
            }
            else {
                enabled = false;
                warnings.push('自动模式下无法确认 trace/device 匹配，已禁用 adb 协同（可手动开启 read_only/full）');
            }
        }
    }
    // Provide a hint about host OS (useful for debugging ADB availability).
    if (process.env.NODE_ENV !== 'production') {
        warnings.push(`host=${os_1.default.platform()} adb=${adb.getAdbPath()}`);
    }
    return {
        mode: resolved.mode,
        enabled,
        availability,
        deviceInfo,
        traceMatch,
        warnings,
    };
}
//# sourceMappingURL=adbContextDetector.js.map