import { PathId } from "../../routes.js";
import { useActiveEntities } from "../../store/active-entities.js";
import { useWorkspace } from "../../store/store.js";
import { computed, nextTick, ref, watch } from "vue";
import { useRouter } from "vue-router";
import { isDefined, shouldIgnoreEntity } from "@scalar/oas-utils/helpers";
import Fuse from "fuse.js";
//#region src/components/Search/useSearch.ts
/**
* Hook for managing search functionality.
* Provides search state, results, and methods for searching.
*/
function useSearch() {
	const router = useRouter();
	const { activeWorkspace, activeWorkspaceRequests, activeWorkspaceCollections } = useActiveEntities();
	const { requests, tags } = useWorkspace();
	const fuseDataArray = ref([]);
	const searchResults = ref([]);
	const selectedSearchResult = ref(0);
	const searchText = ref("");
	const searchInputRef = ref(null);
	const searchResultRefs = ref([]);
	const fuse = new Fuse(fuseDataArray.value, { keys: [
		"title",
		"description",
		"body"
	] });
	const resetSearch = () => {
		searchText.value = "";
		selectedSearchResult.value = 0;
		searchResults.value = [];
		if (searchInputRef.value instanceof HTMLInputElement) searchInputRef.value.blur();
	};
	const populateFuseDataArray = (items) => {
		fuseDataArray.value = items.filter((request) => !shouldIgnoreEntity(request)).filter((request) => {
			const collection = activeWorkspaceCollections.value?.find((activeWorkspaceCollection) => activeWorkspaceCollection.requests.includes(request.uid));
			return !Boolean(collection?.tags.map((uid) => tags[uid]).filter(isDefined).filter((tag) => request.tags?.includes(tag.name)).filter((tag) => shouldIgnoreEntity(tag)).length);
		}).map((request) => ({
			id: request.uid,
			title: request.summary ?? request.method,
			description: request.description ?? "",
			httpVerb: request.method,
			path: request.path,
			link: router?.resolve({
				name: "request",
				params: {
					[PathId.Request]: request.uid,
					[PathId.Workspace]: activeWorkspace.value?.uid
				}
			})?.href
		}));
		fuse.setCollection(fuseDataArray.value);
	};
	const fuseSearch = () => {
		selectedSearchResult.value = 0;
		searchResults.value = fuse.search(searchText.value);
	};
	watch(searchText, (newValue) => {
		if (newValue.length) fuseSearch();
		else searchResults.value = [];
	});
	const navigateSearchResults = (direction) => {
		const offset = direction === "up" ? -1 : 1;
		const length = searchResultsWithPlaceholderResults.value.length;
		selectedSearchResult.value = (selectedSearchResult.value + offset + length) % length;
		nextTick(() => {
			const element = searchResultRefs.value[selectedSearchResult.value];
			if (element instanceof HTMLElement) element.scrollIntoView({
				behavior: "smooth",
				block: "center"
			});
		});
	};
	const selectSearchResult = () => {
		if (selectedSearchResult.value >= 0) {
			const selectedResult = searchResultsWithPlaceholderResults.value[selectedSearchResult.value];
			if (selectedResult) onSearchResultClick(selectedResult);
		}
	};
	const validRequests = computed(() => activeWorkspaceRequests.value.map((uid) => requests[uid]).filter(isDefined));
	watch(activeWorkspaceRequests, () => {
		populateFuseDataArray(validRequests.value);
	}, { immediate: true });
	const onSearchResultClick = (entry) => {
		router.push(entry.item.id);
		resetSearch();
	};
	const searchResultsWithPlaceholderResults = computed(() => {
		if (searchText.value.length === 0) return fuseDataArray.value.map((item) => {
			return { item };
		});
		return searchResults.value;
	});
	return {
		searchText,
		searchResultsWithPlaceholderResults,
		selectedSearchResult,
		onSearchResultClick,
		fuseSearch,
		searchInputRef,
		searchResultRefs,
		navigateSearchResults,
		selectSearchResult,
		populateFuseDataArray
	};
}
//#endregion
export { useSearch };

//# sourceMappingURL=useSearch.js.map