import { g as getTaskAccessor } from './data-accessor-CVbz8X_7.js';
import { H as findTasks } from './tasks-CnwUAUXf.js';
import { json } from '@sveltejs/kit';
import './paths-2M_UxD_H.js';
import 'node:fs';
import 'node:path';
import 'node:os';
import 'node:events';
import 'node:diagnostics_channel';
import 'fs';
import 'events';
import 'util';
import 'path';
import 'assert';
import 'worker_threads';
import 'module';
import 'node:url';
import 'url';
import 'buffer';
import 'node:module';
import 'node:child_process';
import 'node:crypto';
import 'node:process';
import 'node:async_hooks';
import './conduit-Ca_OFWR_.js';
import './indexes-aYlULWG3.js';
import './utils-BvHPJOVk.js';
import './unique-constraint-BM-Apx1H.js';
import './chunk-DDP5xfzh.js';
import './sqlite-Dsbe0wec.js';
import './dual-scope-db-ATrwq0LH.js';
import './memory-schema-Bi7cChwo.js';
import './agent-registry-BtE5uhMO.js';
import './nexus-sqlite2-DgTuv9BQ.js';
import './nexus-schema2-BEd-ol6e.js';
import './logger-ChCwlV5k.js';
import 'node:sqlite';
import './lock-BoHF25mx.js';
import './index-IVoIz4D5.js';
import 'constants';
import 'stream';
import 'node:fs/promises';
import 'jsonc-parser';
import '@cleocode/cant';
import 'node:util';
import 'node:buffer';
import 'child_process';
import './branch-lock-qP1EkZ7p.js';
import './src2-DK8DKufK.js';
import 'llmtxt/identity';
import './sessions-j73dkbCI.js';
import './registry2-BSyCTA3q.js';
import './index-Ca4C2M45.js';
import './mvi-helpers-BNsJ8ZpQ.js';
import './session-store-OwQWMSf_.js';
import './session-manifest-ZHrj54LR.js';
import './storage-B9UA3BHR.js';
import './config-B0nEjgSh.js';
import './json-DXl7iDYg.js';
import './data-safety-central-vmfu_Y4J.js';
import './sqlite-data-accessor-C59wZ0zp.js';
import './cross-db-cleanup-C_FLyd-6.js';

//#region src/lib/tasks/search.ts
/**
* Normalizes a raw search string entered by the user.
*
* Rules:
* - Empty / whitespace-only → `{ kind: 'empty' }`
* - Matches `T###`, `t###`, or bare `###` (digits only) → `{ kind: 'id', id: 'T###' }`
* - Anything else → `{ kind: 'title', query: trimmed }` for fuzzy title search
*
* @param raw - The raw string value from the search input.
* @returns A discriminated union describing the normalized search intent.
*/
function normalizeSearch(raw) {
	const trimmed = raw.trim();
	if (trimmed.length === 0) return { kind: "empty" };
	const idMatch = /^[Tt]?(\d+)$/.exec(trimmed);
	if (idMatch) return {
		kind: "id",
		id: `T${idMatch[1]}`
	};
	return {
		kind: "title",
		query: trimmed
	};
}

//#region src/routes/api/tasks/search/+server.ts
/**
* GET /api/tasks/search — search tasks by ID or partial title.
*
* Query params:
*   q — raw search string (e.g. "T663", "t663", "663", "council")
*
* Response:
*   { kind: 'id', task: SearchTaskRow | null }              — exact ID lookup
*   { kind: 'title', tasks: SearchTaskRow[], total: number } — fuzzy title filter
*   { kind: 'empty' }                                       — no query provided
*
* The normalisation logic (ID vs. title) is centralised in
* $lib/tasks/search.ts so the same rules apply everywhere.
*
* T9617 refactor: zero raw SQL — delegates to `findTasks` from
* `@cleocode/core/tasks`. The response preserves the pre-T9617 snake_case
* field contract so existing consumers of this endpoint are unaffected.
*
* @task T9617
*/
var GET = async ({ locals, url }) => {
	const ctx = locals.projectCtx;
	if (!ctx.tasksDbExists) return json({ error: "tasks.db unavailable" }, { status: 503 });
	const normalized = normalizeSearch(url.searchParams.get("q") ?? "");
	if (normalized.kind === "empty") return json({ kind: "empty" });
	try {
		const accessor = await getTaskAccessor(ctx.projectPath);
		if (normalized.kind === "id") {
			const result = await findTasks({
				id: normalized.id,
				limit: 50
			}, ctx.projectPath, accessor);
			const match = result.results.find((r) => r.id.toUpperCase() === normalized.id.toUpperCase()) ?? result.results[0] ?? null;
			return json({
				kind: "id",
				task: match ? {
					id: match.id,
					title: match.title,
					status: match.status,
					priority: match.priority,
					type: match.type ?? "task",
					parent_id: match.parentId ?? null,
					pipeline_stage: null,
					updated_at: (/* @__PURE__ */ new Date()).toISOString()
				} : null
			});
		}
		const tasks = (await findTasks({
			query: normalized.query,
			limit: 50
		}, ctx.projectPath, accessor)).results.map((r) => ({
			id: r.id,
			title: r.title,
			status: r.status,
			priority: r.priority,
			type: r.type ?? "task",
			parent_id: r.parentId ?? null,
			pipeline_stage: null,
			updated_at: (/* @__PURE__ */ new Date()).toISOString()
		}));
		return json({
			kind: "title",
			tasks,
			total: tasks.length
		});
	} catch (err) {
		return json({ error: String(err) }, { status: 500 });
	}
};

export { GET };
//# sourceMappingURL=_server.ts-C8vCaxSl.js.map
