"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.LEGACY_TO_PLAIN_PHRASE_RULES = exports.AMPLIFICATION_UNKNOWN_CURRENT_FRAME_TEXT = exports.AMPLIFICATION_UNKNOWN_TEXT = exports.SUPPLY_WORKLOAD_WEAK_TEXT = exports.SUPPLY_NONE_CURRENT_FRAME_TEXT = exports.SUPPLY_NONE_TEXT = exports.OPTIMIZATION_LABEL = exports.DEEP_REASON_ALIASES = exports.DEEP_REASON_LABEL = exports.TRIAD_HEADING = exports.TRIAD_ROLE_ALIASES = exports.TRIAD_EVIDENCE_LABELS = exports.TRIAD_LABELS = void 0;
exports.hasTriadRoleText = hasTriadRoleText;
exports.stripTriadPrefix = stripTriadPrefix;
exports.normalizeLegacyTriadTerms = normalizeLegacyTriadTerms;
exports.parseTriadParts = parseTriadParts;
exports.buildTriadStatement = buildTriadStatement;
exports.TRIAD_LABELS = {
    trigger: '直接原因',
    supply: '资源问题',
    amplification: '放大因素',
};
exports.TRIAD_EVIDENCE_LABELS = {
    trigger: '直接原因证据',
    supply: '资源问题证据',
    amplification: '放大因素证据',
};
exports.TRIAD_ROLE_ALIASES = {
    trigger: ['触发因子', '直接原因'],
    supply: ['供给约束', '资源瓶颈', '资源问题'],
    amplification: ['放大路径', '放大环节', '放大因素'],
};
exports.TRIAD_HEADING = `根因机制拆解（${exports.TRIAD_LABELS.trigger}/${exports.TRIAD_LABELS.supply}/${exports.TRIAD_LABELS.amplification}）`;
exports.DEEP_REASON_LABEL = '为什么慢';
exports.DEEP_REASON_ALIASES = ['为什么慢', '慢因子拆解'];
exports.OPTIMIZATION_LABEL = '优化方向';
exports.SUPPLY_NONE_TEXT = `${exports.TRIAD_LABELS.supply}不明显`;
exports.SUPPLY_NONE_CURRENT_FRAME_TEXT = `${exports.TRIAD_LABELS.supply}不明显（当前帧）`;
exports.SUPPLY_WORKLOAD_WEAK_TEXT = `负载主导（${exports.TRIAD_LABELS.supply}弱）`;
exports.AMPLIFICATION_UNKNOWN_TEXT = '未识别放大因素';
exports.AMPLIFICATION_UNKNOWN_CURRENT_FRAME_TEXT = '未观察到明确放大因素证据（当前帧）';
function escapeRegExp(value) {
    return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
function aliasGroup(role) {
    return exports.TRIAD_ROLE_ALIASES[role].map(escapeRegExp).join('|');
}
function buildTriadLinePattern(role) {
    return new RegExp(`(?:${aliasGroup(role)})(?:（[^）]*）)?\\s*[:：]\\s*([^；;\\n]+)`);
}
function hasTriadRoleText(text, role) {
    return new RegExp(`(?:${aliasGroup(role)})`).test(String(text || ''));
}
function stripTriadPrefix(text) {
    let out = String(text || '').trim();
    for (const role of ['trigger', 'supply', 'amplification']) {
        const pattern = new RegExp(`^(?:${aliasGroup(role)})(?:（[^）]*）)?\\s*[:：]\\s*`);
        out = out.replace(pattern, '');
    }
    return out.trim();
}
function normalizeLegacyTriadTerms(text) {
    return String(text || '')
        .replace(/触发因子（直接原因）/g, exports.TRIAD_LABELS.trigger)
        .replace(/供给约束（资源瓶颈）/g, exports.TRIAD_LABELS.supply)
        .replace(/放大路径（问题放大环节）/g, exports.TRIAD_LABELS.amplification);
}
function parseTriadParts(text) {
    const source = String(text || '');
    const trigger = source.match(buildTriadLinePattern('trigger'))?.[1]?.trim();
    const supply = source.match(buildTriadLinePattern('supply'))?.[1]?.trim();
    const amplification = source.match(buildTriadLinePattern('amplification'))?.[1]?.trim();
    return {
        ...(trigger ? { trigger } : {}),
        ...(supply ? { supply } : {}),
        ...(amplification ? { amplification } : {}),
    };
}
function buildTriadStatement(params) {
    const parts = [];
    if (params.trigger)
        parts.push(`${exports.TRIAD_LABELS.trigger}: ${params.trigger}`);
    if (params.supply)
        parts.push(`${exports.TRIAD_LABELS.supply}: ${params.supply}`);
    if (params.amplification)
        parts.push(`${exports.TRIAD_LABELS.amplification}: ${params.amplification}`);
    return parts.join('；');
}
exports.LEGACY_TO_PLAIN_PHRASE_RULES = [
    {
        pattern: /触发因子（直接原因）|触发因子/g,
        replacement: exports.TRIAD_LABELS.trigger,
    },
    {
        pattern: /供给约束（资源瓶颈）|供给约束/g,
        replacement: exports.TRIAD_LABELS.supply,
    },
    {
        pattern: /放大路径（问题放大环节）|放大路径/g,
        replacement: exports.TRIAD_LABELS.amplification,
    },
    {
        pattern: /负载主导（供给约束弱）/g,
        replacement: `任务量偏大（${exports.SUPPLY_NONE_TEXT}）`,
    },
];
//# sourceMappingURL=analysisNarrative.js.map