/**
 * Pre-execution write detection for COM-backed execute_code Python code.
 *
 * Scans the code string for patterns that indicate write operations —
 * both our wrapped API (Workbook/Worksheet methods) and raw COM property
 * assignments / method calls. If no write pattern matches, the code is
 * read-only and can skip the approval prompt.
 *
 * The analyzer is intentionally conservative: a false positive (flagging a
 * read as a write) just shows an unnecessary approval prompt, while a false
 * negative (missing a write) would skip approval for a destructive operation.
 * This means comments or string literals containing write patterns will
 * trigger approval — that's fine.
 */
/** Exported for the sync test to verify against Python source. */
export const COM_WRAPPED_API_WRITE_OPS = [
    'addPicture',
    'addSheet',
    'autoFill',
    'copyPasteRange',
    'moveSheet',
    'setCell',
    'setCellRange',
    'setIBTextColors'
];
const WRAPPED_API_PATTERNS = COM_WRAPPED_API_WRITE_OPS.map((name) => new RegExp(`\\.${name}\\s*\\(`));
// ---------------------------------------------------------------------------
// Raw COM write patterns (direct property assignments & mutating methods)
// ---------------------------------------------------------------------------
const RAW_COM_WRITES = [
    /\.Value\s*=/,
    /\.Formula\s*=/,
    /\.FormulaR1C1\s*=/,
    /\.NumberFormat\s*=/,
    /\.Name\s*=/,
    /\.ColumnWidth\s*=/,
    /\.RowHeight\s*=/,
    /\.Visible\s*=/,
    /\.Font\.\w+\s*=/,
    /\.Interior\.\w+\s*=/,
    /\.Borders\s*[.(=]/,
    /\.Delete\s*\(/,
    /\.Insert\s*\(/,
    /\.Clear\s*\(/,
    /\.ClearContents\s*\(/,
    /\.ClearFormats\s*\(/,
    /\.ClearComments\s*\(/,
    /\.ClearNotes\s*\(/,
    /\.Copy\s*\(/,
    /\.PasteSpecial\s*\(/,
    /\.AutoFill\s*\(/,
    /\.Merge\s*\(/,
    /\.UnMerge\s*\(/,
    /\.Move\s*\(/,
    /Worksheets\.Add\s*\(/,
    /Charts\.Add\s*\(/,
    /Names\.Add\s*\(/,
    /\.AddComment\s*\(/,
    /\.AddChart2?\s*\(/,
    /\.AddShape\s*\(/,
    /\.AddPicture\s*\(/,
    /\.AddTextbox\s*\(/,
    /\.AddLine\s*\(/,
    /\.AddConnector\s*\(/,
    /\.SetSourceData\s*\(/,
    /\.HasTitle\s*=/,
    /\.ChartTitle\b/,
    /\.SeriesCollection\s*\(/,
    /\.ChartType\s*=/,
    /\.ChartStyle\s*=/,
    /\.Left\s*=/,
    /\.Top\s*=/,
    /\.Width\s*=/,
    /\.Height\s*=/,
    /\.Sort\s*\(/,
    /\.AutoFilter\s*\(/,
    /\.Protect\s*\(/,
    /\.Unprotect\s*\(/,
    /\.Calculation\s*=/,
    /\.EnableEvents\s*=/,
    /\.DisplayAlerts\s*=/,
    /\.ScreenUpdating\s*=/,
    /\.CutCopyMode\s*=/,
    /\bexec\s*\(/,
    /\beval\s*\(/
];
const ALL_WRITE_PATTERNS = [...WRAPPED_API_PATTERNS, ...RAW_COM_WRITES];
const ALWAYS_CONFIRM_OPERATIONS = [
    {
        label: 'Quit Excel',
        reason: 'This closes the Excel application and can discard unsaved workbook state.',
        pattern: /\.Quit\s*\(/
    },
    {
        label: 'Close workbook or window',
        reason: 'This closes an Excel workbook/window and can discard unsaved workbook state.',
        pattern: /\.Close\s*\(/
    }
];
/**
 * Analyze COM-backed execute_code Python for write operations.
 *
 * Returns `{ isReadOnly: true }` when the code contains no detectable
 * writes and can be executed without an approval prompt.
 */
export function analyzeComWriteCode(code) {
    const dangerousOperations = ALWAYS_CONFIRM_OPERATIONS.filter(({ pattern }) => pattern.test(code)).map(({ label, reason }) => ({ label, reason }));
    if (dangerousOperations.length > 0) {
        return {
            isReadOnly: false,
            matchedPattern: dangerousOperations[0]?.label ?? null,
            alwaysRequireApproval: true,
            dangerousOperations
        };
    }
    for (const p of ALL_WRITE_PATTERNS) {
        if (p.test(code)) {
            return {
                isReadOnly: false,
                matchedPattern: p.source,
                alwaysRequireApproval: false,
                dangerousOperations: []
            };
        }
    }
    return {
        isReadOnly: true,
        matchedPattern: null,
        alwaysRequireApproval: false,
        dangerousOperations: []
    };
}
//# sourceMappingURL=write-analyzer.js.map