var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
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/key-constants", "wed/util", "../base-config", "../util", "../wed-test-util"], function (require, exports, keyConstants, util_1, globalConfig, util_2, wed_test_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    keyConstants = __importStar(keyConstants);
    globalConfig = __importStar(globalConfig);
    const assert = chai.assert;
    describe("wed menus:", () => {
        let setup;
        let editor;
        let caretManager;
        let ps;
        let guiRoot;
        let menuManager;
        before(() => {
            setup = new wed_test_util_1.EditorSetup(`${wed_test_util_1.dataPath}/wed_test_data/source_converted.xml`, globalConfig.config, document);
            ({ editor } = setup);
            return setup.init().then(() => {
                // tslint:disable-next-line:no-any
                editor.validator._validateUpTo(editor.dataRoot, -1);
                caretManager = editor.caretManager;
                guiRoot = editor.guiRoot;
                ps = guiRoot.querySelectorAll(".body .p");
                menuManager = editor.editingMenuManager;
            });
        });
        afterEach(() => {
            setup.reset();
        });
        after(() => {
            setup.restore();
            // tslint:disable-next-line:no-any
            editor = undefined;
            // tslint:disable-next-line:no-any
            caretManager = undefined;
        });
        function contextMenuHasAttributeOption(myEditor) {
            wed_test_util_1.contextMenuHasOption(myEditor, /^Add @/);
        }
        describe("has context menus", () => {
            it("when there is no caret", () => __awaiter(this, void 0, void 0, function* () {
                const initial = guiRoot.getElementsByClassName("title")[0].childNodes[1];
                assert.isUndefined(caretManager.getNormalizedCaret());
                wed_test_util_1.activateContextMenu(editor, initial.parentNode);
                yield util_2.delay(1);
                // tslint:disable-next-line:no-any
                assert.isDefined(menuManager.currentDropdown, "dropdown defined");
                assert.isDefined(caretManager.getNormalizedCaret(), "caret defined");
            }));
            it("when the user tries to bring up a contextual menu when the caret is " +
                "outside wed", () => __awaiter(this, void 0, void 0, function* () {
                caretManager.clearSelection(); // Also clears the caret.
                assert.isUndefined(caretManager.getNormalizedCaret());
                wed_test_util_1.activateContextMenu(editor, guiRoot.getElementsByClassName("title")[0]);
                yield util_2.delay(1);
                // tslint:disable-next-line:no-any
                assert.isDefined(menuManager.currentDropdown);
            }));
            it("when there is a caret", () => __awaiter(this, void 0, void 0, function* () {
                const initial = guiRoot.getElementsByClassName("title")[0].childNodes[1];
                caretManager.setCaret(initial, 0);
                wed_test_util_1.activateContextMenu(editor, initial.parentNode);
                yield util_2.delay(1);
                // tslint:disable-next-line:no-any
                assert.isDefined(menuManager.currentDropdown);
            }));
            it("with attribute options, when invoked on a start label", () => {
                wed_test_util_1.activateContextMenu(editor, guiRoot.querySelector(".__start_label._title_label ._element_name"));
                contextMenuHasAttributeOption(editor);
            });
            it("with attribute options, when invoked in an attribute", () => {
                wed_test_util_1.activateContextMenu(editor, guiRoot.querySelector(".__start_label._p_label ._attribute_value"));
                contextMenuHasAttributeOption(editor);
            });
            it("on elements inside _phantom_wrap", () => {
                const p = guiRoot.querySelector(`.body .p[${util_1.encodeAttrName("rend")}='wrap']`);
                const dataNode = $.data(p, "wed_mirror_node");
                const rend = dataNode.getAttribute("rend");
                // Make sure the paragraph has rend="wrap".
                assert.equal(rend, "wrap");
                wed_test_util_1.activateContextMenu(editor, p);
            });
        });
        describe("has a completion menu when the caret is in an attribute", () => {
            it("that takes completions", () => {
                const p = ps[9];
                const attrVals = wed_test_util_1.getAttributeValuesFor(p);
                caretManager.setCaret(attrVals[0].firstChild, 0);
                // This is an arbitrary menu item we check for.
                wed_test_util_1.contextMenuHasOption(editor, /^Y$/);
            });
            it("for which the mode provides completion", () => {
                const p = ps[13];
                const attrVals = wed_test_util_1.getAttributeValuesFor(p);
                caretManager.setCaret(attrVals[0].firstChild, 0);
                // This is an arbitrary menu item we check for.
                wed_test_util_1.contextMenuHasOption(editor, /^completion1$/);
            });
        });
        describe("does not have completion menu", () => {
            it("when the caret is in an attribute that takes completions but the " +
                "attribute is not visible", () => {
                // Reduce visibility to 0 so that no attribute is visible.
                editor.setLabelVisibilityLevel(0);
                const attrVals = wed_test_util_1.getAttributeValuesFor(ps[9]);
                caretManager.setCaret(attrVals[0].firstChild, 0);
                const menu = editor.window.document.
                    getElementsByClassName("wed-context-menu")[0];
                assert.isUndefined(menu, "the menu should not exist");
            });
        });
        describe("has a replacement menu when the caret is in an attribute", () => {
            it("that takes completions", () => {
                const p = ps[9];
                const attrVals = wed_test_util_1.getAttributeValuesFor(p);
                caretManager.setCaret(attrVals[0].firstChild, 0);
                wed_test_util_1.contextMenuHasOption(editor, /^Y$/);
                editor.type("Y");
                // The context menu should be gone.
                const menu = editor.window.document.
                    getElementsByClassName("wed-context-menu")[0];
                assert.isUndefined(menu, "the menu should not exist");
                editor.type(keyConstants.REPLACEMENT_MENU);
                wed_test_util_1.contextMenuHasOption(editor, /^Y$/);
            });
            it("for which the mode provides completion", () => {
                const p = ps[13];
                const attrVals = wed_test_util_1.getAttributeValuesFor(p);
                caretManager.setCaret(attrVals[0].firstChild, 0);
                // This is an arbitrary menu item we check for.
                wed_test_util_1.contextMenuHasOption(editor, /^completion1$/);
                editor.type("completion1");
                // The context menu should be gone.
                const menu = editor.window.document.
                    getElementsByClassName("wed-context-menu")[0];
                assert.isUndefined(menu, "the menu should not exist");
                editor.type(keyConstants.REPLACEMENT_MENU);
                wed_test_util_1.contextMenuHasOption(editor, /^completion1$/);
            });
        });
        describe("does not have a replacement menu", () => {
            it("when the caret is in an attribute that takes completions but the " +
                "attribute is not visible", () => {
                // Reduce visibility to 0 so that no attribute is visible.
                editor.setLabelVisibilityLevel(0);
                const attrVals = wed_test_util_1.getAttributeValuesFor(ps[9]);
                caretManager.setCaret(attrVals[0].firstChild, 0);
                let menu = editor.window.document.
                    getElementsByClassName("wed-context-menu")[0];
                assert.isUndefined(menu, "the menu should not exist");
                // The menu won't come up with a the shortcut.
                editor.type(keyConstants.REPLACEMENT_MENU);
                menu = editor.window.document.
                    getElementsByClassName("wed-context-menu")[0];
                assert.isUndefined(menu, "the menu should not exist");
            });
        });
    });
});
//# sourceMappingURL=wed-menu-test.js.map