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 __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", "merge-options", "../base-config", "../util", "../wed-test-util"], function (require, exports, merge_options_1, globalConfig, util_1, wed_test_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    merge_options_1 = __importDefault(merge_options_1);
    globalConfig = __importStar(globalConfig);
    const options = {
        schema: "/base/build/schemas/simplified-rng.js",
    };
    const assert = chai.assert;
    describe("wed wildcard support:", () => {
        let setup;
        let editor;
        let caretManager;
        let guiRoot;
        before(() => __awaiter(this, void 0, void 0, function* () {
            setup = new wed_test_util_1.EditorSetup(`${wed_test_util_1.dataPath}/wed_test_data/wildcard_converted.xml`, merge_options_1.default(globalConfig.config, options), document);
            ({ editor } = setup);
            yield setup.init();
            // tslint:disable-next-line:no-any
            editor.validator._validateUpTo(editor.dataRoot, -1);
            caretManager = editor.caretManager;
            guiRoot = editor.guiRoot;
        }));
        afterEach(() => {
            setup.reset();
        });
        after(() => {
            setup.restore();
            // tslint:disable-next-line:no-any
            editor = undefined;
            // tslint:disable-next-line:no-any
            caretManager = undefined;
        });
        it("elements and attributes allowed due to wildcards are readonly", () => {
            const bar = editor.dataRoot.querySelector("bar");
            const barGUI = caretManager.fromDataLocation(bar, 0).node;
            assert.isTrue(barGUI.classList.contains("_readonly"));
            const attrNames = wed_test_util_1.getAttributeNamesFor(barGUI);
            let attrName;
            for (let ix = 0; ix < attrNames.length; ++ix) {
                attrName = attrNames[ix];
                if (attrName.textContent === "foo:baz") {
                    break;
                }
            }
            // tslint:disable-next-line:no-unnecessary-type-assertion
            const attr = attrName.closest("._attribute");
            assert.isTrue(attr.classList.contains("_readonly"));
        });
        it("prevent typing in readonly elements and attributes", () => {
            const bar = editor.dataRoot.querySelector("bar");
            const barGUI = caretManager.fromDataLocation(bar, 0).node;
            assert.isTrue(barGUI.classList.contains("_readonly"));
            caretManager.setCaret(bar, 0);
            editor.type("foo");
            assert.equal(bar.textContent, "abc");
            const attrNames = wed_test_util_1.getAttributeNamesFor(barGUI);
            let attrName;
            for (let ix = 0; ix < attrNames.length; ++ix) {
                attrName = attrNames[ix];
                if (attrName.textContent === "foo:baz") {
                    break;
                }
            }
            // tslint:disable-next-line:no-unnecessary-type-assertion
            const attr = attrName.closest("._attribute");
            assert.isTrue(attr.classList.contains("_readonly"));
            const fooBaz = bar.getAttributeNode("foo:baz");
            caretManager.setCaret(fooBaz, 0);
            editor.type("foo");
            assert.equal(fooBaz.value, "x");
            // We drop the _readonly classes to make sure that we're testing what we
            // think we're testing. Note that the classes will be added right back as we
            // change the file because it is revalidated. This is why we type only one
            // character.
            barGUI.classList.remove("_readonly");
            attr.classList.remove("_readonly");
            caretManager.setCaret(fooBaz, 0);
            editor.type("f");
            assert.equal(fooBaz.value, "fx");
            barGUI.classList.remove("_readonly");
            caretManager.setCaret(bar, 0);
            editor.type("f");
            assert.equal(bar.textContent, "fabc");
        });
        describe("a context menu has the complex pattern action when invoked", () => {
            it("on an element allowed due to a complex pattern", () => __awaiter(this, void 0, void 0, function* () {
                let el;
                yield util_1.waitForSuccess(() => {
                    el = guiRoot.querySelector("._readonly ._element_name");
                    assert.isNotNull(el);
                });
                wed_test_util_1.activateContextMenu(editor, el);
                wed_test_util_1.contextMenuHasOption(editor, /Complex name pattern/, 1);
            }));
            it("on an attribute allowed due to a complex pattern", () => __awaiter(this, void 0, void 0, function* () {
                let el;
                yield util_1.waitForSuccess(() => {
                    el = guiRoot.querySelector("._readonly ._attribute_value");
                    assert.isNotNull(el);
                });
                wed_test_util_1.activateContextMenu(editor, el);
                wed_test_util_1.contextMenuHasOption(editor, /Complex name pattern/, 1);
            }));
        });
        function contextMenuHasNoTransforms() {
            const menu = editor.window.document.getElementsByClassName("wed-context-menu")[0];
            assert.isDefined(menu, "the menu should exist");
            const items = menu.querySelectorAll("li[data-kind]");
            assert.equal(items.length, 0, "there should be no items that can transform the document");
        }
        describe("a context menu invoked on a readonly", () => {
            it("element has no actions that can transform the document", () => __awaiter(this, void 0, void 0, function* () {
                let el;
                yield util_1.waitForSuccess(() => {
                    el = guiRoot.querySelector("._readonly ._element_name");
                    assert.isNotNull(el);
                });
                wed_test_util_1.activateContextMenu(editor, el);
                contextMenuHasNoTransforms();
            }));
            it("attribute has no actions that can transform the document", () => __awaiter(this, void 0, void 0, function* () {
                let el;
                yield util_1.waitForSuccess(() => {
                    el = guiRoot.querySelector("._readonly ._attribute_value");
                    assert.isNotNull(el);
                });
                wed_test_util_1.activateContextMenu(editor, el);
                contextMenuHasNoTransforms();
            }));
        });
    });
});
//# sourceMappingURL=wed-wildcard-test.js.map