
import { __decorate, __param } from '../../../../../../../external/tslib/tslib.es6.js';
import { localize } from '../../../../nls.js';
import { append, $, trackFocus } from '../../../../base/browser/dom.js';
import { ActionBar } from '../../../../base/browser/ui/actionbar/actionbar.js';
import { Button } from '../../../../base/browser/ui/button/button.js';
import { InputBox } from '../../../../base/browser/ui/inputbox/inputBox.js';
import { Widget } from '../../../../base/browser/ui/widget.js';
import { Action } from '../../../../base/common/actions.js';
import { Delayer, disposableTimeout } from '../../../../base/common/async.js';
import { Emitter } from '../../../../base/common/event.js';
import { KeyMod as KeyMod$1, KeyCode } from '../../../../base/common/keyCodes.js';
import { CONTEXT_FIND_WIDGET_NOT_VISIBLE } from '../../../../editor/contrib/find/browser/findModel.js';
import { IClipboardService } from '../../../../platform/clipboard/common/clipboardService.service.js';
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.service.js';
import { ContextKeyExpr } from '../../../../platform/contextkey/common/contextkey.js';
import { IContextKeyService } from '../../../../platform/contextkey/common/contextkey.service.js';
import { IContextViewService, IContextMenuService } from '../../../../platform/contextview/browser/contextView.service.js';
import { IKeybindingService } from '../../../../platform/keybinding/common/keybinding.service.js';
import { KeybindingsRegistry, KeybindingWeight } from '../../../../platform/keybinding/common/keybindingsRegistry.js';
import { ThemeIcon } from '../../../../base/common/themables.js';
import { ContextScopedReplaceInput } from '../../../../platform/history/browser/contextScopedHistoryWidget.js';
import { isSearchViewFocused, getSearchView } from './searchActionsBase.js';
import { SearchContext, SearchCommandIds } from '../common/constants.js';
import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.service.js';
import { isMacintosh } from '../../../../base/common/platform.js';
import { Toggle } from '../../../../base/browser/ui/toggle/toggle.js';
import { IViewsService } from '../../../services/views/common/viewsService.service.js';
import { searchReplaceAllIcon, searchHideReplaceIcon, searchShowContextIcon, searchShowReplaceIcon } from './searchIcons.js';
import { ToggleSearchEditorContextLinesCommandId } from '../../searchEditor/browser/constants.js';
import { showHistoryKeybindingHint } from '../../../../platform/history/browser/historyWidgetKeybindingHint.js';
import { defaultToggleStyles, defaultInputBoxStyles } from '../../../../platform/theme/browser/defaultStyles.js';
import { NotebookFindFilters } from '../../notebook/browser/contrib/find/findFilters.js';
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
import { IEditorService } from '../../../services/editor/common/editorService.service.js';
import { NotebookEditorInput } from '../../notebook/common/notebookEditorInput.js';
import { GroupModelChangeKind } from '../../../common/editor.js';
import { SearchFindInput } from './searchFindInput.js';
import { getDefaultHoverDelegate } from '../../../../base/browser/ui/hover/hoverDelegateFactory.js';
import { MutableDisposable } from '../../../../base/common/lifecycle.js';
import { NotebookFindScopeType } from '../../notebook/common/notebookCommon.js';

var SearchWidget_1;
const SingleLineInputHeight = 26;
class ReplaceAllAction extends Action {
    static {
        this.ID = "search.action.replaceAll";
    }
    constructor(_searchWidget) {
        super(
            ReplaceAllAction.ID,
            "",
            ThemeIcon.asClassName(searchReplaceAllIcon),
            false
        );
        this._searchWidget = _searchWidget;
    }
    set searchWidget(searchWidget) {
        this._searchWidget = searchWidget;
    }
    run() {
        if (this._searchWidget) {
            return this._searchWidget.triggerReplaceAll();
        }
        return Promise.resolve();
    }
}
const hoverLifecycleOptions = {
    groupId: "search-widget"
};
const ctrlKeyMod = (isMacintosh ? KeyMod$1.WinCtrl : KeyMod$1.CtrlCmd);
function stopPropagationForMultiLineUpwards(event, value, textarea) {
    const isMultiline = !!value.match(/\n/);
    if (textarea && (isMultiline || textarea.clientHeight > SingleLineInputHeight) && textarea.selectionStart > 0) {
        event.stopPropagation();
        return;
    }
}
function stopPropagationForMultiLineDownwards(event, value, textarea) {
    const isMultiline = !!value.match(/\n/);
    if (textarea && (isMultiline || textarea.clientHeight > SingleLineInputHeight) && textarea.selectionEnd < textarea.value.length) {
        event.stopPropagation();
        return;
    }
}
let SearchWidget = class SearchWidget extends Widget {
    static {
        SearchWidget_1 = this;
    }
    static {
        this.INPUT_MAX_HEIGHT = 134;
    }
    static {
        this.REPLACE_ALL_DISABLED_LABEL = ( localize(13491, "Replace All (Submit Search to Enable)"));
    }
    static {
        this.REPLACE_ALL_ENABLED_LABEL = keyBindingService2 => {
            return keyBindingService2.appendKeybinding(( localize(13492, "Replace All")), ReplaceAllAction.ID);
        };
    }
    constructor(
        container,
        options,
        contextViewService,
        contextKeyService,
        keybindingService,
        clipboardServce,
        configurationService,
        accessibilityService,
        contextMenuService,
        instantiationService,
        editorService
    ) {
        super();
        this.contextViewService = contextViewService;
        this.contextKeyService = contextKeyService;
        this.keybindingService = keybindingService;
        this.clipboardServce = clipboardServce;
        this.configurationService = configurationService;
        this.accessibilityService = accessibilityService;
        this.contextMenuService = contextMenuService;
        this.instantiationService = instantiationService;
        this.editorService = editorService;
        this.ignoreGlobalFindBufferOnNextFocus = false;
        this.previousGlobalFindBufferValue = null;
        this._accessibilityHelpHintAnnounced = false;
        this._onSearchSubmit = this._register(( new Emitter()));
        this.onSearchSubmit = this._onSearchSubmit.event;
        this._onSearchCancel = this._register(( new Emitter()));
        this.onSearchCancel = this._onSearchCancel.event;
        this._onReplaceToggled = this._register(( new Emitter()));
        this.onReplaceToggled = this._onReplaceToggled.event;
        this._onReplaceStateChange = this._register(( new Emitter()));
        this.onReplaceStateChange = this._onReplaceStateChange.event;
        this._onPreserveCaseChange = this._register(( new Emitter()));
        this.onPreserveCaseChange = this._onPreserveCaseChange.event;
        this._onReplaceValueChanged = this._register(( new Emitter()));
        this.onReplaceValueChanged = this._onReplaceValueChanged.event;
        this._onReplaceAll = this._register(( new Emitter()));
        this.onReplaceAll = this._onReplaceAll.event;
        this._onBlur = this._register(( new Emitter()));
        this.onBlur = this._onBlur.event;
        this._onDidHeightChange = this._register(( new Emitter()));
        this.onDidHeightChange = this._onDidHeightChange.event;
        this._onDidToggleContext = this._register(( new Emitter()));
        this.onDidToggleContext = this._onDidToggleContext.event;
        this.replaceActive = SearchContext.ReplaceActiveKey.bindTo(this.contextKeyService);
        this.searchInputBoxFocused = SearchContext.SearchInputBoxFocusedKey.bindTo(this.contextKeyService);
        this.replaceInputBoxFocused = SearchContext.ReplaceInputBoxFocusedKey.bindTo(this.contextKeyService);
        const notebookOptions = options.notebookOptions ?? {
            isInNotebookMarkdownInput: true,
            isInNotebookMarkdownPreview: true,
            isInNotebookCellInput: true,
            isInNotebookCellOutput: true
        };
        this._notebookFilters = this._register(( new NotebookFindFilters(
            notebookOptions.isInNotebookMarkdownInput,
            notebookOptions.isInNotebookMarkdownPreview,
            notebookOptions.isInNotebookCellInput,
            notebookOptions.isInNotebookCellOutput,
            {
                findScopeType: NotebookFindScopeType.None
            }
        )));
        this._register(this._notebookFilters.onDidChange(() => {
            if (this.searchInput) {
                this.searchInput.updateFilterStyles();
            }
        }));
        this._register(this.editorService.onDidEditorsChange(e => {
            if (this.searchInput && e.event.editor instanceof NotebookEditorInput && (e.event.kind === GroupModelChangeKind.EDITOR_OPEN || e.event.kind === GroupModelChangeKind.EDITOR_CLOSE)) {
                this.searchInput.filterVisible = this._hasNotebookOpen();
            }
        }));
        this._replaceHistoryDelayer = ( new Delayer(500));
        this._toggleReplaceButtonListener = this._register(( new MutableDisposable()));
        this.render(container, options);
        this._register(this.configurationService.onDidChangeConfiguration(e => {
            if (e.affectsConfiguration("editor.accessibilitySupport")) {
                this.updateAccessibilitySupport();
            }
        }));
        this._register(
            this.accessibilityService.onDidChangeScreenReaderOptimized(() => this.updateAccessibilitySupport())
        );
        this.updateAccessibilitySupport();
    }
    _hasNotebookOpen() {
        const editors = this.editorService.editors;
        return ( editors.some(editor => editor instanceof NotebookEditorInput));
    }
    getNotebookFilters() {
        return this._notebookFilters;
    }
    focus(select = true, focusReplace = false, suppressGlobalSearchBuffer = false) {
        this.ignoreGlobalFindBufferOnNextFocus = suppressGlobalSearchBuffer;
        if (focusReplace && this.isReplaceShown()) {
            if (this.replaceInput) {
                this._updateSearchInputAriaLabel(false);
                this.replaceInput.focus();
                if (select) {
                    this.replaceInput.select();
                }
            }
        } else {
            if (this.searchInput) {
                this._updateSearchInputAriaLabel(true);
                this.searchInput.focus();
                if (select) {
                    this.searchInput.select();
                }
            }
        }
    }
    _updateSearchInputAriaLabel(includeHint) {
        if (!this.searchInput) {
            return;
        }
        let searchLabel = ( localize(13493, "Search: Type Search Term and press Enter to search"));
        if (includeHint && !this._accessibilityHelpHintAnnounced && this.configurationService.getValue("accessibility.verbosity.find") && this.accessibilityService.isScreenReaderOptimized()) {
            const keybinding = this.keybindingService.lookupKeybinding("editor.action.accessibilityHelp")?.getAriaLabel();
            if (keybinding) {
                searchLabel += ", " + ( localize(13494, "Press {0} for accessibility help", keybinding));
                this._accessibilityHelpHintAnnounced = true;
                this._labelResetTimeout?.dispose();
                this._labelResetTimeout = disposableTimeout(() => {
                    if (this.searchInput) {
                        this.searchInput.inputBox.setAriaLabel(( localize(13493, "Search: Type Search Term and press Enter to search")));
                    }
                }, 1000);
            }
        }
        this.searchInput.inputBox.setAriaLabel(searchLabel);
    }
    setWidth(width) {
        this.searchInput?.inputBox.layout();
        if (this.replaceInput) {
            this.replaceInput.width = width - 28;
            this.replaceInput.inputBox.layout();
        }
    }
    clear() {
        this.searchInput?.clear();
        this.replaceInput?.setValue("");
        this.setReplaceAllActionState(false);
    }
    isReplaceShown() {
        return this.replaceContainer ? !this.replaceContainer.classList.contains("disabled") : false;
    }
    isReplaceActive() {
        return !!this.replaceActive.get();
    }
    getReplaceValue() {
        return this.replaceInput?.getValue() ?? "";
    }
    toggleReplace(show) {
        if (show === undefined || show !== this.isReplaceShown()) {
            this.onToggleReplaceButton();
        }
    }
    getSearchHistory() {
        return this.searchInput?.inputBox.getHistory() ?? [];
    }
    getReplaceHistory() {
        return this.replaceInput?.inputBox.getHistory() ?? [];
    }
    prependSearchHistory(history) {
        this.searchInput?.inputBox.prependHistory(history);
    }
    prependReplaceHistory(history) {
        this.replaceInput?.inputBox.prependHistory(history);
    }
    clearHistory() {
        this.searchInput?.inputBox.clearHistory();
        this.replaceInput?.inputBox.clearHistory();
    }
    showNextSearchTerm() {
        this.searchInput?.inputBox.showNextValue();
    }
    showPreviousSearchTerm() {
        this.searchInput?.inputBox.showPreviousValue();
    }
    showNextReplaceTerm() {
        this.replaceInput?.inputBox.showNextValue();
    }
    showPreviousReplaceTerm() {
        this.replaceInput?.inputBox.showPreviousValue();
    }
    searchInputHasFocus() {
        return !!this.searchInputBoxFocused.get();
    }
    replaceInputHasFocus() {
        return !!this.replaceInput?.inputBox.hasFocus();
    }
    focusReplaceAllAction() {
        this.replaceActionBar?.focus(true);
    }
    focusRegexAction() {
        this.searchInput?.focusOnRegex();
    }
    set replaceButtonVisibility(val) {
        if (this.toggleReplaceButton) {
            this.toggleReplaceButton.element.style.display = val ? "" : "none";
        }
    }
    render(container, options) {
        this.domNode = append(container, $(".search-widget"));
        this.domNode.style.position = "relative";
        if (!options._hideReplaceToggle) {
            this.renderToggleReplaceButton(this.domNode);
        }
        this.renderSearchInput(this.domNode, options);
        this.renderReplaceInput(this.domNode, options);
    }
    updateAccessibilitySupport() {
        this.searchInput?.setFocusInputOnOptionClick(!this.accessibilityService.isScreenReaderOptimized());
    }
    renderToggleReplaceButton(parent) {
        const opts = {
            buttonBackground: undefined,
            buttonBorder: undefined,
            buttonForeground: undefined,
            buttonHoverBackground: undefined,
            buttonSecondaryBackground: undefined,
            buttonSecondaryForeground: undefined,
            buttonSecondaryHoverBackground: undefined,
            buttonSeparator: undefined,
            title: ( localize(13495, "Toggle Replace")),
            hoverDelegate: getDefaultHoverDelegate("element")
        };
        this.toggleReplaceButton = this._register(( new Button(parent, opts)));
        this.toggleReplaceButton.element.setAttribute("aria-expanded", "false");
        this.toggleReplaceButton.element.classList.add("toggle-replace-button");
        this.toggleReplaceButton.icon = searchHideReplaceIcon;
        this._toggleReplaceButtonListener.value = this.toggleReplaceButton.onDidClick(() => this.onToggleReplaceButton());
    }
    renderSearchInput(parent, options) {
        const history = options.searchHistory || [];
        const inputOptions = {
            label: ( localize(13493, "Search: Type Search Term and press Enter to search")),
            validation: value => this.validateSearchInput(value),
            placeholder: ( localize(13496, "Search")),
            appendCaseSensitiveLabel: this.keybindingService.appendKeybinding("", SearchCommandIds.ToggleCaseSensitiveCommandId),
            appendWholeWordsLabel: this.keybindingService.appendKeybinding("", SearchCommandIds.ToggleWholeWordCommandId),
            appendRegexLabel: this.keybindingService.appendKeybinding("", SearchCommandIds.ToggleRegexCommandId),
            history: ( new Set(history)),
            showHistoryHint: () => showHistoryKeybindingHint(this.keybindingService),
            flexibleHeight: true,
            flexibleMaxHeight: SearchWidget_1.INPUT_MAX_HEIGHT,
            showCommonFindToggles: true,
            inputBoxStyles: options.inputBoxStyles,
            toggleStyles: options.toggleStyles,
            hoverLifecycleOptions
        };
        const searchInputContainer = append(parent, $(".search-container.input-box"));
        this.searchInput = this._register(( new SearchFindInput(
            searchInputContainer,
            this.contextViewService,
            inputOptions,
            this.contextKeyService,
            this.contextMenuService,
            this.instantiationService,
            this._notebookFilters,
            this._hasNotebookOpen()
        )));
        this._register(
            this.searchInput.onKeyDown(keyboardEvent => this.onSearchInputKeyDown(keyboardEvent))
        );
        this.searchInput.setValue(options.value || "");
        this.searchInput.setRegex(!!options.isRegex);
        this.searchInput.setCaseSensitive(!!options.isCaseSensitive);
        this.searchInput.setWholeWords(!!options.isWholeWords);
        this._register(
            this.searchInput.onCaseSensitiveKeyDown(keyboardEvent => this.onCaseSensitiveKeyDown(keyboardEvent))
        );
        this._register(
            this.searchInput.onRegexKeyDown(keyboardEvent => this.onRegexKeyDown(keyboardEvent))
        );
        this._register(this.searchInput.inputBox.onDidChange(() => this.onSearchInputChanged()));
        this._register(
            this.searchInput.inputBox.onDidHeightChange(() => this._onDidHeightChange.fire())
        );
        this._register(this.onReplaceValueChanged(() => {
            this._replaceHistoryDelayer.trigger(() => this.replaceInput?.inputBox.addToHistory());
        }));
        this.searchInputFocusTracker = this._register(trackFocus(this.searchInput.inputBox.inputElement));
        this._register(this.searchInputFocusTracker.onDidFocus(async () => {
            this.searchInputBoxFocused.set(true);
            const useGlobalFindBuffer = this.searchConfiguration.globalFindClipboard;
            if (!this.ignoreGlobalFindBufferOnNextFocus && useGlobalFindBuffer) {
                const globalBufferText = await this.clipboardServce.readFindText();
                if (globalBufferText && this.previousGlobalFindBufferValue !== globalBufferText) {
                    this.searchInput?.inputBox.addToHistory();
                    this.searchInput?.setValue(globalBufferText);
                    this.searchInput?.select();
                }
                this.previousGlobalFindBufferValue = globalBufferText;
            }
            this.ignoreGlobalFindBufferOnNextFocus = false;
        }));
        this._register(
            this.searchInputFocusTracker.onDidBlur(() => this.searchInputBoxFocused.set(false))
        );
        this.showContextToggle = ( new Toggle({
            isChecked: false,
            title: this.keybindingService.appendKeybinding(( localize(13497, "Toggle Context Lines")), ToggleSearchEditorContextLinesCommandId),
            icon: searchShowContextIcon,
            hoverLifecycleOptions,
            ...defaultToggleStyles
        }));
        this._register(this.showContextToggle.onChange(() => this.onContextLinesChanged()));
        if (options.showContextToggle) {
            this.contextLinesInput = ( new InputBox(searchInputContainer, this.contextViewService, {
                type: "number",
                inputBoxStyles: defaultInputBoxStyles
            }));
            this.contextLinesInput.element.classList.add("context-lines-input");
            this.contextLinesInput.value = "" + (this.configurationService.getValue("search").searchEditor.defaultNumberOfContextLines ?? 1);
            this._register(this.contextLinesInput.onDidChange(value => {
                if (value !== "0") {
                    this.showContextToggle.checked = true;
                }
                this.onContextLinesChanged();
            }));
            append(searchInputContainer, this.showContextToggle.domNode);
        }
    }
    onContextLinesChanged() {
        this._onDidToggleContext.fire();
        if (this.contextLinesInput.value.includes("-")) {
            this.contextLinesInput.value = "0";
        }
        this._onDidToggleContext.fire();
    }
    setContextLines(lines) {
        if (!this.contextLinesInput) {
            return;
        }
        if (lines === 0) {
            this.showContextToggle.checked = false;
        } else {
            this.showContextToggle.checked = true;
            this.contextLinesInput.value = "" + lines;
        }
    }
    renderReplaceInput(parent, options) {
        this.replaceContainer = append(parent, $(".replace-container.disabled"));
        const replaceBox = append(this.replaceContainer, $(".replace-input"));
        this.replaceInput = this._register(( new ContextScopedReplaceInput(replaceBox, this.contextViewService, {
            label: ( localize(13498, "Replace: Type replace term and press Enter to preview")),
            placeholder: ( localize(13499, "Replace")),
            appendPreserveCaseLabel: this.keybindingService.appendKeybinding("", SearchCommandIds.TogglePreserveCaseId),
            history: ( new Set(options.replaceHistory)),
            showHistoryHint: () => showHistoryKeybindingHint(this.keybindingService),
            flexibleHeight: true,
            flexibleMaxHeight: SearchWidget_1.INPUT_MAX_HEIGHT,
            inputBoxStyles: options.inputBoxStyles,
            toggleStyles: options.toggleStyles,
            hoverLifecycleOptions
        }, this.contextKeyService, true)));
        this._register(this.replaceInput.onDidOptionChange(viaKeyboard => {
            if (!viaKeyboard) {
                if (this.replaceInput) {
                    this._onPreserveCaseChange.fire(this.replaceInput.getPreserveCase());
                }
            }
        }));
        this._register(
            this.replaceInput.onKeyDown(keyboardEvent => this.onReplaceInputKeyDown(keyboardEvent))
        );
        this.replaceInput.setValue(options.replaceValue || "");
        this._register(
            this.replaceInput.inputBox.onDidChange(() => this._onReplaceValueChanged.fire())
        );
        this._register(
            this.replaceInput.inputBox.onDidHeightChange(() => this._onDidHeightChange.fire())
        );
        this.replaceAllAction = ( new ReplaceAllAction(this));
        this.replaceAllAction.label = SearchWidget_1.REPLACE_ALL_DISABLED_LABEL;
        this.replaceActionBar = this._register(( new ActionBar(this.replaceContainer)));
        this.replaceActionBar.push([this.replaceAllAction], {
            icon: true,
            label: false
        });
        this.onkeydown(
            this.replaceActionBar.domNode,
            keyboardEvent => this.onReplaceActionbarKeyDown(keyboardEvent)
        );
        this.replaceInputFocusTracker = this._register(trackFocus(this.replaceInput.inputBox.inputElement));
        this._register(
            this.replaceInputFocusTracker.onDidFocus(() => this.replaceInputBoxFocused.set(true))
        );
        this._register(
            this.replaceInputFocusTracker.onDidBlur(() => this.replaceInputBoxFocused.set(false))
        );
        this._register(
            this.replaceInput.onPreserveCaseKeyDown(keyboardEvent => this.onPreserveCaseKeyDown(keyboardEvent))
        );
    }
    triggerReplaceAll() {
        this._onReplaceAll.fire();
        return Promise.resolve();
    }
    onToggleReplaceButton() {
        this.replaceContainer?.classList.toggle("disabled");
        if (this.isReplaceShown()) {
            this.toggleReplaceButton?.element.classList.remove(...ThemeIcon.asClassNameArray(searchHideReplaceIcon));
            this.toggleReplaceButton?.element.classList.add(...ThemeIcon.asClassNameArray(searchShowReplaceIcon));
        } else {
            this.toggleReplaceButton?.element.classList.remove(...ThemeIcon.asClassNameArray(searchShowReplaceIcon));
            this.toggleReplaceButton?.element.classList.add(...ThemeIcon.asClassNameArray(searchHideReplaceIcon));
        }
        this.toggleReplaceButton?.element.setAttribute("aria-expanded", this.isReplaceShown() ? "true" : "false");
        this.updateReplaceActiveState();
        this._onReplaceToggled.fire();
    }
    setValue(value) {
        this.searchInput?.setValue(value);
    }
    setReplaceAllActionState(enabled) {
        if (this.replaceAllAction && (this.replaceAllAction.enabled !== enabled)) {
            this.replaceAllAction.enabled = enabled;
            this.replaceAllAction.label = enabled ? SearchWidget_1.REPLACE_ALL_ENABLED_LABEL(this.keybindingService) : SearchWidget_1.REPLACE_ALL_DISABLED_LABEL;
            this.updateReplaceActiveState();
        }
    }
    updateReplaceActiveState() {
        const currentState = this.isReplaceActive();
        const newState = this.isReplaceShown() && !!this.replaceAllAction?.enabled;
        if (currentState !== newState) {
            this.replaceActive.set(newState);
            this._onReplaceStateChange.fire(newState);
            this.replaceInput?.inputBox.layout();
        }
    }
    validateSearchInput(value) {
        if (value.length === 0) {
            return null;
        }
        if (!(this.searchInput?.getRegex())) {
            return null;
        }
        try {
            ( new RegExp(value, "u"));
        } catch (e) {
            return {
                content: e.message
            };
        }
        return null;
    }
    onSearchInputChanged() {
        this.searchInput?.clearMessage();
        this.setReplaceAllActionState(false);
        if (this.searchConfiguration.searchOnType) {
            if (this.searchInput?.getRegex()) {
                try {
                    const regex = ( new RegExp(this.searchInput.getValue(), "ug"));
                    const matchienessHeuristic = `
                                ~!@#$%^&*()_+
                                \`1234567890-=
                                qwertyuiop[]\\
                                QWERTYUIOP{}|
                                asdfghjkl;'
                                ASDFGHJKL:"
                                zxcvbnm,./
                                ZXCVBNM<>? `.match(regex)?.length ?? 0;
                    const delayMultiplier = matchienessHeuristic < 50 ? 1 : matchienessHeuristic < 100 ?
                    5 : 10;
                    this.submitSearch(
                        true,
                        this.searchConfiguration.searchOnTypeDebouncePeriod * delayMultiplier
                    );
                } catch {}
            } else {
                this.submitSearch(true, this.searchConfiguration.searchOnTypeDebouncePeriod);
            }
        }
    }
    onSearchInputKeyDown(keyboardEvent) {
        if (keyboardEvent.equals(ctrlKeyMod | KeyCode.Enter)) {
            this.searchInput?.inputBox.insertAtCursor("\n");
            keyboardEvent.preventDefault();
        }
        if (keyboardEvent.equals(KeyCode.Enter)) {
            this.searchInput?.onSearchSubmit();
            this.submitSearch();
            keyboardEvent.preventDefault();
        } else if (keyboardEvent.equals(KeyCode.Escape)) {
            this._onSearchCancel.fire({
                focus: true
            });
            keyboardEvent.preventDefault();
        } else if (keyboardEvent.equals(KeyCode.Tab)) {
            if (this.isReplaceShown()) {
                this.replaceInput?.focus();
            } else {
                this.searchInput?.focusOnCaseSensitive();
            }
            keyboardEvent.preventDefault();
        } else if (keyboardEvent.equals(KeyCode.UpArrow)) {
            stopPropagationForMultiLineUpwards(
                keyboardEvent,
                this.searchInput?.getValue() ?? "",
                this.searchInput?.domNode.querySelector("textarea") ?? null
            );
        } else if (keyboardEvent.equals(KeyCode.DownArrow)) {
            stopPropagationForMultiLineDownwards(
                keyboardEvent,
                this.searchInput?.getValue() ?? "",
                this.searchInput?.domNode.querySelector("textarea") ?? null
            );
        } else if (keyboardEvent.equals(KeyCode.PageUp)) {
            const inputElement = this.searchInput?.inputBox.inputElement;
            if (inputElement) {
                inputElement.setSelectionRange(0, 0);
                inputElement.focus();
                keyboardEvent.preventDefault();
            }
        } else if (keyboardEvent.equals(KeyCode.PageDown)) {
            const inputElement = this.searchInput?.inputBox.inputElement;
            if (inputElement) {
                const endOfText = inputElement.value.length;
                inputElement.setSelectionRange(endOfText, endOfText);
                inputElement.focus();
                keyboardEvent.preventDefault();
            }
        }
    }
    onCaseSensitiveKeyDown(keyboardEvent) {
        if (keyboardEvent.equals(KeyMod$1.Shift | KeyCode.Tab)) {
            if (this.isReplaceShown()) {
                this.replaceInput?.focus();
                keyboardEvent.preventDefault();
            }
        }
    }
    onRegexKeyDown(keyboardEvent) {
        if (keyboardEvent.equals(KeyCode.Tab)) {
            if (this.isReplaceShown()) {
                this.replaceInput?.focusOnPreserve();
                keyboardEvent.preventDefault();
            }
        }
    }
    onPreserveCaseKeyDown(keyboardEvent) {
        if (keyboardEvent.equals(KeyCode.Tab)) {
            if (this.isReplaceActive()) {
                this.focusReplaceAllAction();
            } else {
                this._onBlur.fire();
            }
            keyboardEvent.preventDefault();
        } else if (keyboardEvent.equals(KeyMod$1.Shift | KeyCode.Tab)) {
            this.focusRegexAction();
            keyboardEvent.preventDefault();
        }
    }
    onReplaceInputKeyDown(keyboardEvent) {
        if (keyboardEvent.equals(ctrlKeyMod | KeyCode.Enter)) {
            this.replaceInput?.inputBox.insertAtCursor("\n");
            keyboardEvent.preventDefault();
        }
        if (keyboardEvent.equals(KeyCode.Enter)) {
            this.submitSearch();
            keyboardEvent.preventDefault();
        } else if (keyboardEvent.equals(KeyCode.Tab)) {
            this.searchInput?.focusOnCaseSensitive();
            keyboardEvent.preventDefault();
        } else if (keyboardEvent.equals(KeyMod$1.Shift | KeyCode.Tab)) {
            this.searchInput?.focus();
            keyboardEvent.preventDefault();
        } else if (keyboardEvent.equals(KeyCode.UpArrow)) {
            stopPropagationForMultiLineUpwards(
                keyboardEvent,
                this.replaceInput?.getValue() ?? "",
                this.replaceInput?.domNode.querySelector("textarea") ?? null
            );
        } else if (keyboardEvent.equals(KeyCode.DownArrow)) {
            stopPropagationForMultiLineDownwards(
                keyboardEvent,
                this.replaceInput?.getValue() ?? "",
                this.replaceInput?.domNode.querySelector("textarea") ?? null
            );
        }
    }
    onReplaceActionbarKeyDown(keyboardEvent) {
        if (keyboardEvent.equals(KeyMod$1.Shift | KeyCode.Tab)) {
            this.focusRegexAction();
            keyboardEvent.preventDefault();
        }
    }
    async submitSearch(triggeredOnType = false, delay = 0) {
        this.searchInput?.validate();
        if (!this.searchInput?.inputBox.isInputValid()) {
            return;
        }
        const value = this.searchInput.getValue();
        const useGlobalFindBuffer = this.searchConfiguration.globalFindClipboard;
        if (value && useGlobalFindBuffer) {
            await this.clipboardServce.writeFindText(value);
        }
        this._onSearchSubmit.fire({
            triggeredOnType,
            delay
        });
    }
    getContextLines() {
        return this.showContextToggle.checked ? +this.contextLinesInput.value : 0;
    }
    modifyContextLines(increase) {
        const current = +this.contextLinesInput.value;
        const modified = current + (increase ? 1 : -1);
        this.showContextToggle.checked = modified !== 0;
        this.contextLinesInput.value = "" + modified;
    }
    toggleContextLines() {
        this.showContextToggle.checked = !this.showContextToggle.checked;
        this.onContextLinesChanged();
    }
    dispose() {
        this.setReplaceAllActionState(false);
        super.dispose();
    }
    get searchConfiguration() {
        return this.configurationService.getValue("search");
    }
};
SearchWidget = SearchWidget_1 = ( __decorate([( __param(2, IContextViewService)), ( __param(3, IContextKeyService)), ( __param(4, IKeybindingService)), ( __param(5, IClipboardService)), ( __param(6, IConfigurationService)), ( __param(7, IAccessibilityService)), ( __param(8, IContextMenuService)), ( __param(9, IInstantiationService)), ( __param(10, IEditorService))], SearchWidget));
function registerContributions() {
    KeybindingsRegistry.registerCommandAndKeybindingRule({
        id: ReplaceAllAction.ID,
        weight: KeybindingWeight.WorkbenchContrib,
        when: ( ContextKeyExpr.and(
            SearchContext.SearchViewVisibleKey,
            SearchContext.ReplaceActiveKey,
            CONTEXT_FIND_WIDGET_NOT_VISIBLE
        )),
        primary: KeyMod$1.Alt | KeyMod$1.CtrlCmd | KeyCode.Enter,
        handler: accessor => {
            const viewsService = accessor.get(IViewsService);
            if (isSearchViewFocused(viewsService)) {
                const searchView = getSearchView(viewsService);
                if (searchView) {
                    ( new ReplaceAllAction(searchView.searchAndReplaceWidget)).run();
                }
            }
        }
    });
}

export { SearchWidget, registerContributions };
