var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
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", "chai", "merge-options", "wed/gui-selector", "wed/input-trigger", "wed/input-trigger-factory", "wed/key", "wed/key-constants", "../base-config", "../util", "../wed-test-util"], function (require, exports, chai_1, merge_options_1, gui_selector_1, input_trigger_1, input_trigger_factory_1, key, key_constants_1, globalConfig, util_1, wed_test_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    merge_options_1 = __importDefault(merge_options_1);
    key = __importStar(key);
    globalConfig = __importStar(globalConfig);
    const options = {
        schema: "/base/build/schemas/tei-simplified-rng.js",
        mode: {
            path: "wed/modes/generic/generic",
            options: {
                metadata: "/base/build/schemas/tei-metadata.json",
            },
            // We set a submode that operates on teiHeader so as to be able to test that
            // input triggers operate only on their own region.
            submode: {
                method: "selector",
                selector: "TEI>teiHeader",
                mode: {
                    path: "wed/modes/generic/generic",
                    options: {
                        metadata: "/base/build/schemas/tei-metadata.json",
                    },
                },
            },
        },
    };
    // This is an ad-hoc function meant for these tests *only*. The XML
    // serialization adds an xmlns declaration that we don't care for. So...
    function cleanNamespace(str) {
        return str.replace(/ xmlns=".*?"/, "");
    }
    describe("InputTrigger", () => {
        let setup;
        let editor;
        let mode;
        const mappings = 
        // tslint:disable-next-line:no-http-string
        { "": "http://www.tei-c.org/ns/1.0" };
        let pSelector;
        let pInBody;
        before(() => {
            pSelector = gui_selector_1.GUISelector.fromDataSelector("p", mappings);
        });
        beforeEach(() => {
            setup = new wed_test_util_1.EditorSetup(`${wed_test_util_1.dataPath}/input_trigger_test_data/\
source_converted.xml`, merge_options_1.default(globalConfig.config, options), document);
            ({ editor } = setup);
            return setup.init().then(() => {
                mode = editor.modeTree.getMode(editor.guiRoot);
                pInBody = editor.dataRoot.querySelector("body p");
            });
        });
        afterEach(() => {
            setup.restore();
            // tslint:disable-next-line:no-any
            editor = undefined;
        });
        function pasteTest(p) {
            const trigger = new input_trigger_1.InputTrigger(editor, mode, pSelector);
            let seen = 0;
            trigger.addKeyHandler(key.makeKey(";"), (evType, el) => {
                chai_1.assert.equal(evType, "paste");
                chai_1.assert.equal(el, p);
                seen++;
            });
            // Synthetic event
            const event = util_1.makeFakePasteEvent({
                types: ["text/plain"],
                getData: () => "abc;def",
            });
            editor.caretManager.setCaret(p, 0);
            editor.$guiRoot.trigger(event);
            return seen;
        }
        function keydownTest(p) {
            const trigger = new input_trigger_1.InputTrigger(editor, mode, pSelector);
            let seen = 0;
            trigger.addKeyHandler(key_constants_1.ENTER, (evType, el, ev) => {
                chai_1.assert.equal(evType, "keydown");
                chai_1.assert.equal(el, p);
                ev.stopImmediatePropagation();
                seen++;
            });
            // Synthetic event
            editor.caretManager.setCaret(p, 0);
            editor.type(key_constants_1.ENTER);
            return seen;
        }
        function keypressTest(p) {
            const trigger = new input_trigger_1.InputTrigger(editor, mode, pSelector);
            let seen = 0;
            trigger.addKeyHandler(key.makeKey(";"), (evType, el, ev) => {
                chai_1.assert.equal(evType, "keypress");
                chai_1.assert.equal(el, p);
                ev.stopImmediatePropagation();
                seen++;
            });
            editor.caretManager.setCaret(p, 0);
            editor.type(";");
            return seen;
        }
        it("triggers on paste events", () => {
            chai_1.assert.equal(pasteTest(pInBody), 1);
        });
        it("triggers on keydown events", () => {
            chai_1.assert.equal(keydownTest(pInBody), 1);
        });
        it("triggers on keypress events", () => {
            chai_1.assert.equal(keypressTest(pInBody), 1);
        });
        it("does not trigger on unimportant input events", () => {
            const trigger = new input_trigger_1.InputTrigger(editor, mode, pSelector);
            let seen = 0;
            const p = pInBody;
            trigger.addKeyHandler(key.makeKey(";"), () => {
                seen++;
            });
            editor.caretManager.setCaret(p, 0);
            editor.type(":");
            chai_1.assert.equal(seen, 0);
        });
        // The following tests need to modify the document in significant ways, so we
        // use input_trigger_factory to create an input_trigger that does something
        // significant.
        it("does not try to act on undo/redo changes", () => {
            input_trigger_factory_1.makeSplitMergeInputTrigger(editor, mode, pSelector, key.makeKey(";"), key_constants_1.BACKSPACE, key_constants_1.DELETE);
            let ps = editor.dataRoot.querySelectorAll("body p");
            chai_1.assert.equal(ps.length, 1);
            editor.caretManager.setCaret(ps[0], 0);
            // Synthetic event
            const event = util_1.makeFakePasteEvent({
                types: ["text/plain"],
                getData: () => "ab;cd;ef",
            });
            editor.$guiRoot.trigger(event);
            ps = editor.dataRoot.querySelectorAll("body p");
            chai_1.assert.equal(ps.length, 3);
            chai_1.assert.equal(cleanNamespace(ps[0].outerHTML), "<p>ab</p>");
            chai_1.assert.equal(cleanNamespace(ps[1].outerHTML), "<p>cd</p>");
            chai_1.assert.equal(cleanNamespace(ps[2].outerHTML), "<p>efBlah blah <term>blah</term><term>blah2</term> blah.</p>", "first split: 3rd part");
            editor.undo();
            ps = editor.dataRoot.querySelectorAll("body p");
            chai_1.assert.equal(ps.length, 1);
            chai_1.assert.equal(cleanNamespace(ps[0].outerHTML), "<p>Blah blah <term>blah</term><term>blah2</term> blah.</p>", "after undo");
            editor.redo();
            ps = editor.dataRoot.querySelectorAll("body p");
            chai_1.assert.equal(ps.length, 3, "after redo: length");
            chai_1.assert.equal(cleanNamespace(ps[0].outerHTML), "<p>ab</p>", "after redo: 1st part");
            chai_1.assert.equal(cleanNamespace(ps[1].outerHTML), "<p>cd</p>", "after redo: 2nd part");
            chai_1.assert.equal(cleanNamespace(ps[2].outerHTML), "<p>efBlah blah <term>blah</term><term>blah2</term> blah.</p>", "after redo: 3rd part");
        });
        describe("respects mode regions", () => {
            let pInHeader;
            beforeEach(() => {
                pInHeader = editor.dataRoot.querySelector("teiHeader p");
                chai_1.assert.isDefined(pInHeader);
            });
            it("ignores paste events in the wrong region", () => {
                chai_1.assert.equal(pasteTest(pInHeader), 0);
            });
            it("ignores on keydown events in the wrong region", () => {
                chai_1.assert.equal(keydownTest(pInHeader), 0);
            });
            it("ignores on keypress events in the wrong region", () => {
                chai_1.assert.equal(keypressTest(pInHeader), 0);
            });
        });
    });
});
//  LocalWords:  InputTrigger keydown chai keypress tei Dubeau MPL Mangalam cd
//  LocalWords:  rng js
//# sourceMappingURL=input-trigger-test.js.map