import { PERSISTENT_GOAL_CUSTOM_KEY, defaultMaxTurns, mergeCustomDataPatch, readPersistentGoal, serializePersistentGoal } from "./state.js";
//#region src/agent/goals/patch-from-user-action.ts
function baseCustomData(customData) {
	return { ...customData ?? {} };
}
/**
* Pure transition for `/goal` slash and `POST /api/goals/webchat` so behaviour stays aligned.
*/
function applyPersistentGoalUserAction(customData, action, goalsCfg) {
	const s = readPersistentGoal(customData);
	if (action === "clear") {
		if (!s || s.status === "cleared") return {
			kind: "noop",
			message: "No active goal."
		};
		const b = baseCustomData(customData);
		delete b[PERSISTENT_GOAL_CUSTOM_KEY];
		return {
			kind: "updated",
			customData: b
		};
	}
	if (!s || s.status === "cleared") {
		if (action === "pause") return {
			kind: "noop",
			message: "No goal set."
		};
		if (action === "resume") return {
			kind: "noop",
			message: "No goal to resume."
		};
		if (action === "restart") return {
			kind: "error",
			error: "No goal to restart."
		};
		return {
			kind: "error",
			error: "No goal set."
		};
	}
	if (action === "pause") {
		if (s.status !== "active") return {
			kind: "noop",
			message: statusLineForMessage(s)
		};
		const next = {
			...s,
			status: "paused",
			pausedReason: "user-paused"
		};
		return {
			kind: "updated",
			customData: mergeCustomDataPatch(baseCustomData(customData), { [PERSISTENT_GOAL_CUSTOM_KEY]: serializePersistentGoal(next) })
		};
	}
	if (action === "resume") {
		const next = {
			...s,
			status: "active",
			pausedReason: void 0,
			turnsUsed: 0,
			consecutiveParseFailures: 0
		};
		return {
			kind: "updated",
			customData: mergeCustomDataPatch(baseCustomData(customData), { [PERSISTENT_GOAL_CUSTOM_KEY]: serializePersistentGoal(next) })
		};
	}
	const maxTurns = typeof s.maxTurns === "number" && Number.isFinite(s.maxTurns) ? Math.max(1, Math.min(500, Math.floor(s.maxTurns))) : defaultMaxTurns(goalsCfg);
	const judgeFromCfg = typeof goalsCfg?.judgeModelRef === "string" ? goalsCfg.judgeModelRef.trim() : void 0;
	const judgeModelRef = (s.judgeModelRef ?? judgeFromCfg)?.trim() || void 0;
	const next = {
		goal: s.goal,
		status: "active",
		turnsUsed: 0,
		maxTurns,
		createdAt: Date.now(),
		lastTurnAt: 0,
		consecutiveParseFailures: 0,
		...judgeModelRef ? { judgeModelRef } : {},
		...s.uiLocale ? { uiLocale: s.uiLocale } : {},
		...s.checklist?.length ? {
			checklist: s.checklist.map((it) => ({ ...it })),
			decomposed: true
		} : {}
	};
	return {
		kind: "updated",
		customData: mergeCustomDataPatch(baseCustomData(customData), { [PERSISTENT_GOAL_CUSTOM_KEY]: serializePersistentGoal(next) }),
		kickoff: s.goal
	};
}
function statusLineForMessage(state) {
	const turns = `${state.turnsUsed}/${state.maxTurns} turns`;
	if (state.status === "active") return `⊙ Goal (active, ${turns}): ${state.goal}`;
	if (state.status === "paused") return `⏸ Goal (paused, ${turns}${state.pausedReason ? ` — ${state.pausedReason}` : ""}): ${state.goal}`;
	if (state.status === "done") return `✓ Goal done (${turns}): ${state.goal}`;
	return `Goal (${state.status}, ${turns}): ${state.goal}`;
}
//#endregion
export { applyPersistentGoalUserAction };

//# sourceMappingURL=patch-from-user-action.js.map