import { s as normalizeOptionalString } from "./string-coerce-CIT1-v_a.js";
import { t as buildTaskStatusSnapshot } from "./task-status-DM0dKSwU.js";
import { a as findTaskByRunId, b as resolveTaskForLookupToken, o as getTaskById, p as listTasksForRelatedSessionKey } from "./task-registry-Dwvvoolw.js";
//#region src/tasks/task-owner-access.ts
function canOwnerAccessTask(task, callerOwnerKey) {
	return task.scopeKind === "session" && normalizeOptionalString(task.ownerKey) === normalizeOptionalString(callerOwnerKey);
}
function getTaskByIdForOwner(params) {
	const task = getTaskById(params.taskId);
	return task && canOwnerAccessTask(task, params.callerOwnerKey) ? task : void 0;
}
function findTaskByRunIdForOwner(params) {
	const task = findTaskByRunId(params.runId);
	return task && canOwnerAccessTask(task, params.callerOwnerKey) ? task : void 0;
}
function listTasksForRelatedSessionKeyForOwner(params) {
	return listTasksForRelatedSessionKey(params.relatedSessionKey).filter((task) => canOwnerAccessTask(task, params.callerOwnerKey));
}
function buildTaskStatusSnapshotForRelatedSessionKeyForOwner(params) {
	return buildTaskStatusSnapshot(listTasksForRelatedSessionKeyForOwner({
		relatedSessionKey: params.relatedSessionKey,
		callerOwnerKey: params.callerOwnerKey
	}));
}
function findLatestTaskForRelatedSessionKeyForOwner(params) {
	return listTasksForRelatedSessionKeyForOwner(params)[0];
}
function resolveTaskForLookupTokenForOwner(params) {
	const direct = getTaskByIdForOwner({
		taskId: params.token,
		callerOwnerKey: params.callerOwnerKey
	});
	if (direct) return direct;
	const byRun = findTaskByRunIdForOwner({
		runId: params.token,
		callerOwnerKey: params.callerOwnerKey
	});
	if (byRun) return byRun;
	const related = findLatestTaskForRelatedSessionKeyForOwner({
		relatedSessionKey: params.token,
		callerOwnerKey: params.callerOwnerKey
	});
	if (related) return related;
	const raw = resolveTaskForLookupToken(params.token);
	return raw && canOwnerAccessTask(raw, params.callerOwnerKey) ? raw : void 0;
}
//#endregion
export { listTasksForRelatedSessionKeyForOwner as a, getTaskByIdForOwner as i, findLatestTaskForRelatedSessionKeyForOwner as n, resolveTaskForLookupTokenForOwner as o, findTaskByRunIdForOwner as r, buildTaskStatusSnapshotForRelatedSessionKeyForOwner as t };
