"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.closeSearch = exports.openSearch = exports.replaceAll = exports.replaceNext = exports.findPrev = exports.findNext = void 0;
const prosemirror_search_1 = require("prosemirror-search");
const SelectionContext_1 = require("../SelectionContext/index.js");
const const_1 = require("./const.js");
const wrap_command_1 = require("./utils/wrap-command.js");
exports.findNext = (0, wrap_command_1.wrapCommand)(prosemirror_search_1.findNext, SelectionContext_1.hideSelectionMenu);
exports.findPrev = (0, wrap_command_1.wrapCommand)(prosemirror_search_1.findPrev, SelectionContext_1.hideSelectionMenu);
exports.replaceNext = (0, wrap_command_1.wrapCommand)(prosemirror_search_1.replaceNext, SelectionContext_1.hideSelectionMenu);
exports.replaceAll = (0, wrap_command_1.wrapCommand)(prosemirror_search_1.replaceAll, SelectionContext_1.hideSelectionMenu);
const openSearch = (state, dispatch) => {
    if (dispatch) {
        const searchState = (0, prosemirror_search_1.getSearchState)(state);
        const search = state.doc.textBetween(state.selection.from, state.selection.to, ' ');
        const meta = { open: true };
        dispatch((0, prosemirror_search_1.setSearchState)((0, SelectionContext_1.hideSelectionMenu)(state.tr.setMeta(const_1.pluginKey, meta)), new prosemirror_search_1.SearchQuery({
            ...(searchState
                ? {
                    regexp: searchState.query.regexp,
                    replace: searchState.query.replace,
                    literal: searchState.query.literal,
                    wholeWord: searchState.query.wholeWord,
                    caseSensitive: searchState.query.caseSensitive,
                    filter: searchState.query.filter || undefined,
                }
                : undefined),
            search,
        })));
    }
    return true;
};
exports.openSearch = openSearch;
const closeSearch = (state, dispatch) => {
    if (dispatch) {
        const meta = { open: false };
        dispatch((0, prosemirror_search_1.setSearchState)(state.tr.setMeta(const_1.pluginKey, meta), new prosemirror_search_1.SearchQuery({ search: '' })));
    }
    return true;
};
exports.closeSearch = closeSearch;
//# sourceMappingURL=commands.js.map
