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/gui/dialog-search-replace", "wed/key-constants", "../../base-config", "../../wed-test-util"], function (require, exports, dloc_1, dialog_search_replace_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("dialog-search-replace", () => {
        let setup;
        let editor;
        let dataRoot;
        let caretManager;
        let ps;
        let firstBodyP;
        let firstBodyPLocation;
        let pFiveFirstThree;
        let pFiveFirstFour;
        let pSevenFirstThree;
        let firstABCText;
        let firstABCDText;
        let firstABCAttribute;
        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));
            // This is the first "abcd" found when doing a TEXT search.
            firstABCDText = new dloc_1.DLocRange(caretManager.mustFromDataLocation(ps[3].firstChild.firstChild, 0), caretManager.mustFromDataLocation(ps[3].lastChild, 2));
            const rend = ps[7].getAttributeNode("rend");
            firstABCAttribute = new dloc_1.DLocRange(caretManager.mustFromDataLocation(rend, 0), caretManager.mustFromDataLocation(rend, 3));
        });
        afterEach(() => {
            // Make sure there is no dialog after each test.
            expect(document.querySelector(".modal.in")).to.be.null;
            setup.reset();
        });
        after(() => {
            setup.restore();
            // tslint:disable-next-line:no-any
            editor = undefined;
        });
        function makeSearch(direction) {
            // tslint:disable-next-line:no-any
            return new dialog_search_replace_1.DialogSearchReplace(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, "correct top").to.have.nested.property("top")
                .closeTo(rangeRect.top, 3);
            expect(highlightRect, "correct left").to.have.nested.property("left")
                .closeTo(rangeRect.left, 3);
            highlightRect = highlights[highlights.length - 1].getBoundingClientRect();
            expect(highlightRect, "correct bottom").to.have.nested.property("bottom")
                .closeTo(rangeRect.bottom, 3);
            expect(highlightRect, "correct right").to.have.nested.property("right")
                .closeTo(rangeRect.right, 3);
        }
        function checkNoHighlight() {
            expect(document.querySelector("._wed_highlight")).to.be.null;
        }
        function closeDialog() {
            const close = document.querySelector(".modal.in .close");
            close.click();
        }
        function typeInSearch(text) {
            const search = document
                .querySelector(".modal.in input[name=search]");
            search.value = text;
            $(search).trigger("input");
        }
        function typeInReplace(text) {
            const field = document
                .querySelector(".modal.in input[name=replace]");
            field.value = text;
            $(field).trigger("input");
        }
        function clearReplace() {
            const field = document
                .querySelector(".modal.in input[name=replace]");
            field.value = "";
            $(field).trigger("input");
        }
        function clickFind() {
            const find = document
                .querySelector(".modal.in [data-bb-handler=find]");
            find.click();
        }
        function clickReplace() {
            const button = document.querySelector(".modal.in [data-bb-handler=replaceFind]");
            button.click();
        }
        function clickReplaceAll() {
            const button = document.querySelector(".modal.in [data-bb-handler=replaceAll]");
            button.click();
        }
        function clickBackwards() {
            const button = document.querySelector(".modal.in [name=direction][value=backwards]");
            button.click();
        }
        function checkReplaceButton(enabled) {
            const button = document.querySelector(".modal.in [data-bb-handler=replaceFind]");
            expect(button).to.have.property("disabled").equal(!enabled);
        }
        function checkReplaceAllButton(enabled) {
            const button = document.querySelector(".modal.in [data-bb-handler=replaceAll]");
            expect(button).to.have.property("disabled").equal(!enabled);
        }
        function clickAttributes() {
            const button = document.querySelector(".modal.in [name=context][value=attributes]");
            button.click();
        }
        describe("DialogSearchReplace", () => {
            beforeEach(() => {
                checkNoHighlight();
            });
            afterEach(() => {
                closeDialog();
            });
            it("can be built", () => {
                makeSearch(dialog_search_replace_1.Direction.FORWARD);
            });
            it("updates the highlight when the user types", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abc");
                checkHighlightRanges(firstABCText);
            });
            it("searches forward", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abc");
                checkHighlightRanges(firstABCText);
                clickFind();
                checkHighlightRanges(pFiveFirstThree);
            });
            it("searches backwards", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abc");
                checkHighlightRanges(firstABCText);
                clickFind();
                checkHighlightRanges(pFiveFirstThree);
                clickBackwards();
                clickFind();
                checkHighlightRanges(firstABCText);
            });
            it("removes the highlight when there is no match", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abcNO MATCH");
                checkNoHighlight();
            });
            function rollTest(key) {
                editor.type(key);
                typeInSearch("abc");
                let count = 0;
                while (document.querySelector("._wed_highlight") !== null) {
                    count++;
                    clickFind();
                }
                expect(count).to.be.greaterThan(0);
                let count2 = 0;
                clickFind();
                while (document.querySelector("._wed_highlight") !== null) {
                    count2++;
                    clickFind();
                }
                expect(count2).to.equal(count);
            }
            it("rolls over forward", () => {
                editor.caretManager.setCaret(editor.caretManager.minCaret);
                rollTest(key_constants_1.SEARCH_FORWARD);
            });
            it("rolls over backwards", () => {
                editor.caretManager.setCaret(editor.caretManager.maxCaret);
                rollTest(key_constants_1.SEARCH_BACKWARDS);
            });
            it("starts with a replace button that is disabled", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                checkReplaceButton(false);
            });
            it("keeps the replace button disabled if the hit is ill-formed", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abc");
                // This is an ill-formed hit.
                checkHighlightRanges(firstABCText);
                checkReplaceButton(false);
                typeInReplace("foo");
                checkReplaceButton(false);
            });
            it("enables the replace button if the replace field is filled", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abc");
                clickFind();
                checkHighlightRanges(pFiveFirstThree);
                checkReplaceButton(false);
                typeInReplace("foo");
                checkReplaceButton(true);
                clearReplace();
                checkReplaceButton(false);
            });
            it("starts with a replace all button that is disabled", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                checkReplaceAllButton(false);
            });
            it("enables replace button even if the hit is ill-formed", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abc");
                // This is an ill-formed hit.
                checkHighlightRanges(firstABCText);
                typeInReplace("foo");
                checkReplaceAllButton(true);
            });
            it("enables the replace all button if the replace field is filled", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abc");
                clickFind();
                checkHighlightRanges(pFiveFirstThree);
                checkReplaceAllButton(false);
                typeInReplace("foo");
                checkReplaceAllButton(true);
                clearReplace();
                checkReplaceAllButton(false);
            });
            it("replaces", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abc");
                // This is an ill-formed hit.
                checkHighlightRanges(firstABCText);
                checkReplaceButton(false);
                clickFind();
                checkHighlightRanges(pFiveFirstThree);
                typeInReplace("foo");
                checkReplaceButton(true);
                clickReplace();
                const parent = pFiveFirstThree.start.node.parentNode;
                const dataParent = editor.toDataNode(parent);
                expect(dataParent).to.have.property("textContent").equal("foodefghij");
                checkHighlightRanges(pSevenFirstThree);
            });
            it("replaces all", function () {
                // tslint:disable-next-line:no-invalid-this
                this.timeout(4000);
                editor.caretManager.setCaret(editor.caretManager.minCaret);
                editor.type(key_constants_1.SEARCH_FORWARD);
                typeInSearch("abcd");
                typeInReplace("fnarble");
                clickReplaceAll();
                clickFind();
                // This is a hit that cannot be replaced because it is malformed.
                checkHighlightRanges(firstABCDText);
                clickFind();
                // Everything else was replaced, so we have no more hits.
                checkNoHighlight();
            });
            it("searches attributes", () => {
                editor.type(key_constants_1.SEARCH_FORWARD);
                clickAttributes();
                typeInSearch("abc");
                checkHighlightRanges(firstABCAttribute);
            });
        });
    });
});
//# sourceMappingURL=dialog-search-replace-test.js.map