"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
var utils_exports = {};
__export(utils_exports, {
  activeTabId: () => activeTabId,
  currentWindowId: () => currentWindowId,
  getExtensionVersion: () => getExtensionVersion,
  getPlaygroundUrl: () => getPlaygroundUrl,
  getTabInfo: () => getTabInfo,
  sendToWorker: () => sendToWorker,
  workerMessageTypes: () => workerMessageTypes
});
module.exports = __toCommonJS(utils_exports);
var import_buffer = __toESM(require("buffer"));
var import_browser = __toESM(require("process/browser"));
var import_console_browserify = __toESM(require("console-browserify"));
var buffer = {
  Buffer: import_buffer.default
};
const workerMessageTypes = {
  SAVE_CONTEXT: "save-context",
  GET_CONTEXT: "get-context"
};
function sendToWorker(type, payload) {
  return __async(this, null, function* () {
    return new Promise((resolve, reject) => {
      chrome.runtime.sendMessage({ type, payload }, (response) => {
        if (response.error) {
          reject(response.error);
        } else {
          resolve(response);
        }
      });
    });
  });
}
function getPlaygroundUrl(cacheContextId) {
  return chrome.runtime.getURL(
    `./pages/playground.html?cache_context_id=${cacheContextId}`
  );
}
function activeTabId() {
  return __async(this, null, function* () {
    return new Promise((resolve, reject) => {
      chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
        var _a;
        if ((_a = tabs == null ? void 0 : tabs[0]) == null ? void 0 : _a.id) {
          resolve(tabs[0].id);
        } else {
          reject(new Error("No active tab found"));
        }
      });
    });
  });
}
function currentWindowId() {
  return __async(this, null, function* () {
    return new Promise((resolve, reject) => {
      chrome.windows.getCurrent((window) => {
        if (window == null ? void 0 : window.id) {
          resolve(window.id);
        } else {
          reject(new Error("No active window found"));
        }
      });
    });
  });
}
function getExtensionVersion() {
  return chrome.runtime.getManifest().version;
}
function getTabInfo(tabId) {
  return __async(this, null, function* () {
    return yield chrome.tabs.get(tabId);
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  activeTabId,
  currentWindowId,
  getExtensionVersion,
  getPlaygroundUrl,
  getTabInfo,
  sendToWorker,
  workerMessageTypes
});
