import './paths-2M_UxD_H.js';
import { r as resolveSagaMemberIds } from './storage-B9UA3BHR.js';
import { t as taskListItemNext } from './mvi-helpers-BNsJ8ZpQ.js';

//#region ../core/dist/pagination.js
/**
* LAFS-compliant pagination utility for list commands.
*
* Wraps result arrays in LAFSPage offset pagination objects.
* Supports both offset-based and cursor-based pagination modes.
*
* @task T4668
* @epic T4663
*/
/**
* Default page size when limit is not specified.
*
* @task T4668
*/
var DEFAULT_LIMIT = 50;
/**
* Create an LAFSPage object from pagination parameters.
*
* Returns mode:"none" when no pagination is requested (no limit/offset).
* Returns mode:"offset" with hasMore/total when pagination is active.
*
* @task T4668
* @epic T4663
*/
function createPage(input) {
	const { total, limit, offset } = input;
	if (limit === void 0 && offset === void 0) return { mode: "none" };
	const effectiveLimit = limit ?? DEFAULT_LIMIT;
	const effectiveOffset = offset ?? 0;
	return {
		mode: "offset",
		limit: effectiveLimit,
		offset: effectiveOffset,
		hasMore: effectiveOffset + effectiveLimit < total,
		total
	};
}
/**
* Apply pagination to an array of items and return the sliced result with page metadata.
*
* @task T4668
* @epic T4663
*/
function paginate(items, limit, offset) {
	if (!items || items.length === 0) return {
		items: [],
		page: { mode: "none" }
	};
	const total = items.length;
	if (limit === void 0 && offset === void 0) return {
		items,
		page: { mode: "none" }
	};
	const effectiveOffset = offset ?? 0;
	const effectiveLimit = limit ?? DEFAULT_LIMIT;
	return {
		items: items.slice(effectiveOffset, effectiveOffset + effectiveLimit),
		page: createPage({
			total,
			limit: effectiveLimit,
			offset: effectiveOffset
		})
	};
}

//#region ../core/dist/sagas/constants.js
/**
* Binding-source tag used in `ListTasksResult.bindingSource` whenever
* `listTasks` resolved children via the Saga membership path instead of
* the default `parentId` filter.
*
* @deprecated Since T10638, saga membership is parent_id-based so this
*   binding source distinction is no longer meaningful.
*/
var LIST_BINDING_SAGA_GROUPS = "saga.groups";
//#endregion
//#region ../core/dist/tasks/list.js
var TASK_LIST_DEFAULT_LIMIT = 10;
/**
* List tasks with optional filtering and pagination.
*
* When `options.parentId` resolves to a Saga (`type='saga'`), children are
* resolved via canonical `parentId` containment. All other filters (`status`,
* `priority`, `type`, `phase`, `label`, `excludeArchived`) are applied to the
* resolved member set in-memory. The returned `bindingSource` field is set to
* `'saga.groups'` for legacy client compatibility only.
*
* @task T4460
* @task T9658 — Saga-aware --parent routing
*/
async function listTasks(options = {}, cwd, accessor) {
	const dataAccessor = accessor ?? await (await import('./data-accessor-CVbz8X_7.js').then(function (n) { return n.d; })).getTaskAccessor(cwd);
	let sagaMemberIds = null;
	if (options.parentId) sagaMemberIds = await resolveSagaMemberIds(dataAccessor, options.parentId);
	const queryFilters = { orderBy: options.sortByPriority ? "priority" : "position" };
	if (options.status) queryFilters.status = options.status;
	if (options.priority) queryFilters.priority = options.priority;
	if (options.type) queryFilters.type = options.type;
	if (options.parentId && sagaMemberIds === null) queryFilters.parentId = options.parentId;
	if (options.phase) queryFilters.phase = options.phase;
	if (options.label) queryFilters.label = options.label;
	if (options.excludeArchived && options.status !== "archived") queryFilters.excludeStatus = "archived";
	const queryResult = await dataAccessor.queryTasks(queryFilters);
	let filtered;
	let filteredCount;
	if (sagaMemberIds !== null) {
		const memberOrder = /* @__PURE__ */ new Map();
		for (let idx = 0; idx < sagaMemberIds.length; idx++) {
			const id = sagaMemberIds[idx];
			if (id !== void 0) memberOrder.set(id, idx);
		}
		const memberSet = new Set(sagaMemberIds);
		const sagaFiltered = queryResult.tasks.filter((t) => memberSet.has(t.id)).sort((a, b) => (memberOrder.get(a.id) ?? 0) - (memberOrder.get(b.id) ?? 0));
		filtered = sagaFiltered;
		filteredCount = sagaFiltered.length;
	} else {
		filtered = queryResult.tasks;
		filteredCount = queryResult.total;
	}
	const total = await dataAccessor.countTasks();
	const limit = options.limit === 0 ? void 0 : typeof options.limit === "number" && options.limit > 0 ? options.limit : TASK_LIST_DEFAULT_LIMIT;
	const offset = typeof options.offset === "number" && options.offset > 0 ? options.offset : void 0;
	const { items: tasks, page } = paginate(filtered, limit, offset);
	const pagination = page.mode === "offset" ? {
		limit: page.limit,
		offset: page.offset,
		hasMore: page.hasMore
	} : void 0;
	return {
		tasks: tasks.map((t) => ({
			...t,
			_next: taskListItemNext(t.id)
		})),
		total,
		filtered: filteredCount,
		page,
		pagination,
		...sagaMemberIds !== null ? { bindingSource: LIST_BINDING_SAGA_GROUPS } : {}
	};
}

export { listTasks as l };
//# sourceMappingURL=list-DfOyEStk.js.map
