"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.DesktopScreenshotTool = void 0;
exports.captureDesktopScreenshot = captureDesktopScreenshot;
exports.execPromise = execPromise;
const child_process_1 = require("child_process");
const fs = __importStar(require("fs/promises"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
// ─── Desktop screenshot tool ────────────────────────────────────────
//
// Captures a screenshot of the current desktop using platform-native
// commands. The result is saved under .harness/desktop/ and the path
// is returned so image_analyze can inspect it.
//
// Capability: desktop-control (gated)
// Risk: medium — read-only screen capture, no input replay
const SCREENSHOT_DIR = '.harness/desktop';
const MAX_SCREENSHOT_AGE_MS = 30 * 60_000;
exports.DesktopScreenshotTool = {
    name: 'desktop_screenshot',
    description: 'Capture a screenshot of the current desktop. Returns the file path so image_analyze can inspect it. Requires a desktop-control capability grant.',
    parameters: {
        type: 'object',
        properties: {
            region: { type: 'string', description: 'Optional: "full" (default) or "active" for the active window only' },
        },
    },
    isReadOnly: true,
    async execute(input) {
        const region = String(input.region ?? 'full').trim().toLowerCase();
        const projectDir = process.cwd();
        const screenshotDir = path.join(projectDir, SCREENSHOT_DIR);
        await fs.mkdir(screenshotDir, { recursive: true });
        const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
        const filename = `screenshot-${timestamp}.png`;
        const outputPath = path.join(screenshotDir, filename);
        try {
            await captureDesktopScreenshot(outputPath, region === 'active');
            // Verify the file was created
            const stat = await fs.stat(outputPath);
            if (stat.size === 0) {
                return { success: false, output: 'Screenshot was captured but the file is empty.', error: 'empty screenshot' };
            }
            // Clean old screenshots
            await cleanOldScreenshots(screenshotDir);
            const relativePath = path.relative(projectDir, outputPath);
            return {
                success: true,
                output: `Screenshot saved to ${relativePath} (${Math.round(stat.size / 1024)}KB). Use image_analyze to inspect it.`,
            };
        }
        catch (error) {
            const msg = error instanceof Error ? error.message : String(error);
            return { success: false, output: `Screenshot capture failed: ${msg}`, error: msg };
        }
    },
};
async function captureDesktopScreenshot(outputPath, activeWindowOnly) {
    const platform = os.platform();
    if (platform === 'win32') {
        // Use PowerShell Add-Type to capture screen
        const script = activeWindowOnly
            ? `Add-Type -AssemblyName System.Windows.Forms; $screen = [System.Windows.Forms.Screen]::PrimaryScreen; $bmp = New-Object System.Drawing.Bitmap($screen.Bounds.Width, $screen.Bounds.Height); $g = [System.Drawing.Graphics]::FromImage($bmp); $g.CopyFromScreen($screen.Bounds.Location, [System.Drawing.Point]::Empty, $screen.Bounds.Size); $bmp.Save('${outputPath.replace(/'/g, "''")}'); $g.Dispose(); $bmp.Dispose()`
            : `Add-Type -AssemblyName System.Windows.Forms; $screen = [System.Windows.Forms.Screen]::PrimaryScreen; $bmp = New-Object System.Drawing.Bitmap($screen.Bounds.Width, $screen.Bounds.Height); $g = [System.Drawing.Graphics]::FromImage($bmp); $g.CopyFromScreen($screen.Bounds.Location, [System.Drawing.Point]::Empty, $screen.Bounds.Size); $bmp.Save('${outputPath.replace(/'/g, "''")}'); $g.Dispose(); $bmp.Dispose()`;
        await execPromise(`powershell -NoProfile -Command "${script}"`, 15_000);
    }
    else if (platform === 'darwin') {
        const cmd = activeWindowOnly
            ? `screencapture -w "${outputPath}"`
            : `screencapture -x "${outputPath}"`;
        await execPromise(cmd, 15_000);
    }
    else {
        // Linux: try gnome-screenshot or import (ImageMagick)
        try {
            await execPromise(`gnome-screenshot -f "${outputPath}"`, 15_000);
        }
        catch {
            await execPromise(`import -window root "${outputPath}"`, 15_000);
        }
    }
}
function execPromise(command, timeoutMs) {
    return new Promise((resolve, reject) => {
        (0, child_process_1.exec)(command, { timeout: timeoutMs }, (error, stdout) => {
            if (error)
                reject(error);
            else
                resolve(stdout);
        });
    });
}
async function cleanOldScreenshots(dir) {
    try {
        const files = await fs.readdir(dir);
        const now = Date.now();
        for (const file of files) {
            if (!file.startsWith('screenshot-') || !file.endsWith('.png'))
                continue;
            const filePath = path.join(dir, file);
            const stat = await fs.stat(filePath);
            if (now - stat.mtimeMs > MAX_SCREENSHOT_AGE_MS) {
                await fs.unlink(filePath).catch(() => { });
            }
        }
    }
    catch { /* cleanup is best-effort */ }
}
//# sourceMappingURL=desktopTools.js.map