import { parseJsonPointerPath } from "./json-pointer-path.js";
import * as monaco from "monaco-editor";
//#region src/v2/features/editor/helpers/json/json-pointer-links.ts
var JSON_POINTER_LINK_SCHEME = "scalar-json-pointer";
/**
* Checks if the quote at the given index in the text is an unescaped double quote.
* An unescaped quote is one not preceded by an odd number of backslashes.
*
* @param text - The text string to check within.
* @param index - The index of the character in text to check.
* @returns True if the quote is unescaped, false otherwise.
*/
var isUnescapedQuote = (text, index) => {
	if (text[index] !== "\"") return false;
	let backslashes = 0;
	for (let i = index - 1; i >= 0 && text[i] === "\\"; i--) backslashes++;
	return backslashes % 2 === 0;
};
/**
* Finds the bounds (indices) of the nearest quoted string around a given index on the same line.
* Only considers unescaped double quotes (").
* Returns the left (opening) and right (closing) quote indices if both are found, or null otherwise.
*
* @param line - The string line in which to search.
* @param index - The index around which to search for the quoted string.
* @returns An object with `left` and `right` indices, or null if not found.
*/
var findQuotedStringBounds = (line, index) => {
	let left = -1;
	for (let i = index; i >= 0; i--) if (isUnescapedQuote(line, i)) {
		left = i;
		break;
	}
	if (left === -1) return null;
	let right = -1;
	for (let i = Math.max(left + 1, index); i < line.length; i++) if (isUnescapedQuote(line, i)) {
		right = i;
		break;
	}
	if (right === -1 || right <= left) return null;
	return {
		left,
		right
	};
};
/**
* Enables clickable JSON Pointer links in Monaco JSON editor.
*
* - Scans for "#/" inside JSON string values, but only within properly quoted, unescaped strings.
* - When a valid JSON Pointer is found, highlights it (excluding quotes) as a clickable link.
* - Clicking the link invokes the provided `navigate` function with the decoded pointer.
*
* @param navigate Callback to execute on pointer link click.
*/
var ensureJsonPointerLinkSupport = (navigate) => {
	monaco.languages.registerLinkProvider("json", { provideLinks(textModel) {
		const links = [];
		const lineCount = textModel.getLineCount();
		for (let lineNumber = 1; lineNumber <= lineCount; lineNumber++) {
			const line = textModel.getLineContent(lineNumber);
			let fromIndex = 0;
			while (true) {
				const idx = line.indexOf("#/", fromIndex);
				if (idx === -1) break;
				const bounds = findQuotedStringBounds(line, idx);
				if (!bounds) {
					fromIndex = idx + 2;
					continue;
				}
				const value = line.slice(bounds.left + 1, bounds.right);
				if (!parseJsonPointerPath(value)) {
					fromIndex = bounds.right + 1;
					continue;
				}
				const range = new monaco.Range(lineNumber, bounds.left + 2, lineNumber, bounds.right + 1);
				links.push({
					range,
					url: monaco.Uri.parse(`${JSON_POINTER_LINK_SCHEME}:${encodeURIComponent(value)}`),
					tooltip: "Follow link"
				});
				fromIndex = bounds.right + 1;
			}
		}
		return { links };
	} });
	const linkOpener = monaco.editor.registerLinkOpener({ open(resource) {
		if (resource.scheme !== JSON_POINTER_LINK_SCHEME) return false;
		const encoded = (resource.path || "").replace(/^\//, "");
		const pointer = decodeURIComponent(encoded);
		if (!pointer) return false;
		return Promise.resolve(navigate(pointer)).then(() => true);
	} });
	const dispose = () => {
		linkOpener.dispose();
	};
	return {
		linkOpener,
		dispose
	};
};
//#endregion
export { ensureJsonPointerLinkSupport };

//# sourceMappingURL=json-pointer-links.js.map