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", "../base-config", "../util", "../wed-test-util"], function (require, exports, keyConstants, globalConfig, util_1, wed_test_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    keyConstants = __importStar(keyConstants);
    globalConfig = __importStar(globalConfig);
    const assert = chai.assert;
    describe("wed file state:", () => {
        let setup;
        let editor;
        let caretManager;
        let $modificationStatus;
        let $saveStatus;
        let titles;
        beforeEach(() => {
            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;
                // tslint:disable-next-line:no-any
                $modificationStatus = editor.$modificationStatus;
                // tslint:disable-next-line:no-any
                $saveStatus = editor.$saveStatus;
                titles = editor.guiRoot.getElementsByClassName("title");
            });
        });
        afterEach(() => {
            setup.restore();
        });
        it("modification status shows an unmodified document when starting", () => {
            assert.isTrue($modificationStatus.hasClass("label-success"));
        });
        it("save status shows an unsaved document when starting", () => {
            assert.isTrue($saveStatus.hasClass("label-default"));
            assert.equal($saveStatus.children("span").text(), "");
        });
        it("onbeforeunload returns falsy on unmodified doc", () => {
            assert.isFalse(!!editor.window.onbeforeunload
                // tslint:disable-next-line:no-any
                .call(editor.window, undefined));
        });
        it("modification status shows a modified document after modification", () => {
            // Text node inside title.
            const initial = titles[0].childNodes[1];
            caretManager.setCaret(initial, 0);
            editor.type(" ");
            assert.isTrue($modificationStatus.hasClass("label-warning"));
        });
        it("onbeforeunload returns truthy on modified doc", () => {
            // Text node inside title.
            const initial = titles[0].childNodes[1];
            caretManager.setCaret(initial, 0);
            editor.type(" ");
            assert.isTrue(!!editor.window.onbeforeunload
                // tslint:disable-next-line:no-any
                .call(editor.window, undefined));
        });
        it("modification status shows an unmodified document after save", () => {
            // Text node inside title.
            const initial = titles[0].childNodes[1];
            caretManager.setCaret(initial, 0);
            editor.type(" ");
            assert.isTrue($modificationStatus.hasClass("label-warning"));
            editor.type(keyConstants.SAVE);
            return util_1.waitForSuccess(() => {
                assert.isTrue($modificationStatus.hasClass("label-success"));
            });
        });
        it("save status shows a saved document after a save", () => {
            assert.isTrue($saveStatus.hasClass("label-default"));
            assert.equal($saveStatus.children("span").text(), "");
            editor.type(keyConstants.SAVE);
            return util_1.waitForSuccess(() => {
                assert.isTrue($saveStatus.hasClass("label-success"));
                assert.equal($saveStatus.children("span").text(), "moments ago");
                // We also check the tooltip text.
                assert.equal($saveStatus.data("bs.tooltip").getTitle(), "The last save was a manual save.");
            });
        });
        it("save status shows a saved document after an autosave", () => {
            assert.isTrue($saveStatus.hasClass("label-default"));
            assert.equal($saveStatus.children("span").text(), "");
            // Text node inside title.
            const initial = titles[0].childNodes[1];
            caretManager.setCaret(initial, 0);
            editor.type(" ");
            editor.saver.setAutosaveInterval(50);
            return util_1.waitForSuccess(() => {
                assert.isTrue($saveStatus.hasClass("label-info"));
                assert.equal($saveStatus.children("span").text(), "moments ago");
                // We also check the tooltip text.
                assert.equal($saveStatus.data("bs.tooltip").getTitle(), "The last save was an autosave.");
            });
        });
        it("save status tooltip updated after a different kind of save", () => __awaiter(this, void 0, void 0, function* () {
            // Text node inside title.
            const initial = titles[0].childNodes[1];
            caretManager.setCaret(initial, 0);
            editor.type(" ");
            editor.saver.setAutosaveInterval(50);
            yield util_1.waitForSuccess(() => {
                // We check the initial tooltip text.
                const tooltip = $saveStatus.data("bs.tooltip");
                assert.isDefined(tooltip);
                assert.equal(tooltip.getTitle(), "The last save was an autosave.");
            });
            // Now perform a save.
            editor.type(keyConstants.SAVE);
            return util_1.waitForSuccess(() => {
                // We check the tooltip changed.
                const tooltip = $saveStatus.data("bs.tooltip");
                assert.isDefined(tooltip);
                assert.equal(tooltip.getTitle(), "The last save was a manual save.");
            });
        }));
    });
});
//# sourceMappingURL=wed-file-state-test.js.map