"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const Util_1 = require("../Util");
const ObjectParser_1 = require("../utils/ObjectParser");
class AppConfig {
    /** Read and parse the config file asynchronously */
    static readConfigFile(onError) {
        return new Promise((resolve, reject) => {
            Util_1.readJsonFile(AppConfig.filePath, AppConfig.fileEncoding)
                .then(json => resolve(AppConfig.parseData(json, AppConfig.getDefaults(process.platform), onError)))
                .catch(reject);
        });
    }
    /** Stringify and save the config file asynchronously */
    static saveConfigFile(data) {
        return new Promise((resolve, reject) => {
            // Convert config to json string
            const json = AppConfig.stringifyData(data);
            // Save the config file
            fs.writeFile(AppConfig.filePath, json, function (error) {
                if (error) {
                    return reject(error);
                }
                else {
                    return resolve();
                }
            });
        });
    }
    /**
     * Parse and object as an app config data object
     * (Extract the valid settings, and use the default values for everything else, then return a new object with these settings combined)
     */
    static parseData(data, defaultData, onError) {
        const parsed = Util_1.deepCopy(defaultData);
        const parser = new ObjectParser_1.ObjectParser({
            input: data,
            onError: onError ? (error => onError(`Error while parsing Config: ${error.toString()}`)) : noop
        });
        parser.prop('flashpointPath', v => parsed.flashpointPath = str(v));
        parser.prop('imageFolderPath', v => parsed.imageFolderPath = str(v));
        parser.prop('logoFolderPath', v => parsed.logoFolderPath = str(v));
        parser.prop('playlistFolderPath', v => parsed.playlistFolderPath = str(v));
        parser.prop('jsonFolderPath', v => parsed.jsonFolderPath = str(v));
        parser.prop('useCustomTitlebar', v => parsed.useCustomTitlebar = !!v);
        parser.prop('startRouter', v => parsed.startRouter = !!v);
        parser.prop('startRedirector', v => parsed.startRedirector = !!v);
        parser.prop('useFiddler', v => parsed.useFiddler = !!v);
        parser.prop('disableExtremeGames', v => parsed.disableExtremeGames = !!v);
        parser.prop('showBrokenGames', v => parsed.showBrokenGames = !!v);
        // Do some alterations
        parsed.flashpointPath = parsed.flashpointPath.replace(/\\/g, '/'); // (Clean path)
        // Return
        return parsed;
    }
    /** Serialize an app config data object into a string */
    static stringifyData(data) {
        return Util_1.stringifyJsonDataFile(data);
    }
    /** Get the default config data for a specified platform */
    static getDefaults(platform) {
        return AppConfig.configDataDefaults[platform] || AppConfig.configDataDefaultBase;
    }
    /** Create and return a copy of the default config data for a specified platform */
    static createCopyOfDefaults(platform) {
        return Util_1.deepCopy(AppConfig.getDefaults(platform));
    }
}
/** Path to the config file */
AppConfig.filePath = './config.json';
/** Encoding used by config file */
AppConfig.fileEncoding = 'utf8';
/**
 * Configs which all default config settings are based on
 * (Also used as default for any platform thats not listed in "configDataDefaults")
 */
AppConfig.configDataDefaultBase = Object.freeze({
    flashpointPath: '',
    imageFolderPath: 'Data/Images',
    logoFolderPath: 'Data/Logos',
    playlistFolderPath: 'Data/Playlists',
    jsonFolderPath: 'Data',
    themeFolderPath: 'Data/Themes',
    useCustomTitlebar: false,
    startRouter: true,
    startRedirector: true,
    useFiddler: false,
    disableExtremeGames: false,
    showBrokenGames: false,
});
/**
 * Default configurations for each platform
 * (Any platform that is not listed here will use "configDataDefaultBase" as their default)
 */
AppConfig.configDataDefaults = {
    // Windows
    win32: Object.freeze(AppConfig.parseData({
        useCustomTitlebar: true,
    }, AppConfig.configDataDefaultBase)),
    // Linux
    linux: Object.freeze(AppConfig.parseData({
        useCustomTitlebar: false,
    }, AppConfig.configDataDefaultBase)),
};
exports.AppConfig = AppConfig;
/** Coerce anything to a string */
function str(str) {
    return (str || '') + '';
}
function noop() { }
//# sourceMappingURL=AppConfigFile.js.map