"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const BrowsePageLayout_1 = require("../BrowsePageLayout");
const ObjectParser_1 = require("../utils/ObjectParser");
const util_1 = require("../order/util");
/** Default Preferences Data used for values that are not found in the file */
exports.defaultPreferencesData = Object.freeze({
    browsePageGameScale: 0.087,
    browsePageShowExtreme: false,
    enableEditing: true,
    browsePageLayout: BrowsePageLayout_1.BrowsePageLayout.grid,
    browsePageShowLeftSidebar: true,
    browsePageShowRightSidebar: true,
    browsePageLeftSidebarWidth: 320,
    browsePageRightSidebarWidth: 320,
    showDeveloperTab: false,
    useWine: false,
    currentTheme: undefined,
    lastSelectedLibrary: '',
    gamesOrderBy: 'title',
    gamesOrder: 'ascending',
    mainWindow: Object.freeze({
        x: undefined,
        y: undefined,
        width: undefined,
        height: undefined,
        maximized: false,
    }),
    showLogSource: Object.freeze({
    // (Add log sources that should be hidden by default here)
    }),
});
/**
 * Overwrite a preferences data object with data from another object
 * @param source Object to overwrite
 * @param data Object with data to overwrite the source with
 * @returns source object (not a copy)
 */
function overwritePreferenceData(source, data, onError) {
    //
    const parser = new ObjectParser_1.ObjectParser({
        input: data,
        onError: onError ? (error => onError(`Error while parsing Config: ${error.toString()}`)) : noop
    });
    parser.prop('browsePageGameScale', v => source.browsePageGameScale = num(v));
    parser.prop('browsePageShowExtreme', v => source.browsePageShowExtreme = !!v);
    parser.prop('enableEditing', v => source.enableEditing = !!v);
    parser.prop('browsePageLayout', v => source.browsePageLayout = num(v));
    parser.prop('browsePageShowLeftSidebar', v => source.browsePageShowLeftSidebar = !!v);
    parser.prop('browsePageShowRightSidebar', v => source.browsePageShowRightSidebar = !!v);
    parser.prop('browsePageLeftSidebarWidth', v => source.browsePageLeftSidebarWidth = num(v));
    parser.prop('browsePageRightSidebarWidth', v => source.browsePageRightSidebarWidth = num(v));
    parser.prop('showDeveloperTab', v => source.showDeveloperTab = !!v);
    parser.prop('useWine', v => source.useWine = !!v);
    parser.prop('currentTheme', v => source.currentTheme = str(v), true);
    parser.prop('lastSelectedLibrary', v => source.lastSelectedLibrary = str(v));
    parser.prop('gamesOrderBy', v => source.gamesOrderBy = strOpt(v, util_1.gameOrderByOptions, 'title'));
    parser.prop('gamesOrder', v => source.gamesOrder = strOpt(v, util_1.gameOrderReverseOptions, 'ascending'));
    parseMainWindow(parser.prop('mainWindow'), source.mainWindow);
    parser.prop('showLogSource').mapRaw((item, label) => source.showLogSource[label] = !!item);
    return source;
}
exports.overwritePreferenceData = overwritePreferenceData;
function parseMainWindow(parser, output) {
    parser.prop('x', v => output.x = num(v), true);
    parser.prop('y', v => output.y = num(v), true);
    parser.prop('width', v => output.width = num(v), true);
    parser.prop('height', v => output.height = num(v), true);
    parser.prop('maximized', v => output.maximized = !!v);
}
function num(n) {
    return parseFloat(n) || 0;
}
function str(str) {
    return (str || '') + '';
}
function strOpt(text, options, defaultOption) {
    text = str(text);
    for (let option of options) {
        if (text === option) {
            return text;
        }
    }
    return defaultOption;
}
function noop() { }
//# sourceMappingURL=util.js.map