"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.activate = activate;
exports.deactivate = deactivate;
const vscode = require("vscode");
const net = require("net");
let socket = null;
let lastClipboardContent = '';
let lastClipboardSourceFile = '';
function connect() {
    socket = net.createConnection({ path: '/tmp/cheatnt.sock' });
    socket.on('connect', () => {
        console.log('Connected to CheatNt supervisor');
    });
    socket.on('error', (err) => {
        console.error('Socket error:', err.message);
        socket = null;
    });
    socket.on('close', () => {
        socket = null;
        // retry after 2 seconds if supervisor is not running yet
        setTimeout(connect, 2000);
    });
}
function notify(event) {
    if (socket?.writable) {
        socket.write(JSON.stringify(event) + '\n');
    }
}
function activate(context) {
    connect();
    function startClipboardPolling(context) {
        const interval = setInterval(async () => {
            const current = await vscode.env.clipboard.readText();
            if (current !== lastClipboardContent) {
                lastClipboardContent = current;
                // check if it came from an open document
                const sourceFile = vscode.workspace.textDocuments.find(doc => doc.getText().includes(current));
                lastClipboardSourceFile = sourceFile?.fileName ?? '';
                notify({
                    type: 'COPY',
                    file: lastClipboardSourceFile,
                    inserted: lastClipboardContent,
                    deletedCount: 0,
                    causedByUndoRedo: false,
                    causedByCopyPaste: false,
                    timestamp: Date.now(),
                    offset: -1,
                    isSaved: false
                    // copied: lastClipboardContent,
                    // copiedFrom: lastClipboardSourceFile,
                });
            }
        }, 200);
        context.subscriptions.push({ dispose: () => clearInterval(interval) });
    }
    startClipboardPolling(context);
    context.subscriptions.push(vscode.workspace.onDidChangeTextDocument(event => {
        // if (event.contentChanges.length === 0) return;
        for (const change of event.contentChanges) {
            const isUndoRedo = event.reason === vscode.TextDocumentChangeReason.Undo ||
                event.reason === vscode.TextDocumentChangeReason.Redo;
            const text = change.text;
            const isCopyPaste = (text.length > 0 &&
                lastClipboardContent.length > 0 &&
                lastClipboardContent == text
            // not lastClipboardContent.includes(text)
            );
            const eventType = isCopyPaste ? "PASTE" : "CHANGE";
            notify({
                type: eventType,
                file: event.document.fileName,
                inserted: change.text,
                deletedCount: change.rangeLength,
                causedByUndoRedo: isUndoRedo,
                causedByCopyPaste: isCopyPaste,
                timestamp: Date.now(),
                offset: change.rangeOffset,
                isSaved: !event.document.isDirty
            });
        }
    }));
}
function deactivate() {
    socket?.destroy();
}
