"use strict";
// The save data manager has a feature where certain variables are automatically restored to default
// values at certain times.
Object.defineProperty(exports, "__esModule", { value: true });
exports.restoreDefaultForFeatureKey = exports.restoreDefaultsForAllFeaturesKey = exports.restoreDefaultsForAllFeaturesAndKeys = void 0;
const SaveDataKey_1 = require("../../../../enums/SaveDataKey");
const SerializationType_1 = require("../../../../enums/SerializationType");
const deepCopy_1 = require("../../../../functions/deepCopy");
const log_1 = require("../../../../functions/log");
const table_1 = require("../../../../functions/table");
const ReadonlySet_1 = require("../../../../types/ReadonlySet");
const constants_1 = require("./constants");
const RESETTABLE_SAVE_DATA_KEYS = new ReadonlySet_1.ReadonlySet([
    SaveDataKey_1.SaveDataKey.RUN,
    SaveDataKey_1.SaveDataKey.LEVEL,
    SaveDataKey_1.SaveDataKey.ROOM,
]);
function restoreDefaultsForAllFeaturesAndKeys(saveDataMap, saveDataDefaultsMap) {
    for (const saveDataKey of RESETTABLE_SAVE_DATA_KEYS) {
        restoreDefaultsForAllFeaturesKey(saveDataMap, saveDataDefaultsMap, saveDataKey);
    }
}
exports.restoreDefaultsForAllFeaturesAndKeys = restoreDefaultsForAllFeaturesAndKeys;
function restoreDefaultsForAllFeaturesKey(saveDataMap, saveDataDefaultsMap, saveDataKey) {
    (0, table_1.iterateTableInOrder)(saveDataMap, (subscriberName, saveData) => {
        restoreDefaultForFeatureKey(saveDataDefaultsMap, subscriberName, saveData, saveDataKey);
    }, constants_1.SAVE_DATA_MANAGER_DEBUG);
}
exports.restoreDefaultsForAllFeaturesKey = restoreDefaultsForAllFeaturesKey;
function restoreDefaultForFeatureKey(saveDataDefaultsMap, subscriberName, saveData, saveDataKey) {
    // Only allow certain save data keys to be reset.
    if (!RESETTABLE_SAVE_DATA_KEYS.has(saveDataKey)) {
        error(`Failed to restore default values for a save data key of "${saveDataKey}", since it is not on the allowed list of resettable save data keys.`);
    }
    const childTable = saveData[saveDataKey];
    if (childTable === undefined) {
        // This feature does not happen to store any variables on this particular child table.
        return;
    }
    // Get the default values for this feature.
    const saveDataDefaults = saveDataDefaultsMap.get(subscriberName);
    if (saveDataDefaults === undefined) {
        (0, log_1.logError)(`Failed to find the default copy of the save data for subscriber: ${subscriberName}`);
        return;
    }
    // Get the default values for the specific sub-table of this feature.
    const childTableDefaults = saveDataDefaults[saveDataKey];
    if (childTableDefaults === undefined) {
        (0, log_1.logError)(`Failed to find the default copy of the child table "${saveDataKey}" for subscriber "${subscriberName}". This error usually means that your mod-specific save data is out of date. You can try purging all of your mod-specific save data by deleting the following directory: C:\\Program Files (x86)\\Steam\\steamapps\\common\\The Binding of Isaac Rebirth\\data`);
        return;
    }
    // Make a new copy of the default child table.
    const childTableDefaultsCopy = (0, deepCopy_1.deepCopy)(childTableDefaults, SerializationType_1.SerializationType.NONE, `${subscriberName} --> ${saveDataKey}`);
    // We do not want to blow away the existing child table because we do not want to break any
    // existing references. Instead, empty the table and copy all of the elements from the copy of the
    // defaults table.
    clearAndCopyAllElements(childTable, childTableDefaultsCopy);
}
exports.restoreDefaultForFeatureKey = restoreDefaultForFeatureKey;
/**
 * Will empty the old table of all elements, and then shallow copy all the elements from the new
 * table to the old table.
 */
function clearAndCopyAllElements(oldTable, newTable) {
    (0, table_1.clearTable)(oldTable);
    for (const [key, value] of newTable) {
        oldTable.set(key, value);
    }
}
