//#region src/v2/features/command-palette/helpers/postman-request-tree.ts
var pathKey = (path) => JSON.stringify([...path]);
/**
* Inverse of {@link pathKey}: parses stable request index path JSON from the UI.
* Returns undefined for malformed input.
*/
var parsePostmanPathKey = (key) => {
	try {
		const parsed = JSON.parse(key);
		if (!Array.isArray(parsed) || parsed.length === 0) return;
		for (const n of parsed) if (typeof n !== "number" || !Number.isInteger(n) || n < 0) return;
		return parsed;
	} catch {
		return;
	}
};
/** Maps UI path keys to `ConvertOptions.requestIndexPaths`; invalid keys are skipped. */
var pathKeysToRequestIndexPaths = (keys) => {
	const out = [];
	for (const key of keys) {
		const path = parsePostmanPathKey(key);
		if (path !== void 0) out.push([...path]);
	}
	return out;
};
var isPostmanFolderNode = (node) => {
	if (node === null || node === void 0 || typeof node !== "object") return false;
	return "item" in node && Array.isArray(node.item);
};
/**
* Resolves the Postman JSON node at `path` (indices into root `item`, then each folder’s `item`).
* Matches {@link buildPostmanRequestTree} paths and postman-to-openapi `getNodeAtPath`.
*/
var getPostmanItemAtIndexPath = (itemRoot, path) => {
	if (!Array.isArray(itemRoot) || path.length === 0) return;
	let list = itemRoot;
	let node;
	for (let i = 0; i < path.length; i++) {
		const idx = path[i];
		if (typeof idx !== "number" || idx < 0 || idx >= list.length) return;
		node = list[idx];
		if (node === void 0) return;
		if (i < path.length - 1) {
			if (!isPostmanFolderNode(node)) return;
			list = node.item;
		}
	}
	return node;
};
var extractRequestMethod = (request) => {
	if (typeof request === "string") return "get".toLowerCase();
	if (request && typeof request === "object" && "method" in request) {
		const method = request.method;
		if (typeof method === "string" && method.length > 0) return method.toLowerCase();
	}
	return "get".toLowerCase();
};
/**
* Builds a shallow tree of folders and requests from a Postman `item` array.
* Paths are indices from the collection root through nested `item` arrays.
*/
var buildPostmanRequestTree = (items) => {
	if (!Array.isArray(items)) return [];
	const walk = (list, prefix) => list.map((raw, index) => {
		const path = [...prefix, index];
		const name = typeof raw?.name === "string" && raw.name.length > 0 ? raw.name : "Untitled";
		if (raw?.item !== void 0 && Array.isArray(raw.item)) return {
			path,
			name,
			isFolder: true,
			children: walk(raw.item, path)
		};
		if (raw?.request !== void 0) return {
			path,
			name,
			isFolder: false,
			method: extractRequestMethod(raw.request)
		};
		return {
			path,
			name,
			isFolder: true,
			children: []
		};
	});
	return walk(items, []);
};
/** Counts Postman request leaves in an `item` array (same rules as {@link buildPostmanRequestTree}). */
var countPostmanRequestLeaves = (items) => {
	if (!Array.isArray(items)) return 0;
	const walk = (list) => list.reduce((sum, raw) => {
		if (raw?.item !== void 0 && Array.isArray(raw.item)) return sum + walk(raw.item);
		if (raw?.request !== void 0) return sum + 1;
		return sum;
	}, 0);
	return walk(items);
};
/** Path keys for every request leaf in the tree (depth-first). */
var collectAllRequestPathKeys = (nodes) => {
	const keys = [];
	for (const node of nodes) if (node.isFolder) keys.push(...collectAllRequestPathKeys(node.children ?? []));
	else keys.push(pathKey(node.path));
	return keys;
};
var collectRequestPathKeysUnderFolder = (node) => {
	if (!node.isFolder) return [pathKey(node.path)];
	return collectAllRequestPathKeys(node.children ?? []);
};
/** Returns a new selection list after toggling one request path key. */
var applyPostmanRequestSelectionChange = (selectedKeys, key, selected) => {
	const next = new Set(selectedKeys);
	if (selected) next.add(key);
	else next.delete(key);
	return [...next];
};
/** Returns a new selection list after selecting or clearing every request under a folder. */
var applyPostmanFolderSelectionChange = (selectedKeys, node, selected) => {
	const keys = collectRequestPathKeysUnderFolder(node);
	const next = new Set(selectedKeys);
	for (const key of keys) if (selected) next.add(key);
	else next.delete(key);
	return [...next];
};
var folderFullySelected = (node, selected) => {
	const keys = collectRequestPathKeysUnderFolder(node);
	return keys.length > 0 && keys.every((key) => selected.has(key));
};
/** True when at least one descendant request is selected but not all. */
var folderHasPartialSelection = (node, selected) => {
	const keys = collectRequestPathKeysUnderFolder(node);
	if (keys.length === 0) return false;
	let selectedCount = 0;
	for (const key of keys) if (selected.has(key)) selectedCount++;
	return selectedCount > 0 && selectedCount < keys.length;
};
//#endregion
export { applyPostmanFolderSelectionChange, applyPostmanRequestSelectionChange, buildPostmanRequestTree, collectRequestPathKeysUnderFolder, countPostmanRequestLeaves, extractRequestMethod, folderFullySelected, folderHasPartialSelection, getPostmanItemAtIndexPath, pathKey, pathKeysToRequestIndexPaths };

//# sourceMappingURL=postman-request-tree.js.map