import { computed, ref, toValue } from "vue";
import Fuse from "fuse.js";
import { getResolvedRef } from "@scalar/workspace-store/helpers/get-resolved-ref";
//#region src/v2/features/search/helpers/create-fuse-instance.ts
/**
* Create a Fuse instance for searching the API reference.
*
* Doesn't have any data yet, so it's empty.
*/
function createFuseInstance() {
	return new Fuse([], {
		keys: [
			{
				name: "title",
				weight: .7
			},
			{
				name: "description",
				weight: .3
			},
			{
				name: "operationId",
				weight: .6
			},
			{
				name: "path",
				weight: .5
			},
			{
				name: "tag",
				weight: .4
			},
			{
				name: "method",
				weight: .3
			},
			{
				name: "documentName",
				weight: .3
			}
		],
		threshold: .3,
		distance: 100,
		includeScore: true,
		includeMatches: true,
		ignoreLocation: true,
		useExtendedSearch: true,
		findAllMatches: true
	});
}
//#endregion
//#region src/v2/features/search/helpers/create-search-index.ts
/**
* Create a search index from a list of entries.
*/
function createSearchIndex(documents) {
	const index = [];
	/**
	* Recursively processes entries and their children to build the search index.
	*/
	function processEntries(entriesToProcess, document) {
		entriesToProcess.forEach((entry) => {
			addEntryToIndex(entry, index, document);
			if ("children" in entry && entry.children) processEntries(entry.children, document);
		});
	}
	documents?.forEach((document) => processEntries(document["x-scalar-navigation"]?.children ?? [], document));
	return index;
}
/**
* Adds a single entry to the search index, handling all entry types recursively.
*/
function addEntryToIndex(entry, index, document) {
	if (entry.type === "operation") {
		const operation = getResolvedRef(document?.paths?.[entry.path]?.[entry.method]) ?? {};
		index.push({
			type: "operation",
			title: entry.title,
			id: entry.id,
			description: operation.description || "",
			method: entry.method,
			path: entry.path,
			operationId: operation.operationId,
			entry,
			documentName: document?.info.title ?? ""
		});
		return;
	}
	if (entry.type === "tag" && entry.isGroup === false) {
		index.push({
			id: entry.id,
			title: entry.title,
			description: entry.description || "",
			type: "tag",
			entry,
			documentName: document?.info.title ?? ""
		});
		return;
	}
	if (entry.type === "tag" && entry.isGroup === true) {
		index.push({
			id: entry.id,
			title: entry.title,
			description: "Tag Group",
			type: "tag",
			entry,
			documentName: document?.info.title ?? ""
		});
		return;
	}
	if (entry.type === "text") {
		index.push({
			id: entry.id,
			type: "heading",
			title: entry.title ?? "",
			description: "Heading",
			entry,
			documentName: document?.info.title ?? ""
		});
		return;
	}
}
//#endregion
//#region src/v2/features/search/hooks/use-search-index.ts
var MAX_SEARCH_RESULTS = 25;
/**
* Creates the search index from multiple OpenAPI documents
*/
var useSearchIndex = (documents) => {
	/** When the document changes we replace the search index */
	const fuse = computed(() => {
		const instance = createFuseInstance();
		instance.setCollection(createSearchIndex(toValue(documents)));
		return instance;
	});
	const query = ref("");
	return {
		results: computed(() => {
			if (query.value.length !== 0) return fuse.value.search(query.value, { limit: MAX_SEARCH_RESULTS }).flatMap((result) => {
				if (result.item.entry.type !== "operation") return [];
				return result.item.entry;
			});
			return null;
		}),
		query
	};
};
//#endregion
export { useSearchIndex as t };

//# sourceMappingURL=search-CmsQQkjn.js.map