import { generateLocationId } from "../../../helpers/generate-location-id.js";
import { computed, toValue, watch } from "vue";
import { createSidebarState, generateReverseIndex, getChildEntry } from "@scalar/sidebar";
import { getParentEntry } from "@scalar/workspace-store/navigation";
//#region src/v2/features/modal/hooks/use-modal-sidebar.ts
/**
* useSidebarState - Custom hook to manage the sidebar state and navigation logic in the Scalar API client
*
* This composable manages the sidebar structure, synchronizes selection state
* with the current route, and provides a handler for selecting sidebar items.
*
* Example usage:
*
* const { handleSelectItem, sidebarState } = useSidebarState({
*   workspaceStore,
*   workspaceSlug,
*   documentSlug,
*   path,
*   method,
*   exampleName,
* })
*/
var useModalSidebar = ({ workspaceStore, documentSlug, path, method, exampleName, route }) => {
	const entries = computed(() => workspaceStore?.workspace.documents[toValue(documentSlug) ?? ""]?.["x-scalar-navigation"]?.children ?? []);
	const state = createSidebarState(entries);
	/**
	* Computed index for fast lookup of sidebar nodes by their unique API location.
	*
	* - Only indexes nodes of type 'operation', or 'example'.
	* - The lookup key is a serialized array of: [operationPath, operationMethod, exampleName?].
	* - Supports precise resolution of sidebar entries given an API "location".
	*/
	const locationIndex = computed(() => generateReverseIndex({
		items: entries.value,
		nestedKey: "children",
		filter: (node) => node.type === "operation" || node.type === "example",
		getId: (node) => {
			const operation = getParentEntry("operation", node);
			return generateLocationId({
				document: toValue(documentSlug) ?? "",
				path: operation?.path,
				method: operation?.method,
				example: node.type === "example" ? node.name : void 0
			});
		}
	}));
	/**
	* Looks up a sidebar entry by its unique API location.
	* - First tries to find an entry matching all provided properties (including example).
	* - If not found, falls back to matching only the operation (ignores example field).
	* This allows resolving either examples, operations, or documents as appropriate.
	*
	* @param location - Object specifying the document name, path, method, and optional example name.
	* @returns The matching sidebar entry, or undefined if none found.
	*
	* Example:
	*   const entry = getEntryByLocation({
	*     document: 'pets',
	*     path: '/pets',
	*     method: 'get',
	*     example: 'default',
	*   })
	*/
	const getEntryByLocation = (location) => {
		const entryWithExample = locationIndex.value.get(generateLocationId({
			document: location.document,
			path: location.path,
			method: location.method,
			example: location.example
		}));
		if (entryWithExample) return entryWithExample;
		return locationIndex.value.get(generateLocationId({
			document: location.document,
			path: location.path,
			method: location.method
		}));
	};
	/**
	* Handles item selection from the sidebar and routes navigation accordingly.
	*
	* Example:
	*   handleSelectItem('id-of-entry')
	*/
	const handleSelectItem = (id) => {
		const entry = state.getEntryById(id);
		if (!entry) {
			console.warn(`Could not find sidebar entry with id ${id} to select`);
			return;
		}
		if (entry.type === "operation" || entry.type === "example") {
			if (state.isSelected(id)) {
				state.setExpanded(id, !state.isExpanded(id));
				return;
			}
			const operation = getParentEntry("operation", entry);
			const example = getChildEntry("example", entry);
			if (example) {
				state.setSelected(example.id);
				state.setExpanded(example.id, true);
			} else state.setSelected(id);
			if (!operation) return;
			return route({
				documentSlug: toValue(documentSlug),
				path: operation.path,
				method: operation.method,
				example: example?.name ?? "default"
			});
		}
		state.setExpanded(id, !state.isExpanded(id));
	};
	/** Keep the sidebar state in sync with the modal parameters */
	watch([
		documentSlug,
		path,
		method,
		exampleName
	], ([newDocument, newPath, newMethod, newExample]) => {
		if (!newDocument) {
			state.setSelected(null);
			return;
		}
		const entry = getEntryByLocation({
			document: newDocument,
			path: newPath,
			method: newMethod,
			example: newExample
		});
		if (entry) {
			state.setSelected(entry.id);
			state.setExpanded(entry.id, true);
		}
	}, { immediate: true });
	return {
		handleSelectItem,
		state,
		getEntryByLocation
	};
};
//#endregion
export { useModalSidebar };

//# sourceMappingURL=use-modal-sidebar.js.map