import { createRequire } from "node:module";
const require2 = createRequire(import.meta.url);
let _keyring = null;
let _checked = false;
function loadKeyring() {
  if (_checked) return _keyring;
  _checked = true;
  try {
    _keyring = require2("@napi-rs/keyring");
    return _keyring;
  } catch {
    _keyring = null;
    return null;
  }
}
let _available = null;
function keychainAvailable() {
  if (_available !== null) return _available;
  const mod = loadKeyring();
  if (!mod) {
    _available = false;
    return false;
  }
  try {
    new mod.Entry("wigolo-probe", "probe");
    _available = true;
    return true;
  } catch {
    _available = false;
    return false;
  }
}
function _resetKeychainAvailability() {
  _available = null;
  _checked = false;
  _keyring = null;
}
const WIGOLO_SERVICE = "wigolo";
function keychainSet(service, user, value) {
  const mod = loadKeyring();
  if (!mod) throw new Error("keychain not available");
  const entry = new mod.Entry(service, user);
  entry.setPassword(value);
}
function keychainGet(service, user) {
  const mod = loadKeyring();
  if (!mod) return null;
  try {
    const entry = new mod.Entry(service, user);
    return entry.getPassword();
  } catch {
    return null;
  }
}
function keychainDelete(service, user) {
  const mod = loadKeyring();
  if (!mod) return;
  try {
    const entry = new mod.Entry(service, user);
    entry.deleteCredential();
  } catch {
  }
}
export {
  WIGOLO_SERVICE,
  _resetKeychainAvailability,
  keychainAvailable,
  keychainDelete,
  keychainGet,
  keychainSet
};
//# sourceMappingURL=keychain.js.map