let p = null, f = null, i = null, t = null, w = null;
async function m(r) {
  if (w = r, p = await import("./index-TQvT4fZq.js"), r.logging?.filterSqlTrace) {
    const o = console.log, a = console.error;
    console.log = (...l) => {
      const n = l.join(" ");
      !n.includes("SQL TRACE") && !n.includes("TRACE #") && o(...l);
    }, console.error = (...l) => {
      const n = l.join(" ");
      !n.includes("SQL TRACE") && !n.includes("TRACE #") && a(...l);
    };
  }
  f = await p.default({
    // eslint-disable-next-line no-console
    print: console.log,
    printErr: console.error
  });
  const e = r.vfs?.type || "opfs";
  e === "opfs-sahpool" ? i = await f.installOpfsSAHPoolVfs({
    initialCapacity: r.vfs?.poolConfig?.initialCapacity || 3,
    clearOnInit: r.vfs?.poolConfig?.clearOnInit || !1,
    name: r.vfs?.poolConfig?.name || "sqlite-wasm-pool"
  }) : e === "opfs" ? i = f.opfs || null : e === "memdb" && (i = null);
}
async function y(r) {
  if (t) throw new Error("Database already opened");
  const e = w?.vfs?.type || "opfs";
  if (e === "opfs-sahpool" && i ? t = new i.OpfsSAHPoolDb({
    filename: r,
    flags: "create",
    vfs: "opfs-sahpool"
  }) : e === "memdb" ? t = new f.oo1.DB(":memory:", "c") : t = new f.oo1.OpfsDb(r, "c"), w?.pragma)
    for (const [o, a] of Object.entries(w.pragma))
      a !== void 0 && await d(`PRAGMA ${o} = ${a}`);
}
async function g() {
  if (!t) throw new Error("Database not opened");
  await t.close(), t = null, i = null, f = null, p = null, w = null;
}
async function d(r, e = []) {
  if (!t) throw new Error("Database not opened");
  await t.exec({
    sql: r,
    bind: e,
    rowMode: "array"
  });
}
async function h(r, e = []) {
  if (!t) throw new Error("Database not opened");
  const o = [];
  return await t.exec({
    sql: r,
    bind: e,
    rowMode: "array",
    callback: (a, l) => {
      const n = l.getColumnNames(), c = {};
      for (let u = 0; u < n.length; u++) {
        const s = a[u];
        if (typeof s == "string" && (s.startsWith("{") || s.startsWith("[")))
          try {
            c[n[u]] = JSON.parse(s);
          } catch {
            c[n[u]] = s;
          }
        else
          c[n[u]] = s;
      }
      o.push(c);
    }
  }), o;
}
async function b(r, e = []) {
  if (!t) throw new Error("Database not opened");
  await t.exec({
    sql: r,
    bind: e,
    rowMode: "array"
  });
  const o = Number(t.changes) || 0;
  return { lastInsertRowId: o > 0 ? Number(t.selectValue("SELECT last_insert_rowid()")) : void 0, changes: o };
}
async function E() {
  if (!i?.exportFile)
    throw new Error("Export not supported for this VFS type");
  return await i.exportFile(`/${w?.filename}`);
}
async function D(r, e) {
  if (!i?.importDb)
    throw new Error("Import not supported for this VFS type");
  await i.importDb(r, e);
}
async function S() {
  t && (await t.close(), t = null), i?.wipeFiles && await i.wipeFiles();
}
async function q(r) {
  const { id: e, operation: o, sql: a, params: l, config: n, filename: c, data: u } = r;
  try {
    switch (o) {
      case "init": {
        if (!n) throw new Error("Configuration required for init");
        return await m(n), { id: e, status: "ready" };
      }
      case "open": {
        if (!c) throw new Error("Filename required for open");
        return await y(c), { id: e, status: "success" };
      }
      case "close":
        return await g(), { id: e, status: "success" };
      case "exec": {
        if (!a) throw new Error("SQL required for exec");
        return await d(a, l), { id: e, status: "success" };
      }
      case "query": {
        if (!a) throw new Error("SQL required for query");
        const s = await h(a, l);
        return { id: e, status: "success", results: s };
      }
      case "run": {
        if (!a) throw new Error("SQL required for run");
        const s = await b(a, l);
        return { id: e, status: "success", results: s };
      }
      case "export": {
        const s = await E();
        return { id: e, status: "success", results: s };
      }
      case "import": {
        if (!c || !u) throw new Error("Filename and data required for import");
        return await D(c, u), { id: e, status: "success" };
      }
      case "delete":
        return await S(), { id: e, status: "success" };
      default:
        throw new Error(`Unknown operation: ${o}`);
    }
  } catch (s) {
    return { id: e, status: "error", error: s instanceof Error ? s.message : "Unknown error" };
  }
}
self.addEventListener("message", async (r) => {
  const e = r.data;
  if (self.origin && r.origin && r.origin !== self.origin)
    return self.postMessage({ id: e.id, status: "error", error: "Invalid message origin" });
  const o = await q(e);
  self.postMessage(o);
});
//# sourceMappingURL=worker.js.map
