import {
  tokenKey
} from "./chunk-GYGNEQSC.js";

// runtime/subsystems/jobs/job-handler.base.ts
var ParentClosePolicy = /* @__PURE__ */ ((ParentClosePolicy2) => {
  ParentClosePolicy2["Terminate"] = "terminate";
  ParentClosePolicy2["Cancel"] = "cancel";
  ParentClosePolicy2["Abandon"] = "abandon";
  return ParentClosePolicy2;
})(ParentClosePolicy || {});
var JobHandlerBase = class {
};
var JOB_HANDLER_REGISTRY = /* @__PURE__ */ new Map();
var JOB_HANDLER_METADATA_KEY = Symbol.for(tokenKey("jobs", "handler-metadata"));
function JobHandler(type, meta) {
  return (target) => {
    if (JOB_HANDLER_REGISTRY.has(type)) {
      const env = process.env.NODE_ENV;
      if (env === "production") {
        throw new Error(
          `[JobHandler] Duplicate registration for job type '${type}'. Each @JobHandler must declare a unique type.`
        );
      }
      if (env !== "test") {
        console.warn(
          `[JobHandler] Duplicate registration for job type '${type}'. Overwriting previous handler \u2014 this is almost certainly a bug.`
        );
      }
    }
    Reflect.defineMetadata(JOB_HANDLER_METADATA_KEY, { type, meta }, target);
    JOB_HANDLER_REGISTRY.set(type, {
      type,
      meta,
      handlerClass: target
    });
  };
}
var HandlerRegistry;
((HandlerRegistry2) => {
  function getAll() {
    return Array.from(JOB_HANDLER_REGISTRY.values());
  }
  HandlerRegistry2.getAll = getAll;
  function get(type) {
    return JOB_HANDLER_REGISTRY.get(type);
  }
  HandlerRegistry2.get = get;
})(HandlerRegistry || (HandlerRegistry = {}));
var FN_KEY_SENTINEL = "<fn>";
function keySelectorToTemplate(key) {
  if (typeof key === "string") return key;
  if (typeof key === "function") return FN_KEY_SENTINEL;
  return null;
}
function resolveJobKey(kind, type, template, payload, evaluateTemplate) {
  if (template == null) return null;
  if (template !== FN_KEY_SENTINEL) return evaluateTemplate(template, payload);
  const meta = JOB_HANDLER_REGISTRY.get(type)?.meta;
  const key = meta?.[kind]?.key;
  if (typeof key !== "function") {
    throw new JobKeyFunctionUnavailableError(type, kind);
  }
  return key(payload);
}
var JobKeyFunctionUnavailableError = class extends Error {
  constructor(jobType, kind) {
    super(
      `[jobs] ${kind} key for job '${jobType}' was persisted as a function sentinel ('${FN_KEY_SENTINEL}') but no live function is registered for it. The @JobHandler must be imported before start() so its meta is in JOB_HANDLER_REGISTRY.`
    );
    this.jobType = jobType;
    this.kind = kind;
    this.name = "JobKeyFunctionUnavailableError";
  }
  jobType;
  kind;
};

export {
  ParentClosePolicy,
  JobHandlerBase,
  JOB_HANDLER_REGISTRY,
  JOB_HANDLER_METADATA_KEY,
  JobHandler,
  HandlerRegistry,
  FN_KEY_SENTINEL,
  keySelectorToTemplate,
  resolveJobKey,
  JobKeyFunctionUnavailableError
};
//# sourceMappingURL=chunk-7P5ODGLA.js.map