
import { raceTimeout } from '../../../../base/common/async.js';
import { CancellationToken } from '../../../../base/common/cancellation.js';
import { localize } from '../../../../nls.js';
import { RawContextKey } from '../../../../platform/contextkey/common/contextkey.js';

const CONTEXT_PREFERENCES_SEARCH_FOCUS = ( new RawContextKey("inPreferencesSearch", false));
const SETTINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS = "settings.action.clearSearchResults";
const SETTINGS_EDITOR_COMMAND_SHOW_AI_RESULTS = "settings.action.showAIResults";
const SETTINGS_EDITOR_COMMAND_TOGGLE_AI_SEARCH = "settings.action.toggleAiSearch";
const SETTINGS_EDITOR_COMMAND_SHOW_CONTEXT_MENU = "settings.action.showContextMenu";
const SETTINGS_EDITOR_COMMAND_SUGGEST_FILTERS = "settings.action.suggestFilters";
const CONTEXT_SETTINGS_EDITOR = ( new RawContextKey("inSettingsEditor", false));
const CONTEXT_SETTINGS_JSON_EDITOR = ( new RawContextKey("inSettingsJSONEditor", false));
const CONTEXT_SETTINGS_SEARCH_FOCUS = ( new RawContextKey("inSettingsSearch", false));
const CONTEXT_TOC_ROW_FOCUS = ( new RawContextKey("settingsTocRowFocus", false));
const CONTEXT_SETTINGS_ROW_FOCUS = ( new RawContextKey("settingRowFocus", false));
const CONTEXT_KEYBINDINGS_EDITOR = ( new RawContextKey("inKeybindings", false));
const CONTEXT_KEYBINDINGS_SEARCH_FOCUS = ( new RawContextKey("inKeybindingsSearch", false));
const CONTEXT_KEYBINDINGS_SEARCH_HAS_VALUE = ( new RawContextKey("keybindingsSearchHasValue", false));
const CONTEXT_KEYBINDING_FOCUS = ( new RawContextKey("keybindingFocus", false));
const CONTEXT_WHEN_FOCUS = ( new RawContextKey("whenFocus", false));
const CONTEXT_AI_SETTING_RESULTS_AVAILABLE = ( new RawContextKey("aiSettingResultsAvailable", false));
const KEYBINDINGS_EDITOR_COMMAND_SEARCH = "keybindings.editor.searchKeybindings";
const KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS = "keybindings.editor.clearSearchResults";
const KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_HISTORY = "keybindings.editor.clearSearchHistory";
const KEYBINDINGS_EDITOR_COMMAND_RECORD_SEARCH_KEYS = "keybindings.editor.recordSearchKeys";
const KEYBINDINGS_EDITOR_COMMAND_SORTBY_PRECEDENCE = "keybindings.editor.toggleSortByPrecedence";
const KEYBINDINGS_EDITOR_COMMAND_DEFINE = "keybindings.editor.defineKeybinding";
const KEYBINDINGS_EDITOR_COMMAND_ADD = "keybindings.editor.addKeybinding";
const KEYBINDINGS_EDITOR_COMMAND_DEFINE_WHEN = "keybindings.editor.defineWhenExpression";
const KEYBINDINGS_EDITOR_COMMAND_ACCEPT_WHEN = "keybindings.editor.acceptWhenExpression";
const KEYBINDINGS_EDITOR_COMMAND_REJECT_WHEN = "keybindings.editor.rejectWhenExpression";
const KEYBINDINGS_EDITOR_COMMAND_REMOVE = "keybindings.editor.removeKeybinding";
const KEYBINDINGS_EDITOR_COMMAND_RESET = "keybindings.editor.resetKeybinding";
const KEYBINDINGS_EDITOR_COMMAND_COPY = "keybindings.editor.copyKeybindingEntry";
const KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND = "keybindings.editor.copyCommandKeybindingEntry";
const KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND_TITLE = "keybindings.editor.copyCommandTitle";
const KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR = "keybindings.editor.showConflicts";
const KEYBINDINGS_EDITOR_COMMAND_FOCUS_KEYBINDINGS = "keybindings.editor.focusKeybindings";
const KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS = "keybindings.editor.showDefaultKeybindings";
const KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS = "keybindings.editor.showUserKeybindings";
const KEYBINDINGS_EDITOR_SHOW_EXTENSION_KEYBINDINGS = "keybindings.editor.showExtensionKeybindings";
const MODIFIED_SETTING_TAG = "modified";
const EXTENSION_SETTING_TAG = "ext:";
const FEATURE_SETTING_TAG = "feature:";
const ID_SETTING_TAG = "id:";
const LANGUAGE_SETTING_TAG = "lang:";
const GENERAL_TAG_SETTING_TAG = "tag:";
const POLICY_SETTING_TAG = "hasPolicy";
const AGENTS_WINDOW_SETTING_TAG = "override:agentsWindow";
const WORKSPACE_TRUST_SETTING_TAG = "workspaceTrust";
const REQUIRE_TRUSTED_WORKSPACE_SETTING_TAG = "requireTrustedWorkspace";
const ADVANCED_SETTING_TAG = "advanced";
const KEYBOARD_LAYOUT_OPEN_PICKER = "workbench.action.openKeyboardLayoutPicker";
const EXTENSION_FETCH_TIMEOUT_MS = 1000;
const STRING_MATCH_SEARCH_PROVIDER_NAME = "local";
const TF_IDF_SEARCH_PROVIDER_NAME = "tfIdf";
const FILTER_MODEL_SEARCH_PROVIDER_NAME = "filterModel";
const EMBEDDINGS_SEARCH_PROVIDER_NAME = "embeddingsFull";
const LLM_RANKED_SEARCH_PROVIDER_NAME = "llmRanked";
var WorkbenchSettingsEditorSettings;
(function(WorkbenchSettingsEditorSettings) {
    WorkbenchSettingsEditorSettings["ShowAISearchToggle"] = "workbench.settings.showAISearchToggle";
    WorkbenchSettingsEditorSettings["EnableNaturalLanguageSearch"] = "workbench.settings.enableNaturalLanguageSearch";
})(WorkbenchSettingsEditorSettings || (WorkbenchSettingsEditorSettings = {}));
let cachedExtensionToggleData;
async function getExperimentalExtensionToggleData(chatEntitlementService, extensionGalleryService, productService) {
    if (!extensionGalleryService.isEnabled()) {
        return undefined;
    }
    if (chatEntitlementService.sentiment.hidden || chatEntitlementService.sentiment.disabled) {
        return undefined;
    }
    if (cachedExtensionToggleData) {
        return cachedExtensionToggleData;
    }
    if (productService.extensionRecommendations) {
        const settingsEditorRecommendedExtensions = {};
        ( Object.keys(productService.extensionRecommendations)).forEach(extensionId => {
            const extensionInfo = productService.extensionRecommendations[extensionId];
            if (extensionInfo.onSettingsEditorOpen) {
                settingsEditorRecommendedExtensions[extensionId] = extensionInfo;
            }
        });
        const recommendedExtensionsGalleryInfo = {};
        for (const key in settingsEditorRecommendedExtensions) {
            const extensionId = key;
            const isStable = productService.quality === "stable";
            try {
                const extensions = await raceTimeout(extensionGalleryService.getExtensions([{
                    id: extensionId,
                    preRelease: !isStable
                }], CancellationToken.None), EXTENSION_FETCH_TIMEOUT_MS);
                if (extensions?.length === 1) {
                    recommendedExtensionsGalleryInfo[key] = extensions[0];
                } else {
                    return undefined;
                }
            } catch (e) {
                return undefined;
            }
        }
        cachedExtensionToggleData = {
            settingsEditorRecommendedExtensions,
            recommendedExtensionsGalleryInfo
        };
        return cachedExtensionToggleData;
    }
    return undefined;
}
function compareTwoNullableNumbers(a, b) {
    const aOrMax = a ?? Number.MAX_SAFE_INTEGER;
    const bOrMax = b ?? Number.MAX_SAFE_INTEGER;
    if (aOrMax < bOrMax) {
        return -1;
    } else if (aOrMax > bOrMax) {
        return 1;
    } else {
        return 0;
    }
}
const PREVIEW_INDICATOR_DESCRIPTION = ( localize(
    12697,
    "Preview setting: this setting controls a new feature that is still under refinement yet ready to use. Feedback is welcome."
));
const EXPERIMENTAL_INDICATOR_DESCRIPTION = ( localize(
    12698,
    "Experimental setting: this setting controls a new feature that is actively being developed and may be unstable. It is subject to change or removal."
));
const ADVANCED_INDICATOR_DESCRIPTION = ( localize(
    12699,
    "Advanced setting: this setting is intended for advanced scenarios and configurations. Only modify this if you know what it does."
));
const knownAcronyms = ( new Set());
[
    "css",
    "html",
    "scss",
    "less",
    "json",
    "js",
    "ts",
    "ie",
    "id",
    "php",
    "scm"
].forEach(str => knownAcronyms.add(str));
const knownTermMappings = ( new Map());
knownTermMappings.set("power shell", "PowerShell");
knownTermMappings.set("powershell", "PowerShell");
knownTermMappings.set("javascript", "JavaScript");
knownTermMappings.set("typescript", "TypeScript");
knownTermMappings.set("github", "GitHub");
knownTermMappings.set("jet brains", "JetBrains");
knownTermMappings.set("jetbrains", "JetBrains");
knownTermMappings.set("re sharper", "ReSharper");
knownTermMappings.set("resharper", "ReSharper");
function wordifyKey(key) {
    key =
    key.replace(/\.([a-z0-9])/g, (_, p1) => ` \u203A ${p1.toUpperCase()}`).replace(/([a-z0-9])([A-Z])/g, "$1 $2").replace(/([A-Z]{1,})([A-Z][a-z])/g, "$1 $2").replace(/^[a-z]/g, match => match.toUpperCase()).replace(/\b\w+\b/g, match => {
        return ( knownAcronyms.has(match.toLowerCase())) ? match.toUpperCase() : match;
    });
    for (const [k, v] of knownTermMappings) {
        key = key.replace(( new RegExp(`\\b${k}\\b`, "gi")), v);
    }
    return key;
}

export { ADVANCED_INDICATOR_DESCRIPTION, ADVANCED_SETTING_TAG, AGENTS_WINDOW_SETTING_TAG, CONTEXT_AI_SETTING_RESULTS_AVAILABLE, CONTEXT_KEYBINDINGS_EDITOR, CONTEXT_KEYBINDINGS_SEARCH_FOCUS, CONTEXT_KEYBINDINGS_SEARCH_HAS_VALUE, CONTEXT_KEYBINDING_FOCUS, CONTEXT_PREFERENCES_SEARCH_FOCUS, CONTEXT_SETTINGS_EDITOR, CONTEXT_SETTINGS_JSON_EDITOR, CONTEXT_SETTINGS_ROW_FOCUS, CONTEXT_SETTINGS_SEARCH_FOCUS, CONTEXT_TOC_ROW_FOCUS, CONTEXT_WHEN_FOCUS, EMBEDDINGS_SEARCH_PROVIDER_NAME, EXPERIMENTAL_INDICATOR_DESCRIPTION, EXTENSION_FETCH_TIMEOUT_MS, EXTENSION_SETTING_TAG, FEATURE_SETTING_TAG, FILTER_MODEL_SEARCH_PROVIDER_NAME, GENERAL_TAG_SETTING_TAG, ID_SETTING_TAG, KEYBINDINGS_EDITOR_COMMAND_ACCEPT_WHEN, KEYBINDINGS_EDITOR_COMMAND_ADD, KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_HISTORY, KEYBINDINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS, KEYBINDINGS_EDITOR_COMMAND_COPY, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND, KEYBINDINGS_EDITOR_COMMAND_COPY_COMMAND_TITLE, KEYBINDINGS_EDITOR_COMMAND_DEFINE, KEYBINDINGS_EDITOR_COMMAND_DEFINE_WHEN, KEYBINDINGS_EDITOR_COMMAND_FOCUS_KEYBINDINGS, KEYBINDINGS_EDITOR_COMMAND_RECORD_SEARCH_KEYS, KEYBINDINGS_EDITOR_COMMAND_REJECT_WHEN, KEYBINDINGS_EDITOR_COMMAND_REMOVE, KEYBINDINGS_EDITOR_COMMAND_RESET, KEYBINDINGS_EDITOR_COMMAND_SEARCH, KEYBINDINGS_EDITOR_COMMAND_SHOW_SIMILAR, KEYBINDINGS_EDITOR_COMMAND_SORTBY_PRECEDENCE, KEYBINDINGS_EDITOR_SHOW_DEFAULT_KEYBINDINGS, KEYBINDINGS_EDITOR_SHOW_EXTENSION_KEYBINDINGS, KEYBINDINGS_EDITOR_SHOW_USER_KEYBINDINGS, KEYBOARD_LAYOUT_OPEN_PICKER, LANGUAGE_SETTING_TAG, LLM_RANKED_SEARCH_PROVIDER_NAME, MODIFIED_SETTING_TAG, POLICY_SETTING_TAG, PREVIEW_INDICATOR_DESCRIPTION, REQUIRE_TRUSTED_WORKSPACE_SETTING_TAG, SETTINGS_EDITOR_COMMAND_CLEAR_SEARCH_RESULTS, SETTINGS_EDITOR_COMMAND_SHOW_AI_RESULTS, SETTINGS_EDITOR_COMMAND_SHOW_CONTEXT_MENU, SETTINGS_EDITOR_COMMAND_SUGGEST_FILTERS, SETTINGS_EDITOR_COMMAND_TOGGLE_AI_SEARCH, STRING_MATCH_SEARCH_PROVIDER_NAME, TF_IDF_SEARCH_PROVIDER_NAME, WORKSPACE_TRUST_SETTING_TAG, WorkbenchSettingsEditorSettings, compareTwoNullableNumbers, getExperimentalExtensionToggleData, knownAcronyms, knownTermMappings, wordifyKey };
