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", "rxjs/operators", "wed", "../base-config", "../wed-test-util"], function (require, exports, operators_1, wed_1, globalConfig, wed_test_util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    globalConfig = __importStar(globalConfig);
    const assert = chai.assert;
    describe("wed save:", () => {
        let setup;
        let editor;
        let server;
        before(() => {
            setup = new wed_test_util_1.EditorSetup(`${wed_test_util_1.dataPath}/wed_test_data/server_interaction_converted.xml`, globalConfig.config, document);
            ({ editor, server } = setup);
            return setup.init();
        });
        afterEach(() => {
            setup.reset();
        });
        after(() => {
            setup.restore();
            // tslint:disable-next-line:no-any
            editor = undefined;
        });
        it("saves using the keyboard", () => {
            const prom = editor.saver.events
                .pipe(operators_1.filter((ev) => ev.name === "Saved"), operators_1.first()).toPromise()
                .then(() => {
                assert.deepEqual(server.lastSaveRequest, {
                    command: "save",
                    version: wed_1.version,
                    data: "<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\
<teiHeader><fileDesc><titleStmt><title>abcd</title></titleStmt>\
<publicationStmt><p/></publicationStmt><sourceDesc><p/></sourceDesc>\
</fileDesc></teiHeader><text><body><p>Blah blah <term>blah</term> blah.</p>\
<p><term>blah</term></p></body></text></TEI>",
                });
            });
            editor.type(wed_1.keyConstants.SAVE);
            return prom;
        });
        it("saves using the toolbar", () => {
            // We just check the event happened.
            const prom = editor.saver.events
                .pipe(operators_1.filter((ev) => ev.name === "Saved"), operators_1.first()).toPromise();
            const button = editor.widget
                .querySelector("[data-original-title='Save']");
            button.click();
            return prom;
        });
        it("serializes properly", () => {
            const prom = editor.saver.events
                .pipe(operators_1.filter((ev) => ev.name === "Saved"), operators_1.first()).toPromise()
                .then(() => {
                assert.deepEqual(server.lastSaveRequest, {
                    command: "save",
                    version: wed_1.version,
                    data: "<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\
<teiHeader><fileDesc><titleStmt><title>abcd</title></titleStmt>\
<publicationStmt><p><abbr/></p></publicationStmt><sourceDesc><p/></sourceDesc>\
</fileDesc></teiHeader><text><body><p>Blah blah <term>blah</term> blah.</p>\
<p><term>blah</term></p></body></text></TEI>",
                });
            });
            const p = editor.dataRoot.querySelector("p");
            editor.caretManager.setCaret(p, 0);
            const trs = editor.modeTree.getMode(p)
                .getContextualActions("insert", "abbr", p, 0);
            trs[0].execute({ name: "abbr" });
            editor.type(wed_1.keyConstants.SAVE);
            return prom;
        });
        it("does not autosave if not modified", (done) => {
            // tslint:disable-next-line:no-floating-promises
            editor.save().then(() => {
                const sub = editor.saver.events
                    .pipe(operators_1.filter((ev) => ev.name === "Autosaved")).subscribe(() => {
                    throw new Error("autosaved!");
                });
                editor.saver.setAutosaveInterval(50);
                setTimeout(() => {
                    sub.unsubscribe();
                    done();
                }, 500);
            });
        });
        it("autosaves when the document is modified", (done) => {
            // We're testing that autosave is not called again after the first time.
            let autosaved = false;
            const sub = editor.saver.events.pipe(operators_1.filter((x) => x.name === "Autosaved"))
                .subscribe(() => {
                if (autosaved) {
                    throw new Error("autosaved more than once");
                }
                autosaved = true;
                assert.deepEqual(server.lastSaveRequest, {
                    command: "autosave",
                    version: wed_1.version,
                    data: "<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\
<teiHeader><fileDesc><titleStmt><title>abcd</title></titleStmt>\
<publicationStmt/><sourceDesc><p/></sourceDesc>\
</fileDesc></teiHeader><text><body><p>Blah blah <term>blah</term> blah.</p>\
<p><term>blah</term></p></body></text></TEI>",
                });
                setTimeout(() => {
                    sub.unsubscribe();
                    done();
                }, 500);
            });
            editor.dataUpdater.removeNode(editor.dataRoot.querySelector("p"));
            editor.saver.setAutosaveInterval(50);
        });
        it("autosaves when the document is modified after a first autosave timeout " +
            "that did nothing", (done) => {
            // tslint:disable-next-line:no-floating-promises
            editor.save().then(() => {
                // We're testing that autosave is not called again after the first
                // time.
                let autosaved = false;
                const interval = 50;
                const sub = editor.saver.events
                    .pipe(operators_1.filter((x) => x.name === "Autosaved"))
                    .subscribe(() => {
                    if (autosaved) {
                        throw new Error("autosaved more than once");
                    }
                    autosaved = true;
                    assert.deepEqual(server.lastSaveRequest, {
                        command: "autosave",
                        version: wed_1.version,
                        data: "<TEI xmlns=\"http://www.tei-c.org/ns/1.0\">\
<teiHeader><fileDesc><titleStmt><title>abcd</title></titleStmt>\
<publicationStmt/><sourceDesc><p/></sourceDesc>\
</fileDesc></teiHeader><text><body><p>Blah blah <term>blah</term> blah.</p>\
<p><term>blah</term></p></body></text></TEI>",
                    });
                    setTimeout(() => {
                        sub.unsubscribe();
                        done();
                    }, interval * 2);
                });
                editor.saver.setAutosaveInterval(interval);
                setTimeout(() => {
                    assert.isFalse(autosaved, "should not have been saved yet");
                    editor.dataUpdater.removeNode(editor.dataRoot.querySelector("p"));
                }, interval * 2);
            });
        });
    });
});
//# sourceMappingURL=wed-save-test.js.map