"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 find_exports = {};
__export(find_exports, {
  readEnv: () => readEnv,
  resolve: () => resolve,
  resolveSync: () => resolveSync
});
module.exports = __toCommonJS(find_exports);
var import_keychain = require("./keychain");
function readEnv(name) {
  const value = process.env[name];
  if (typeof value !== "string") {
    return void 0;
  }
  const trimmed = value.trim();
  return trimmed ? trimmed : void 0;
}
async function resolve(opts) {
  const { accounts, allowEnvOnly, service } = opts;
  for (let i = 0, { length } = accounts; i < length; i += 1) {
    const account = accounts[i];
    const fromEnv = readEnv(account);
    if (fromEnv) {
      return { value: fromEnv, source: "env", account };
    }
  }
  if (allowEnvOnly) {
    return void 0;
  }
  for (let i = 0, { length } = accounts; i < length; i += 1) {
    const account = accounts[i];
    const fromKeychain = await (0, import_keychain.readSecret)({ service, account });
    if (fromKeychain) {
      return { value: fromKeychain, source: "keychain", account };
    }
  }
  return void 0;
}
function resolveSync(opts) {
  const { accounts, allowEnvOnly, service } = opts;
  for (let i = 0, { length } = accounts; i < length; i += 1) {
    const account = accounts[i];
    const fromEnv = readEnv(account);
    if (fromEnv) {
      return { value: fromEnv, source: "env", account };
    }
  }
  if (allowEnvOnly) {
    return void 0;
  }
  for (let i = 0, { length } = accounts; i < length; i += 1) {
    const account = accounts[i];
    const fromKeychain = (0, import_keychain.readSecretSync)({ service, account });
    if (fromKeychain) {
      return { value: fromKeychain, source: "keychain", account };
    }
  }
  return void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  readEnv,
  resolve,
  resolveSync
});
