"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 macos_exports = {};
__export(macos_exports, {
  deleteMacOS: () => deleteMacOS,
  deleteMacOSSync: () => deleteMacOSSync,
  isMacOSBackendAvailable: () => isMacOSBackendAvailable,
  readMacOS: () => readMacOS,
  readMacOSSync: () => readMacOSSync,
  runAsync: () => runAsync,
  writeMacOS: () => writeMacOS,
  writeMacOSSync: () => writeMacOSSync
});
module.exports = __toCommonJS(macos_exports);
var import_node_child_process = require("node:child_process");
const SECURITY_BIN = "security";
async function deleteMacOS(service, account) {
  const r = await runAsync(
    ["delete-generic-password", "-s", service, "-a", account],
    { stdio: "ignore" }
  );
  return r.status === 0 ? "removed" : "absent";
}
function deleteMacOSSync(service, account) {
  const r = (0, import_node_child_process.spawnSync)(
    SECURITY_BIN,
    ["delete-generic-password", "-s", service, "-a", account],
    { stdio: "ignore" }
  );
  return r.status === 0 ? "removed" : "absent";
}
function isMacOSBackendAvailable() {
  return true;
}
async function readMacOS(service, account) {
  const r = await runAsync([
    "find-generic-password",
    "-s",
    service,
    "-a",
    account,
    "-w"
  ]);
  if (r.status !== 0) {
    return void 0;
  }
  const out = r.stdout.trim();
  return out || void 0;
}
function readMacOSSync(service, account) {
  const r = (0, import_node_child_process.spawnSync)(
    SECURITY_BIN,
    ["find-generic-password", "-s", service, "-a", account, "-w"],
    { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }
  );
  if (r.status !== 0) {
    return void 0;
  }
  const out = r.stdout.trim();
  return out || void 0;
}
function runAsync(args, opts = {}) {
  return new Promise((resolve) => {
    const child = (0, import_node_child_process.spawn)(SECURITY_BIN, args, {
      stdio: opts.stdio ?? ["ignore", "pipe", "pipe"]
    });
    let stdout = "";
    let stderr = "";
    if (child.stdout) {
      child.stdout.setEncoding("utf8");
      child.stdout.on("data", (chunk) => {
        stdout += chunk;
      });
    }
    if (child.stderr) {
      child.stderr.setEncoding("utf8");
      child.stderr.on("data", (chunk) => {
        stderr += chunk;
      });
    }
    child.on("error", () => resolve({ status: -1, stdout, stderr }));
    child.on("close", (status) => resolve({ status, stdout, stderr }));
  });
}
async function writeMacOS(service, account, value, label) {
  const r = await runAsync([
    "add-generic-password",
    "-U",
    "-A",
    "-T",
    "",
    "-s",
    service,
    "-a",
    account,
    "-w",
    value,
    "-D",
    label,
    "-l",
    label
  ]);
  if (r.status !== 0) {
    throw new Error(
      `security(1) add-generic-password failed (status=${r.status}, account=${account}): ${r.stderr.trim()}`
    );
  }
}
function writeMacOSSync(service, account, value, label) {
  const r = (0, import_node_child_process.spawnSync)(
    SECURITY_BIN,
    [
      "add-generic-password",
      "-U",
      "-A",
      "-T",
      "",
      "-s",
      service,
      "-a",
      account,
      "-w",
      value,
      "-D",
      label,
      "-l",
      label
    ],
    { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }
  );
  if (r.status !== 0) {
    throw new Error(
      `security(1) add-generic-password failed (status=${r.status}, account=${account}): ${r.stderr.trim()}`
    );
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  deleteMacOS,
  deleteMacOSSync,
  isMacOSBackendAvailable,
  readMacOS,
  readMacOSSync,
  runAsync,
  writeMacOS,
  writeMacOSSync
});
