import "../../../../dialect-helpers-DRI5pyY3.mjs";
import "../../../../base64-CmWvODNW.mjs";
import "../../../../types-CuW0zWDA.mjs";
import { t as FTSManager } from "../../../../fts-manager-BbFaF_V8.mjs";
import "../../../../loader-CB6YHeul.mjs";
import { n as apiSuccess, r as handleError, t as apiError } from "../../../../error-CkAseSqZ.mjs";
import { n as parseBody, t as isParseError } from "../../../../parse-GmjecGct.mjs";
import { A as searchRebuildBody } from "../../../../redirects-DXEXBAII.mjs";
import "../../../../byline-fields-2Lp5Hbgf.mjs";
import "../../../../status-CqJQX1QN.mjs";
import "../../../../api/schemas/index.mjs";
import "../../../../search-DZN2HZ-V.mjs";
import { n as requirePerm } from "../../../../authorize-Dz88F_Fw.mjs";

//#region src/astro/routes/api/search/rebuild.ts
const prerender = false;
/**
* Rebuild the search index for a collection
*
* Body:
* - collection: Collection slug to rebuild (required)
*/
const POST = async ({ request, locals }) => {
	const { emdash, user } = locals;
	if (!emdash?.db) return apiError("NOT_CONFIGURED", "EmDash not configured", 500);
	const denied = requirePerm(user, "search:manage");
	if (denied) return denied;
	const body = await parseBody(request, searchRebuildBody);
	if (isParseError(body)) return body;
	const ftsManager = new FTSManager(emdash.db);
	try {
		const config = await ftsManager.getSearchConfig(body.collection);
		if (!config?.enabled) return apiError("SEARCH_NOT_ENABLED", `Search is not enabled for collection "${body.collection}"`, 400);
		const searchableFields = await ftsManager.getSearchableFields(body.collection);
		if (searchableFields.length === 0) return apiError("NO_SEARCHABLE_FIELDS", `No searchable fields defined for collection "${body.collection}"`, 400);
		await ftsManager.rebuildIndex(body.collection, searchableFields, config.weights);
		const stats = await ftsManager.getIndexStats(body.collection);
		return apiSuccess({
			collection: body.collection,
			indexed: stats?.indexed ?? 0
		});
	} catch (error) {
		return handleError(error, "Failed to rebuild index", "REBUILD_ERROR");
	}
};

//#endregion
export { POST, prerender };
//# sourceMappingURL=rebuild.mjs.map