"use strict";
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DevToolsManager = void 0;
const tsyringe_1 = require("tsyringe");
/**
 * DevToolsManager
 *
 * Manages DevTools for WebContentsViews, including:
 * - Keyboard shortcut registration (Ctrl+Shift+D)
 * - Automatic cleanup when views are destroyed
 */
let DevToolsManager = class DevToolsManager {
    viewsWithDevTools = new Set();
    /**
     * Register DevTools shortcut (Ctrl+Shift+D) on a WebContentsView
     * Works on both X11 and Wayland
     */
    registerShortcut(view) {
        view.webContents.on("before-input-event", (event, input) => {
            // Check for Ctrl+Shift+D (or Cmd+Shift+D on macOS)
            if (input.type === "keyDown" &&
                input.key.toLowerCase() === "d" &&
                input.shift &&
                (input.control || input.meta) &&
                !input.alt) {
                event.preventDefault();
                try {
                    const webContentsId = view.webContents.id;
                    if (view.webContents.isDevToolsOpened()) {
                        view.webContents.closeDevTools();
                        this.viewsWithDevTools.delete(webContentsId);
                        console.log("Closed DevTools for view");
                    }
                    else {
                        view.webContents.openDevTools();
                        this.viewsWithDevTools.add(webContentsId);
                        console.log("Opened DevTools for view");
                    }
                }
                catch (err) {
                    console.error("Failed to toggle DevTools:", err);
                }
            }
        });
    }
    /**
     * Close DevTools for a view when it's being destroyed
     */
    closeDevToolsForView(view) {
        try {
            const webContentsId = view.webContents.id;
            if (!view.webContents.isDestroyed() &&
                view.webContents.isDevToolsOpened()) {
                view.webContents.closeDevTools();
                console.log(`Closed DevTools for view (webContents ID: ${webContentsId})`);
            }
            this.viewsWithDevTools.delete(webContentsId);
        }
        catch (err) {
            // Ignore errors during cleanup
            console.debug("Error closing DevTools during cleanup:", err);
        }
    }
    /**
     * Get the set of webContents IDs that have DevTools open
     */
    getViewsWithDevTools() {
        return new Set(this.viewsWithDevTools);
    }
    /**
     * Clear all tracked DevTools
     */
    clear() {
        this.viewsWithDevTools.clear();
    }
};
exports.DevToolsManager = DevToolsManager;
exports.DevToolsManager = DevToolsManager = __decorate([
    (0, tsyringe_1.singleton)(),
    (0, tsyringe_1.injectable)()
], DevToolsManager);
//# sourceMappingURL=DevToolsManager.js.map