"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runMergeTests = void 0;
const DefaultMap_1 = require("../classes/DefaultMap");
const SerializationType_1 = require("../enums/SerializationType");
const deepCopy_1 = require("./deepCopy");
const log_1 = require("./log");
const merge_1 = require("./merge");
const rng_1 = require("./rng");
const serialization_1 = require("./serialization");
const vector_1 = require("./vector");
/**
 * Run the suite of tests that prove that the "merge" function works properly.
 *
 * This function is only useful if you are troubleshooting the save data manager.
 */
function runMergeTests() {
    oldTableHasUpdatedValue();
    newTableHasSameValue();
    oldTableHasUpdatedValueFromNull();
    oldTableHasSerializedIsaacAPIClass();
    oldTableHasFilledChildTable();
    oldTableHasFilledMap();
    oldTableHasFilledDefaultMap();
    oldTableHasVector();
    oldTableHasVectorSerialized();
    oldTableHasRNG();
    oldTableHasRNGSerialized();
    const successText = "All merge tests passed!";
    (0, log_1.logAndPrint)(successText);
}
exports.runMergeTests = runMergeTests;
function oldTableHasUpdatedValue() {
    const key = "foo";
    const oldValue = "bar";
    const newValue = "baz";
    const oldTable = {
        foo: oldValue,
    };
    const newTable = {
        foo: newValue,
    };
    (0, merge_1.merge)(oldTable, newTable, "oldTableHasUpdatedValue");
    const oldTableValue = oldTable.get(key);
    if (oldTableValue !== newValue) {
        error(`The old table does not have a value of: ${newValue}`);
    }
}
function newTableHasSameValue() {
    const key = "foo";
    const oldValue = "bar";
    const newValue = "baz";
    const oldTable = {
        foo: oldValue,
    };
    const newTable = {
        foo: newValue,
    };
    (0, merge_1.merge)(oldTable, newTable, "newTableHasSameValue");
    const newTableValue = newTable.get(key);
    if (newTableValue !== newValue) {
        error(`The new table does not have a value of: ${newValue}`);
    }
}
function oldTableHasUpdatedValueFromNull() {
    const key = "foo";
    const newValue = "baz";
    const oldTable = {
        foo: null,
    };
    const newTable = {
        foo: newValue,
    };
    (0, merge_1.merge)(oldTable, newTable, "oldTableHasUpdatedValueFromNull");
    const oldTableValue = oldTable.get(key);
    if (oldTableValue !== newValue) {
        error(`The old table does not have a value of: ${newValue}`);
    }
}
function oldTableHasSerializedIsaacAPIClass() {
    const x = 50;
    const y = 60;
    const vector = Vector(x, y);
    const vectorSerialized = (0, vector_1.serializeVector)(vector);
    if (!(0, serialization_1.isSerializedIsaacAPIClass)(vectorSerialized)) {
        error('The "isSerializedIsaacAPIClass" function says that a serialized vector is not serialized.');
    }
}
function oldTableHasFilledChildTable() {
    const key = "foo";
    const newValue = "baz";
    const oldTable = {
        foo: null,
    };
    const foo = {
        bar: newValue,
    };
    const newTable = {
        foo,
    };
    (0, merge_1.merge)(oldTable, newTable, "oldTableHasFilledChildTable");
    const oldTableValue = oldTable.get(key);
    if (oldTableValue === undefined) {
        error(`The old table's key of "${key}" was not filled.`);
    }
    if (oldTableValue.bar !== newValue) {
        error('The old table\'s key of "bar" was not filled.');
    }
}
function oldTableHasFilledMap() {
    const fakeV = {
        run: {
            myMap: new Map(),
        },
    };
    const saveData = {
        run: {
            myMap: new Map([
                ["foo1", "bar1"],
                ["foo2", "bar2"],
                ["foo3", "bar3"],
            ]),
        },
    };
    const serializedSaveData = (0, deepCopy_1.deepCopy)(saveData, SerializationType_1.SerializationType.SERIALIZE);
    (0, merge_1.merge)(fakeV, serializedSaveData, "oldTableHasFilledMap");
    const expectedSize = 3;
    if (fakeV.run.myMap.size !== expectedSize) {
        error(`The size of the merged map was equal to ${fakeV.run.myMap.size}, but it should be equal to: ${expectedSize}`);
    }
    {
        const key = "foo1";
        const expectedValue = "bar1";
        const value = fakeV.run.myMap.get(key);
        if (value !== expectedValue) {
            error(`The old table's map key of "${key}" was not equal to "${expectedValue}" and was instead equal to: ${value}`);
        }
    }
    {
        const key = "foo2";
        const expectedValue = "bar2";
        const value = fakeV.run.myMap.get(key);
        if (value !== expectedValue) {
            error(`The old table's map key of "${key}" was not equal to "${expectedValue}" and was instead equal to: ${value}`);
        }
    }
    {
        const key = "foo3";
        const expectedValue = "bar3";
        const value = fakeV.run.myMap.get(key);
        if (value !== expectedValue) {
            error(`The old table's map key of "${key}" was not equal to "${expectedValue}" and was instead equal to: ${value}`);
        }
    }
}
function oldTableHasFilledDefaultMap() {
    const fakeV = {
        run: {
            myDefaultMap: new DefaultMap_1.DefaultMap("default"),
        },
    };
    const saveData = {
        run: {
            myDefaultMap: new DefaultMap_1.DefaultMap("default", [
                ["foo1", "bar1"],
                ["foo2", "bar2"],
                ["foo3", "bar3"],
            ]),
        },
    };
    const serializedSaveData = (0, deepCopy_1.deepCopy)(saveData, SerializationType_1.SerializationType.SERIALIZE);
    (0, merge_1.merge)(fakeV, serializedSaveData, "oldTableHasFilledDefaultMap");
    const expectedSize = 3;
    if (fakeV.run.myDefaultMap.size !== expectedSize) {
        error(`The size of the merged default map was equal to ${fakeV.run.myDefaultMap.size}, but it should be equal to: ${expectedSize}`);
    }
    {
        const key = "foo1";
        const expectedValue = "bar1";
        const value = fakeV.run.myDefaultMap.get(key);
        if (value !== expectedValue) {
            error(`The old table's default map key of "${key}" was not equal to "${expectedValue}" and was instead equal to: ${value}`);
        }
    }
    {
        const key = "foo2";
        const expectedValue = "bar2";
        const value = fakeV.run.myDefaultMap.get(key);
        if (value !== expectedValue) {
            error(`The old table's default map key of "${key}" was not equal to "${expectedValue}" and was instead equal to: ${value}`);
        }
    }
    {
        const key = "foo3";
        const expectedValue = "bar3";
        const value = fakeV.run.myDefaultMap.get(key);
        if (value !== expectedValue) {
            error(`The old table's default map key of "${key}" was not equal to "${expectedValue}" and was instead equal to: ${value}`);
        }
    }
}
function oldTableHasVector() {
    const key = "foo";
    const x = 50;
    const y = 60;
    const newValue = Vector(x, y);
    const oldTable = {
        foo: null,
    };
    const foo = {
        bar: newValue,
    };
    const newTable = {
        foo,
    };
    (0, merge_1.merge)(oldTable, newTable, "oldTableHasVector");
    const oldTableValue = oldTable.get(key);
    if (oldTableValue === undefined) {
        error(`The old table's key of "${key}" was not filled.`);
    }
    if (oldTableValue.bar.X !== x) {
        error(`The old table's value for "x" does not match: ${x}`);
    }
    if (oldTableValue.bar.Y !== y) {
        error(`The old table's value for "y" does not match: ${y}`);
    }
    if (!(0, vector_1.isVector)(oldTableValue.bar)) {
        error("The old table's value is not a Vector object.");
    }
}
function oldTableHasVectorSerialized() {
    const key = "foo";
    const x = 50;
    const y = 60;
    const newValue = Vector(x, y);
    const oldTable = {
        foo: null,
    };
    const foo = {
        bar: newValue,
    };
    const newTable = {
        foo,
    };
    const newTableSerialized = (0, deepCopy_1.deepCopy)(newTable, SerializationType_1.SerializationType.SERIALIZE, "oldTableHasVectorSerialized");
    (0, merge_1.merge)(oldTable, newTableSerialized, "oldTableHasVectorSerialized");
    const oldTableValue = oldTable.get(key);
    if (oldTableValue === undefined) {
        error(`The old table's key of "${key}" was not filled.`);
    }
    if (oldTableValue.bar.X !== x) {
        error(`The old table's value for "x" does not match: ${x}`);
    }
    if (oldTableValue.bar.Y !== y) {
        error(`The old table's value for "y" does not match: ${y}`);
    }
    if (!(0, vector_1.isVector)(oldTableValue.bar)) {
        error("The old table's value is not a Vector object (during the serialized test).");
    }
}
function oldTableHasRNG() {
    const key = "foo";
    const seed = 50;
    const newValue = (0, rng_1.newRNG)(seed);
    const oldTable = {
        foo: null,
    };
    const foo = {
        bar: newValue,
    };
    const newTable = {
        foo,
    };
    (0, merge_1.merge)(oldTable, newTable, "oldTableHasRNG");
    const oldTableValue = oldTable.get(key);
    if (oldTableValue === undefined) {
        error(`The old table's key of "${key}" was not filled.`);
    }
    if (!(0, rng_1.isRNG)(oldTableValue.bar)) {
        error("The old table's value is not an RNG object.");
    }
    const newSeed = oldTableValue.bar.GetSeed();
    if (newSeed !== seed) {
        error(`The old table's seed not match: ${seed}`);
    }
}
function oldTableHasRNGSerialized() {
    const key = "foo";
    const seed = 50;
    const newValue = (0, rng_1.newRNG)(seed);
    const oldTable = {
        foo: null,
    };
    const foo = {
        bar: newValue,
    };
    const newTable = {
        foo,
    };
    const newTableSerialized = (0, deepCopy_1.deepCopy)(newTable, SerializationType_1.SerializationType.SERIALIZE, "oldTableHasRNGSerialized");
    (0, merge_1.merge)(oldTable, newTableSerialized, "oldTableHasRNGSerialized");
    const oldTableValue = oldTable.get(key);
    if (oldTableValue === undefined) {
        error(`The old table's key of "${key}" was not filled.`);
    }
    if (!(0, rng_1.isRNG)(oldTableValue.bar)) {
        error("The old table's value is not an RNG object (during the serialized test).");
    }
    const newSeed = oldTableValue.bar.GetSeed();
    if (newSeed !== seed) {
        error(`The old table's seed not match: ${seed}`);
    }
}
