"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 keychain_exports = {};
__export(keychain_exports, {
  clearCache: () => clearCache,
  deleteSecret: () => deleteSecret,
  deleteSecretFromSlots: () => deleteSecretFromSlots,
  deleteSecretFromSlotsSync: () => deleteSecretFromSlotsSync,
  deleteSecretSync: () => deleteSecretSync,
  detectPlatform: () => detectPlatform,
  getBackendAvailability: () => getBackendAvailability,
  readSecret: () => readSecret,
  readSecretFromSlots: () => readSecretFromSlots,
  readSecretFromSlotsSync: () => readSecretFromSlotsSync,
  readSecretSync: () => readSecretSync,
  writeSecret: () => writeSecret,
  writeSecretSync: () => writeSecretSync,
  writeSecretToSlots: () => writeSecretToSlots,
  writeSecretToSlotsSync: () => writeSecretToSlotsSync
});
module.exports = __toCommonJS(keychain_exports);
var import_node_os = require("node:os");
var import_internal = require("./_internal");
var import_macos = require("./macos");
var import_linux = require("./linux");
var import_windows = require("./windows");
function clearCache() {
  (0, import_internal.invalidateAll)();
}
async function deleteSecret({
  service,
  account
}) {
  let outcome;
  switch (detectPlatform()) {
    case "darwin":
      outcome = await (0, import_macos.deleteMacOS)(service, account);
      break;
    case "linux":
      outcome = await (0, import_linux.deleteLinux)(service, account);
      break;
    case "win32":
      outcome = await (0, import_windows.deleteWindows)(service, account);
      break;
    default:
      outcome = "absent";
  }
  (0, import_internal.invalidate)(service, account);
  return outcome;
}
async function deleteSecretFromSlots({
  service,
  accounts
}) {
  const results = [];
  for (const account of accounts) {
    const outcome = await deleteSecret({ service, account });
    results.push({ account, outcome });
  }
  return results;
}
function deleteSecretFromSlotsSync({
  service,
  accounts
}) {
  const results = [];
  for (const account of accounts) {
    const outcome = deleteSecretSync({ service, account });
    results.push({ account, outcome });
  }
  return results;
}
function deleteSecretSync({
  service,
  account
}) {
  let outcome;
  switch (detectPlatform()) {
    case "darwin":
      outcome = (0, import_macos.deleteMacOSSync)(service, account);
      break;
    case "linux":
      outcome = (0, import_linux.deleteLinuxSync)(service, account);
      break;
    case "win32":
      outcome = (0, import_windows.deleteWindowsSync)(service, account);
      break;
    default:
      outcome = "absent";
  }
  (0, import_internal.invalidate)(service, account);
  return outcome;
}
function detectPlatform() {
  const p = (0, import_node_os.platform)();
  if (p === "darwin" || p === "linux" || p === "win32") {
    return p;
  }
  return "other";
}
function getBackendAvailability() {
  const platform_ = detectPlatform();
  switch (platform_) {
    case "darwin": {
      return {
        available: (0, import_macos.isMacOSBackendAvailable)(),
        toolName: "security(1)",
        installHint: void 0
      };
    }
    case "linux": {
      const available = (0, import_linux.isLinuxBackendAvailable)();
      return {
        available,
        toolName: "secret-tool",
        installHint: available ? void 0 : "apt install libsecret-tools  (Debian/Ubuntu) | dnf install libsecret  (Fedora/RHEL)"
      };
    }
    case "win32": {
      return {
        available: (0, import_windows.isWindowsBackendAvailable)(),
        toolName: "PowerShell (CredentialManager / DPAPI)",
        installHint: void 0
      };
    }
    default:
      return {
        available: false,
        toolName: "n/a",
        installHint: `Platform ${(0, import_node_os.platform)()} is not supported.`
      };
  }
}
async function readSecret({
  service,
  account
}) {
  return (0, import_internal.dedupeRead)(service, account, async () => {
    switch (detectPlatform()) {
      case "darwin":
        return (0, import_macos.readMacOS)(service, account);
      case "linux":
        return (0, import_linux.readLinux)(service, account);
      case "win32":
        return (0, import_windows.readWindows)(service, account);
      default:
        return void 0;
    }
  });
}
async function readSecretFromSlots({
  service,
  accounts
}) {
  for (const account of accounts) {
    const value = await readSecret({ service, account });
    if (value) {
      return { value, account };
    }
  }
  return void 0;
}
function readSecretFromSlotsSync({
  service,
  accounts
}) {
  for (const account of accounts) {
    const value = readSecretSync({ service, account });
    if (value) {
      return { value, account };
    }
  }
  return void 0;
}
function readSecretSync({
  service,
  account
}) {
  if ((0, import_internal.has)(service, account)) {
    return (0, import_internal.getCached)(service, account);
  }
  let value;
  switch (detectPlatform()) {
    case "darwin":
      value = (0, import_macos.readMacOSSync)(service, account);
      break;
    case "linux":
      value = (0, import_linux.readLinuxSync)(service, account);
      break;
    case "win32":
      value = (0, import_windows.readWindowsSync)(service, account);
      break;
    default:
      value = void 0;
  }
  (0, import_internal.setCached)(service, account, value);
  return value;
}
async function writeSecret({
  service,
  account,
  value,
  label
}) {
  if (!value || typeof value !== "string") {
    throw new TypeError("writeSecret: value must be a non-empty string");
  }
  const platform_ = detectPlatform();
  if (platform_ === "other") {
    throw new Error(
      `Unsupported platform: ${(0, import_node_os.platform)()}. Secret storage requires macOS, Linux, or Windows.`
    );
  }
  const current = await readSecret({ service, account });
  if (current === value) {
    return "unchanged";
  }
  const lbl = label ?? `${service} credential`;
  switch (platform_) {
    case "darwin":
      await (0, import_macos.writeMacOS)(service, account, value, lbl);
      break;
    case "linux":
      await (0, import_linux.writeLinux)(service, account, value, lbl);
      break;
    case "win32":
      await (0, import_windows.writeWindows)(service, account, value, lbl);
      break;
  }
  (0, import_internal.setCached)(service, account, value);
  return "written";
}
function writeSecretSync({
  service,
  account,
  value,
  label
}) {
  if (!value || typeof value !== "string") {
    throw new TypeError("writeSecret: value must be a non-empty string");
  }
  const platform_ = detectPlatform();
  if (platform_ === "other") {
    throw new Error(
      `Unsupported platform: ${(0, import_node_os.platform)()}. Secret storage requires macOS, Linux, or Windows.`
    );
  }
  const current = readSecretSync({ service, account });
  if (current === value) {
    return "unchanged";
  }
  const lbl = label ?? `${service} credential`;
  switch (platform_) {
    case "darwin":
      (0, import_macos.writeMacOSSync)(service, account, value, lbl);
      break;
    case "linux":
      (0, import_linux.writeLinuxSync)(service, account, value, lbl);
      break;
    case "win32":
      (0, import_windows.writeWindowsSync)(service, account, value, lbl);
      break;
  }
  (0, import_internal.setCached)(service, account, value);
  return "written";
}
async function writeSecretToSlots({
  service,
  accounts,
  value,
  label
}) {
  const results = [];
  for (const account of accounts) {
    const outcome = await writeSecret({ service, account, value, label });
    results.push({ account, outcome });
  }
  return results;
}
function writeSecretToSlotsSync({
  service,
  accounts,
  value,
  label
}) {
  const results = [];
  for (const account of accounts) {
    const outcome = writeSecretSync({ service, account, value, label });
    results.push({ account, outcome });
  }
  return results;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  clearCache,
  deleteSecret,
  deleteSecretFromSlots,
  deleteSecretFromSlotsSync,
  deleteSecretSync,
  detectPlatform,
  getBackendAvailability,
  readSecret,
  readSecretFromSlots,
  readSecretFromSlotsSync,
  readSecretSync,
  writeSecret,
  writeSecretSync,
  writeSecretToSlots,
  writeSecretToSlotsSync
});
