var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
define(["require", "exports", "wed/dloc", "wed/editor", "wed/gui/quick-search", "wed/key-constants", "../../base-config", "../../wed-test-util"], function (require, exports, dloc_1, editor_1, quick_search_1, key_constants_1, globalConfig, wed_test_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    globalConfig = __importStar(globalConfig);
    const expect = chai.expect;
    describe("quick-search", () => {
        let setup;
        let editor;
        let dataRoot;
        let caretManager;
        let ps;
        let firstBodyP;
        let firstBodyPLocation;
        let pFiveFirstThree;
        let pFiveFirstFour;
        let pSevenFirstThree;
        let firstABCText;
        before(() => {
            setup = new wed_test_util_1.EditorSetup(`${wed_test_util_1.dataPath}/search_test_data/source_converted.xml`, globalConfig.config, document);
            ({ editor } = setup);
            return setup.init();
        });
        beforeEach(() => {
            dataRoot = editor.dataRoot;
            caretManager = editor.caretManager;
            ps = Array.from(dataRoot.querySelectorAll("body p"));
            firstBodyP = ps[0];
            firstBodyPLocation = caretManager.mustFromDataLocation(dloc_1.DLoc.mustMakeDLoc(dataRoot, firstBodyP, 0));
            caretManager.setCaret(firstBodyPLocation);
            // First 3 text characters in the 5th paragraph (at index 4).
            const pFiveStart = dloc_1.DLoc.mustMakeDLoc(dataRoot, ps[4].firstChild, 0);
            pFiveFirstThree = new dloc_1.DLocRange(caretManager.mustFromDataLocation(pFiveStart), caretManager.mustFromDataLocation(pFiveStart.makeWithOffset(3)));
            expect(pFiveFirstThree.mustMakeDOMRange().toString()).to.equal("abc");
            pFiveFirstFour = new dloc_1.DLocRange(caretManager.mustFromDataLocation(pFiveStart), caretManager.mustFromDataLocation(pFiveStart.makeWithOffset(4)));
            expect(pFiveFirstFour.mustMakeDOMRange().toString()).to.equal("abcd");
            const pSevenStart = dloc_1.DLoc.mustMakeDLoc(dataRoot, ps[6].firstChild, 0);
            pSevenFirstThree = new dloc_1.DLocRange(caretManager.mustFromDataLocation(pSevenStart), caretManager.mustFromDataLocation(pSevenStart.makeWithOffset(3)));
            expect(pSevenFirstThree.mustMakeDOMRange().toString()).to.equal("abc");
            // This is the first "abc" found when doing a TEXT search.
            firstABCText = new dloc_1.DLocRange(caretManager.mustFromDataLocation(ps[3].firstChild.firstChild, 0), caretManager.mustFromDataLocation(ps[3].lastChild, 1));
        });
        afterEach(() => {
            // Make sure the minibuffer is off after each test.
            expect(editor).to.have.nested.property("minibuffer.enabled").false;
            setup.reset();
        });
        after(() => {
            setup.restore();
            // tslint:disable-next-line:no-any
            editor = undefined;
        });
        function makeQuickSearch(direction) {
            // tslint:disable-next-line:no-any
            return new quick_search_1.QuickSearch(editor, editor.scroller, direction);
        }
        function checkHighlightRanges(range) {
            const highlights = document.querySelectorAll("._wed_highlight");
            expect(highlights).to.have.property("length").greaterThan(0);
            let highlightRect = highlights[0].getBoundingClientRect();
            const rangeRect = range.mustMakeDOMRange().getBoundingClientRect();
            // The highlights are built as a series of rectangles. Checking each and
            // every rectangle would be onerous. We check the start and end of the
            // range.
            // Rounding can make the boundaries vary a bit.
            expect(highlightRect).to.have.nested.property("top")
                .closeTo(rangeRect.top, 3);
            expect(highlightRect).to.have.nested.property("left")
                .closeTo(rangeRect.left, 3);
            highlightRect = highlights[highlights.length - 1].getBoundingClientRect();
            expect(highlightRect).to.have.nested.property("bottom")
                .closeTo(rangeRect.bottom, 3);
            expect(highlightRect).to.have.nested.property("right")
                .closeTo(rangeRect.right, 3);
        }
        function checkNoHighlight() {
            expect(document.querySelector("._wed_highlight")).to.be.null;
        }
        describe("QuickSearch", () => {
            beforeEach(() => {
                checkNoHighlight();
            });
            it("can be built", () => {
                makeQuickSearch(quick_search_1.Direction.FORWARD);
                editor.minibuffer.uninstallClient();
            });
            it("prompts forward", () => {
                editor.type(key_constants_1.QUICKSEARCH_FORWARD);
                expect(editor).to.have.nested.property("minibuffer.prompt")
                    .equal("Search forward:");
                editor.type(key_constants_1.ESCAPE, editor_1.WedEventTarget.MINIBUFFER);
            });
            it("prompts backwards", () => {
                editor.type(key_constants_1.QUICKSEARCH_BACKWARDS);
                expect(editor).to.have.nested.property("minibuffer.prompt")
                    .equal("Search backwards:");
                editor.type(key_constants_1.ESCAPE, editor_1.WedEventTarget.MINIBUFFER);
            });
            it("updates the highlight when the user types", () => {
                editor.type(key_constants_1.QUICKSEARCH_FORWARD);
                editor.type("abc", editor_1.WedEventTarget.MINIBUFFER);
                checkHighlightRanges(firstABCText);
                editor.type(key_constants_1.ESCAPE, editor_1.WedEventTarget.MINIBUFFER);
            });
            it("searches forward", () => {
                editor.type(key_constants_1.QUICKSEARCH_FORWARD);
                editor.type("abc", editor_1.WedEventTarget.MINIBUFFER);
                checkHighlightRanges(firstABCText);
                editor.type(key_constants_1.QUICKSEARCH_FORWARD, editor_1.WedEventTarget.MINIBUFFER);
                checkHighlightRanges(pFiveFirstThree);
                editor.type(key_constants_1.ESCAPE, editor_1.WedEventTarget.MINIBUFFER);
            });
            it("searches backwards", () => {
                editor.type(key_constants_1.QUICKSEARCH_FORWARD);
                editor.type("abc", editor_1.WedEventTarget.MINIBUFFER);
                checkHighlightRanges(firstABCText);
                editor.type(key_constants_1.QUICKSEARCH_FORWARD, editor_1.WedEventTarget.MINIBUFFER);
                checkHighlightRanges(pFiveFirstThree);
                editor.type(key_constants_1.QUICKSEARCH_BACKWARDS, editor_1.WedEventTarget.MINIBUFFER);
                checkHighlightRanges(firstABCText);
                editor.type(key_constants_1.ESCAPE, editor_1.WedEventTarget.MINIBUFFER);
            });
            it("removes the highlight when there is no match", () => {
                editor.type(key_constants_1.QUICKSEARCH_FORWARD);
                editor.type("abcNO MATCH", editor_1.WedEventTarget.MINIBUFFER);
                checkNoHighlight();
                editor.type(key_constants_1.ESCAPE, editor_1.WedEventTarget.MINIBUFFER);
            });
            function rollTest(key) {
                editor.type(key);
                editor.type("abc", editor_1.WedEventTarget.MINIBUFFER);
                let count = 0;
                while (document.querySelector("._wed_highlight") !== null) {
                    count++;
                    editor.type(key, editor_1.WedEventTarget.MINIBUFFER);
                }
                expect(count).to.be.greaterThan(0);
                let count2 = 0;
                editor.type(key, editor_1.WedEventTarget.MINIBUFFER);
                while (document.querySelector("._wed_highlight") !== null) {
                    count2++;
                    editor.type(key, editor_1.WedEventTarget.MINIBUFFER);
                }
                expect(count2).to.equal(count);
                editor.type(key_constants_1.ESCAPE, editor_1.WedEventTarget.MINIBUFFER);
            }
            it("rolls over forward", () => {
                editor.caretManager.setCaret(editor.caretManager.minCaret);
                rollTest(key_constants_1.QUICKSEARCH_FORWARD);
            });
            it("rolls over backwards", () => {
                editor.caretManager.setCaret(editor.caretManager.maxCaret);
                rollTest(key_constants_1.QUICKSEARCH_BACKWARDS);
            });
        });
    });
});
//# sourceMappingURL=quick-search-test.js.map