// src/lib/pnpmConfig/readMinReleaseAge.ts
import { existsSync, readFileSync } from "node:fs";
import os from "node:os";
import PATH from "node:path";
import * as yaml from "yaml";
function emptySource() {
  return {
    excludeSet: void 0,
    ignoreMissingTime: void 0,
    minutes: void 0,
    strict: void 0
  };
}
function coerceNumber(value) {
  if (typeof value === "number" && Number.isFinite(value)) return value;
  if (typeof value === "string" && value.trim() !== "") {
    const n = Number(value);
    return Number.isFinite(n) ? n : void 0;
  }
  return void 0;
}
function coerceBoolean(value) {
  if (typeof value === "boolean") return value;
  if (typeof value === "string") {
    const lower = value.trim().toLowerCase();
    if (lower === "true") return true;
    if (lower === "false") return false;
  }
  return void 0;
}
function coerceStringArray(value) {
  if (Array.isArray(value)) {
    return value.filter((v) => typeof v === "string" && v.length > 0);
  }
  if (typeof value === "string") {
    return value.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
  }
  return void 0;
}
function readWorkspaceYaml(filePath) {
  const out = emptySource();
  if (!existsSync(filePath)) return out;
  let parsed;
  try {
    parsed = yaml.parse(readFileSync(filePath, "utf8"));
  } catch {
    return out;
  }
  if (parsed === null || typeof parsed !== "object") return out;
  const obj = parsed;
  out.minutes = coerceNumber(obj.minimumReleaseAge);
  out.excludeSet = coerceStringArray(obj.minimumReleaseAgeExclude);
  out.ignoreMissingTime = coerceBoolean(obj.minimumReleaseAgeIgnoreMissingTime);
  out.strict = coerceBoolean(obj.minimumReleaseAgeStrict);
  return out;
}
function applyNpmrcKey(out, key, value) {
  switch (key) {
    case "minimum-release-age":
    case "minimumreleaseage": {
      const n = coerceNumber(value);
      if (n !== void 0) out.minutes = n;
      return;
    }
    case "minimum-release-age-exclude":
    case "minimumreleaseageexclude": {
      const arr = coerceStringArray(value);
      if (arr !== void 0) out.excludeSet = arr;
      return;
    }
    case "minimum-release-age-ignore-missing-time":
    case "minimumreleaseageignoremissingtime": {
      const b = coerceBoolean(value);
      if (b !== void 0) out.ignoreMissingTime = b;
      return;
    }
    case "minimum-release-age-strict":
    case "minimumreleaseagestrict": {
      const b = coerceBoolean(value);
      if (b !== void 0) out.strict = b;
    }
  }
}
function readNpmrc(filePath) {
  const out = emptySource();
  if (!existsSync(filePath)) return out;
  let text;
  try {
    text = readFileSync(filePath, "utf8");
  } catch {
    return out;
  }
  for (const rawLine of text.split("\n")) {
    const line = rawLine.trim();
    if (!line || line.startsWith("#") || line.startsWith(";")) continue;
    const eq = line.indexOf("=");
    if (eq === -1) continue;
    const key = line.slice(0, eq).trim().toLowerCase();
    const value = line.slice(eq + 1).trim().replaceAll(/^["']|["']$/g, "");
    applyNpmrcKey(out, key, value);
  }
  return out;
}
function merge(primary, fallback) {
  return {
    minutes: primary.minutes ?? fallback.minutes,
    excludeSet: primary.excludeSet ?? fallback.excludeSet,
    ignoreMissingTime: primary.ignoreMissingTime ?? fallback.ignoreMissingTime,
    strict: primary.strict ?? fallback.strict
  };
}
function readMinReleaseAgeConfig(cwd) {
  const workspace = readWorkspaceYaml(PATH.resolve(cwd, "pnpm-workspace.yaml"));
  const projectNpmrc = readNpmrc(PATH.resolve(cwd, ".npmrc"));
  const userNpmrc = readNpmrc(PATH.resolve(os.homedir(), ".npmrc"));
  const merged = merge(merge(workspace, projectNpmrc), userNpmrc);
  return {
    minutes: merged.minutes,
    exclude: merged.excludeSet ?? [],
    ignoreMissingTime: merged.ignoreMissingTime ?? true,
    strict: merged.strict ?? true
  };
}
export {
  readMinReleaseAgeConfig
};
//# sourceMappingURL=index.mjs.map
