"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TerminalMonitor = void 0;
const terminalService_1 = require("../../../platform/terminal/common/terminalService");
const lifecycle_1 = require("../../../util/vs/base/common/lifecycle");
const MAX_BUFFER_CHARS = 2000;
/**
 * Monitors terminal activity and tracks the last terminal command execution.
 * Similar to UserInteractionMonitor, this class listens to terminal events
 * to understand user terminal activity patterns.
 */
let TerminalMonitor = class TerminalMonitor extends lifecycle_1.Disposable {
    constructor(_terminalService) {
        super();
        this._terminalService = _terminalService;
        // Listen to terminal shell execution end events to track command completions
        this._register(this._terminalService.onDidEndTerminalShellExecution(e => {
            this._recordTerminalActivity(e);
        }));
        // Clear last activity if the terminal is closed
        this._register(this._terminalService.onDidCloseTerminal(terminal => {
            if (this._lastActivity?.terminal === terminal) {
                this._lastActivity = undefined;
            }
        }));
    }
    _recordTerminalActivity(event) {
        const executedCommand = event.execution;
        this._lastActivity = {
            terminal: event.terminal,
            terminalName: event.terminal.name,
            commandLine: executedCommand.commandLine?.value,
            cwd: formatCwd(executedCommand.cwd),
            exitCode: event.exitCode,
            timestamp: Date.now(),
        };
    }
    /**
     * Gets the terminal output data for telemetry, including how long ago the last command was executed.
     * Data is primarily sourced from onDidEndTerminalShellExecution to ensure consistency
     * (buffer, command, and metadata all come from the same terminal).
     */
    getData() {
        const now = Date.now();
        const terminalCount = this._terminalService.terminals.length;
        if (!this._lastActivity) {
            return JSON.stringify({
                terminalCount,
            });
        }
        // Get buffer from the same terminal where the last command was executed
        const buffer = this._terminalService.getBufferForTerminal(this._lastActivity.terminal, MAX_BUFFER_CHARS * 2);
        const msAgo = now - this._lastActivity.timestamp;
        const data = {
            // All data from the same terminal that executed the last tracked command
            terminalName: this._lastActivity.terminalName,
            commandLine: this._lastActivity.commandLine,
            cwd: this._lastActivity.cwd,
            exitCode: this._lastActivity.exitCode,
            msAgo,
            // Buffer from the same terminal
            buffer: buffer.length <= MAX_BUFFER_CHARS ? {
                fits: true,
                content: buffer,
                length: buffer.length,
            } : {
                fits: false,
                contentStart: buffer.slice(0, MAX_BUFFER_CHARS / 2),
                contentEnd: buffer.slice(-MAX_BUFFER_CHARS / 2),
                length: buffer.length,
                truncatedChars: buffer.length - MAX_BUFFER_CHARS,
            },
            // General terminal state
            terminalCount,
        };
        return JSON.stringify(data);
    }
};
exports.TerminalMonitor = TerminalMonitor;
exports.TerminalMonitor = TerminalMonitor = __decorate([
    __param(0, terminalService_1.ITerminalService)
], TerminalMonitor);
function formatCwd(cwd) {
    if (cwd === undefined) {
        return undefined;
    }
    if (typeof cwd === 'string') {
        return cwd;
    }
    return cwd.fsPath;
}
//# sourceMappingURL=terminalOutput.js.map