"use strict";
/* Socket Lib - Built with esbuild */
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var linux_exports = {};
__export(linux_exports, {
  deleteLinux: () => deleteLinux,
  deleteLinuxSync: () => deleteLinuxSync,
  isLinuxBackendAvailable: () => isLinuxBackendAvailable,
  readLinux: () => readLinux,
  readLinuxSync: () => readLinuxSync,
  writeLinux: () => writeLinux,
  writeLinuxSync: () => writeLinuxSync
});
module.exports = __toCommonJS(linux_exports);
var import_node_child_process = require("node:child_process");
const SECRET_TOOL_BIN = "secret-tool";
async function deleteLinux(service, account) {
  return new Promise((resolve) => {
    const child = (0, import_node_child_process.spawn)(
      SECRET_TOOL_BIN,
      ["clear", "service", service, "user", account],
      { stdio: "ignore" }
    );
    child.on("error", () => resolve("absent"));
    child.on("close", (status) => resolve(status === 0 ? "removed" : "absent"));
  });
}
function deleteLinuxSync(service, account) {
  const r = (0, import_node_child_process.spawnSync)(
    SECRET_TOOL_BIN,
    ["clear", "service", service, "user", account],
    { stdio: "ignore" }
  );
  return r.status === 0 ? "removed" : "absent";
}
function isLinuxBackendAvailable() {
  const r = (0, import_node_child_process.spawnSync)(SECRET_TOOL_BIN, ["--version"], { stdio: "ignore" });
  return r.status === 0;
}
async function readLinux(service, account) {
  return new Promise((resolve) => {
    const child = (0, import_node_child_process.spawn)(
      SECRET_TOOL_BIN,
      ["lookup", "service", service, "user", account],
      { stdio: ["ignore", "pipe", "pipe"] }
    );
    let stdout = "";
    child.stdout.setEncoding("utf8");
    child.stdout.on("data", (chunk) => {
      stdout += chunk;
    });
    child.on("error", () => resolve(void 0));
    child.on("close", (status) => {
      if (status !== 0) {
        resolve(void 0);
        return;
      }
      const out = stdout.trim();
      resolve(out || void 0);
    });
  });
}
function readLinuxSync(service, account) {
  const r = (0, import_node_child_process.spawnSync)(
    SECRET_TOOL_BIN,
    ["lookup", "service", service, "user", account],
    { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }
  );
  if (r.status !== 0) {
    return void 0;
  }
  const out = r.stdout.trim();
  return out || void 0;
}
async function writeLinux(service, account, value, label) {
  return new Promise((resolve, reject) => {
    const child = (0, import_node_child_process.spawn)(
      SECRET_TOOL_BIN,
      ["store", `--label=${label}`, "service", service, "user", account],
      { stdio: ["pipe", "pipe", "pipe"] }
    );
    let stderr = "";
    child.stderr.setEncoding("utf8");
    child.stderr.on("data", (chunk) => {
      stderr += chunk;
    });
    child.on(
      "error",
      (err) => reject(
        new Error(
          `secret-tool store failed: ${err.message}. Install libsecret-tools (apt install libsecret-tools / dnf install libsecret) or ensure a Secret Service provider (gnome-keyring, kwallet) is running.`
        )
      )
    );
    child.on("close", (status) => {
      if (status === 0) {
        resolve();
        return;
      }
      reject(
        new Error(
          `secret-tool store failed (status=${status}, user=${account}): ${stderr.trim()}. Install libsecret-tools (apt install libsecret-tools / dnf install libsecret) or ensure a Secret Service provider (gnome-keyring, kwallet) is running.`
        )
      );
    });
    child.stdin.end(value);
  });
}
function writeLinuxSync(service, account, value, label) {
  const r = (0, import_node_child_process.spawnSync)(
    SECRET_TOOL_BIN,
    ["store", `--label=${label}`, "service", service, "user", account],
    {
      encoding: "utf8",
      input: value,
      stdio: ["pipe", "pipe", "pipe"]
    }
  );
  if (r.status !== 0) {
    throw new Error(
      `secret-tool store failed (status=${r.status}, user=${account}): ${r.stderr.trim()}. Install libsecret-tools (apt install libsecret-tools / dnf install libsecret) or ensure a Secret Service provider (gnome-keyring, kwallet) is running.`
    );
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  deleteLinux,
  deleteLinuxSync,
  isLinuxBackendAvailable,
  readLinux,
  readLinuxSync,
  writeLinux,
  writeLinuxSync
});
