import { ItemRenderer as MonacoSuggestItemRenderer } from 'monaco-editor/esm/vs/editor/contrib/suggest/browser/suggestWidgetRenderer.js';
const patches = [];
let installed = false;
function installRendererPatchIfNeeded() {
    if (installed) {
        return;
    }
    installed = true;
    const rendererPrototype = MonacoSuggestItemRenderer === null || MonacoSuggestItemRenderer === void 0 ? void 0 : MonacoSuggestItemRenderer.prototype;
    if (!rendererPrototype || rendererPrototype.__reactorSuggestRendererPatchesInstalled) {
        return;
    }
    const originalRenderElement = rendererPrototype.renderElement;
    rendererPrototype.renderElement = function (element, index, data) {
        originalRenderElement.call(this, element, index, data);
        for (const patch of patches) {
            try {
                patch({ element, index, data });
            }
            catch (error) {
                // Individual patch errors should never break Monaco suggest rendering.
            }
        }
    };
    rendererPrototype.__reactorSuggestRendererPatchesInstalled = true;
}
export function registerMonacoSuggestRendererPatch(patch) {
    patches.push(patch);
    installRendererPatchIfNeeded();
    return () => {
        const index = patches.indexOf(patch);
        if (index >= 0) {
            patches.splice(index, 1);
        }
    };
}
export function registerMonacoSuggestIconOverride(transform) {
    return registerMonacoSuggestRendererPatch(({ element, data }) => {
        const completion = element === null || element === void 0 ? void 0 : element.completion;
        const iconContainer = data === null || data === void 0 ? void 0 : data.iconContainer;
        if (!iconContainer) {
            return;
        }
        // Monaco reuses list row DOM nodes. Remove previously injected custom icon markup
        // so non-reactor items render with Monaco's default icon classes.
        if (iconContainer.childElementCount > 0) {
            iconContainer.innerHTML = '';
        }
        iconContainer.style.color = '';
        if (!completion) {
            return;
        }
        const iconOverride = transform(completion);
        if (!(iconOverride === null || iconOverride === void 0 ? void 0 : iconOverride.iconClass)) {
            return;
        }
        iconContainer.className = 'icon-label reactor-suggest-icon';
        iconContainer.style.display = 'inline-flex';
        iconContainer.style.alignItems = 'center';
        iconContainer.style.justifyContent = 'center';
        iconContainer.style.width = '16px';
        iconContainer.style.height = '16px';
        iconContainer.style.color = iconOverride.iconColor || '';
        iconContainer.innerHTML = `<i class="${iconOverride.iconClass}"></i>`;
    });
}
//# sourceMappingURL=monacoSuggestRendererPatches.js.map