// src/background.ts
chrome.action.onClicked.addListener((tab) => {
  const tabId = tab?.id;
  if (tabId && chrome.sidePanel.open) {
    chrome.sidePanel.open({ tabId });
  }
});
console.log("[Background] onUserScriptMessage available:", !!chrome.runtime.onUserScriptMessage);
if (chrome.runtime.onUserScriptMessage) {
  chrome.runtime.onUserScriptMessage.addListener((message, sender, sendResponse) => {
    console.log("[Background] Received userScript message:", message, "from:", sender);
    if (message.type === "abort-repl") {
      console.log("[Background] Relaying abort-repl to sidepanels");
      chrome.runtime.sendMessage(message);
      sendResponse({ success: true });
      return true;
    }
  });
  console.log("[Background] onUserScriptMessage listener registered");
} else {
  console.error("[Background] onUserScriptMessage NOT available!");
}
var SIDEPANEL_OPEN_KEY = "sidepanel_open_windows";
var SESSION_LOCKS_KEY = "session_locks";
var openSidepanels = /* @__PURE__ */ new Set();
chrome.storage.session.get(SIDEPANEL_OPEN_KEY, (data) => {
  openSidepanels = new Set(data[SIDEPANEL_OPEN_KEY] || []);
  console.log("[Background] Initialized openSidepanels cache:", Array.from(openSidepanels));
});
chrome.runtime.onConnect.addListener((port) => {
  const match = /^sidepanel:(\d+)$/.exec(port.name);
  if (!match) return;
  const windowId = Number(match[1]);
  openSidepanels.add(windowId);
  chrome.storage.session.get(SIDEPANEL_OPEN_KEY, (data) => {
    const openWindows = new Set(data[SIDEPANEL_OPEN_KEY] || []);
    openWindows.add(windowId);
    chrome.storage.session.set({ [SIDEPANEL_OPEN_KEY]: Array.from(openWindows) });
  });
  port.onMessage.addListener((msg) => {
    if (msg.type === "acquireLock") {
      const { sessionId, windowId: reqWindowId } = msg;
      chrome.storage.session.get(SESSION_LOCKS_KEY, (data) => {
        const sessionLocks = data[SESSION_LOCKS_KEY] || {};
        const ownerWindowId = sessionLocks[sessionId];
        const ownerSidepanelOpen = ownerWindowId !== void 0 && openSidepanels.has(ownerWindowId);
        const success = !ownerWindowId || !ownerSidepanelOpen || ownerWindowId === reqWindowId;
        const response = success ? {
          type: "lockResult",
          sessionId,
          success: true
        } : {
          type: "lockResult",
          sessionId,
          success: false,
          ownerWindowId
        };
        if (success) {
          sessionLocks[sessionId] = reqWindowId;
          chrome.storage.session.set({ [SESSION_LOCKS_KEY]: sessionLocks });
        }
        port.postMessage(response);
      });
    } else if (msg.type === "getLockedSessions") {
      chrome.storage.session.get(SESSION_LOCKS_KEY, (data) => {
        const locks = data[SESSION_LOCKS_KEY] || {};
        const response = {
          type: "lockedSessions",
          locks
        };
        port.postMessage(response);
      });
    }
  });
  port.onDisconnect.addListener(() => {
    closeSidepanel(windowId, false);
  });
});
chrome.windows.onRemoved.addListener((windowId) => {
  closeSidepanel(windowId, false);
});
chrome.commands.onCommand.addListener((command, sender) => {
  if (command === "toggle-sidepanel") {
    if (!sender?.windowId) {
      console.log("[Background] Cannot toggle sidepanel: sender windowId not available");
      return;
    }
    const windowId = sender.windowId;
    if (openSidepanels.has(windowId)) {
      closeSidepanel(windowId);
    } else {
      chrome.sidePanel.open({ windowId });
    }
  }
});
function closeSidepanel(windowId, callCloseOnSidePanelAPI = true) {
  if (callCloseOnSidePanelAPI) {
    chrome.sidePanel.close({ windowId });
  }
  openSidepanels.delete(windowId);
  chrome.storage.session.get([SESSION_LOCKS_KEY, SIDEPANEL_OPEN_KEY], (data) => {
    const sessionLocks = data[SESSION_LOCKS_KEY] || {};
    for (const sessionId in sessionLocks) {
      if (sessionLocks[sessionId] === windowId) {
        delete sessionLocks[sessionId];
      }
    }
    const openWindows = new Set(data[SIDEPANEL_OPEN_KEY] || []);
    openWindows.delete(windowId);
    chrome.storage.session.set({
      [SESSION_LOCKS_KEY]: sessionLocks,
      [SIDEPANEL_OPEN_KEY]: Array.from(openWindows)
    });
  });
}
//# sourceMappingURL=background.js.map
