// runtime/subsystems/jobs/pool-config.loader.ts
import { existsSync, readFileSync } from "fs";
import { resolve } from "path";
import { parse as parseYaml } from "yaml";
var FRAMEWORK_POOLS = Object.freeze({
  events_inbound: Object.freeze({
    queue: "jobs-events-inbound",
    concurrency: 20,
    reserved: true,
    description: "Inbound events drain (events subsystem outbox)."
  }),
  events_change: Object.freeze({
    queue: "jobs-events-change",
    concurrency: 30,
    reserved: true,
    description: "Change events drain (events subsystem outbox)."
  }),
  events_outbound: Object.freeze({
    queue: "jobs-events-outbound",
    concurrency: 10,
    reserved: true,
    description: "Outbound events drain (events subsystem outbox)."
  }),
  interactive: Object.freeze({
    queue: "jobs-interactive",
    concurrency: 20,
    reserved: false,
    description: "User-facing latency-sensitive jobs."
  }),
  batch: Object.freeze({
    queue: "jobs-batch",
    concurrency: 5,
    reserved: false,
    description: "Default pool for background jobs."
  })
});
var RESERVED_POOL_NAMES = new Set(
  Object.entries(FRAMEWORK_POOLS).filter(([, def]) => def.reserved).map(([name]) => name)
);
var cache = /* @__PURE__ */ new Map();
function _resetPoolConfigCacheForTests() {
  cache.clear();
}
function loadPoolConfig(configPath) {
  const resolved = resolve(configPath ?? `${process.cwd()}/codegen.config.yaml`);
  const cached = cache.get(resolved);
  if (cached) return cached;
  const merged = /* @__PURE__ */ new Map();
  for (const [name, def] of Object.entries(FRAMEWORK_POOLS)) {
    merged.set(name, { ...def });
  }
  if (!existsSync(resolved)) {
    cache.set(resolved, merged);
    return merged;
  }
  let raw;
  try {
    raw = parseYaml(readFileSync(resolved, "utf8"));
  } catch (err) {
    throw new Error(
      `pool-config.loader: failed to parse YAML at ${resolved}: ${err.message}`
    );
  }
  const userPools = extractUserPools(raw);
  for (const [name, userDef] of Object.entries(userPools)) {
    const existing = merged.get(name);
    if (existing) {
      const next = {
        queue: existing.queue,
        concurrency: typeof userDef.concurrency === "number" ? userDef.concurrency : existing.concurrency,
        reserved: existing.reserved,
        description: userDef.description ?? existing.description
      };
      merged.set(name, next);
      continue;
    }
    if (typeof userDef.queue !== "string" || userDef.queue.length === 0) {
      throw new Error(
        `pool-config.loader: pool '${name}' must declare a non-empty 'queue'.`
      );
    }
    if (typeof userDef.concurrency !== "number" || userDef.concurrency <= 0) {
      throw new Error(
        `pool-config.loader: pool '${name}' must declare a positive 'concurrency'.`
      );
    }
    if (userDef.reserved === true) {
      throw new Error(
        `pool-config.loader: user-defined pool '${name}' cannot set 'reserved: true' \u2014 reserved is framework-only.`
      );
    }
    merged.set(name, {
      queue: userDef.queue,
      concurrency: userDef.concurrency,
      reserved: false,
      description: userDef.description
    });
  }
  cache.set(resolved, merged);
  return merged;
}
function allNonReservedPoolNames(config) {
  const out = [];
  for (const [name, def] of config) {
    if (!def.reserved) out.push(name);
  }
  return out;
}
function allPoolNames(config) {
  return [...config.keys()];
}
function extractUserPools(raw) {
  if (!raw || typeof raw !== "object") return {};
  const jobs = raw.jobs;
  if (!jobs || typeof jobs !== "object") return {};
  const pools = jobs.pools;
  if (!pools || typeof pools !== "object") return {};
  const out = {};
  for (const [name, def] of Object.entries(pools)) {
    if (!def || typeof def !== "object") continue;
    out[name] = def;
  }
  return out;
}

export {
  FRAMEWORK_POOLS,
  RESERVED_POOL_NAMES,
  _resetPoolConfigCacheForTests,
  loadPoolConfig,
  allNonReservedPoolNames,
  allPoolNames
};
//# sourceMappingURL=chunk-RHVN6NA7.js.map