import { o as normalizeOptionalLowercaseString, s as normalizeOptionalString } from "./string-coerce-CIT1-v_a.js";
import { b as truncateUtf16Safe } from "./utils-CE5zxr9Z.js";
import { c as normalizeAgentId } from "./session-key-NGoeHrFI.js";
import { z } from "zod";
//#region src/cron/delivery-field-schemas.ts
const trimStringPreprocess = (value) => typeof value === "string" ? value.trim() : value;
const trimLowercaseStringPreprocess = (value) => normalizeOptionalLowercaseString(value) ?? value;
const DeliveryModeFieldSchema = z.preprocess(trimLowercaseStringPreprocess, z.enum([
	"deliver",
	"announce",
	"none",
	"webhook"
])).transform((value) => value === "deliver" ? "announce" : value);
const LowercaseNonEmptyStringFieldSchema = z.preprocess(trimLowercaseStringPreprocess, z.string().min(1));
const TrimmedNonEmptyStringFieldSchema = z.preprocess(trimStringPreprocess, z.string().min(1));
const DeliveryThreadIdFieldSchema = z.union([TrimmedNonEmptyStringFieldSchema, z.number().finite()]);
const TimeoutSecondsFieldSchema = z.number().finite().transform((value) => Math.max(0, Math.floor(value)));
function parseDeliveryInput(input) {
	return {
		mode: parseOptionalField(DeliveryModeFieldSchema, input.mode),
		channel: parseOptionalField(LowercaseNonEmptyStringFieldSchema, input.channel),
		to: parseOptionalField(TrimmedNonEmptyStringFieldSchema, input.to),
		threadId: parseOptionalField(DeliveryThreadIdFieldSchema, input.threadId),
		accountId: parseOptionalField(TrimmedNonEmptyStringFieldSchema, input.accountId)
	};
}
function parseOptionalField(schema, value) {
	const parsed = schema.safeParse(value);
	return parsed.success ? parsed.data : void 0;
}
//#endregion
//#region src/cron/service/normalize.ts
function normalizeRequiredName(raw) {
	if (typeof raw !== "string") throw new Error("cron job name is required");
	const name = raw.trim();
	if (!name) throw new Error("cron job name is required");
	return name;
}
function truncateText(input, maxLen) {
	if (input.length <= maxLen) return input;
	return `${truncateUtf16Safe(input, Math.max(0, maxLen - 1)).trimEnd()}…`;
}
function normalizeOptionalAgentId(raw) {
	const trimmed = normalizeOptionalString(raw);
	if (!trimmed) return;
	return normalizeAgentId(trimmed);
}
function inferLegacyName(job) {
	const firstLine = (job?.payload?.kind === "systemEvent" && typeof job.payload.text === "string" ? job.payload.text : job?.payload?.kind === "agentTurn" && typeof job.payload.message === "string" ? job.payload.message : "").split("\n").map((l) => l.trim()).find(Boolean) ?? "";
	if (firstLine) return truncateText(firstLine, 60);
	const kind = typeof job?.schedule?.kind === "string" ? job.schedule.kind : "";
	if (kind === "cron" && typeof job?.schedule?.expr === "string") return `Cron: ${truncateText(job.schedule.expr, 52)}`;
	if (kind === "every" && typeof job?.schedule?.everyMs === "number") return `Every: ${job.schedule.everyMs}ms`;
	if (kind === "at") return "One-shot";
	return "Cron job";
}
function normalizePayloadToSystemText(payload) {
	if (payload.kind === "systemEvent") return payload.text.trim();
	return payload.message.trim();
}
//#endregion
export { DeliveryThreadIdFieldSchema as a, TrimmedNonEmptyStringFieldSchema as c, normalizeRequiredName as i, parseDeliveryInput as l, normalizeOptionalAgentId as n, LowercaseNonEmptyStringFieldSchema as o, normalizePayloadToSystemText as r, TimeoutSecondsFieldSchema as s, inferLegacyName as t, parseOptionalField as u };
