"use strict";
/**
 * Configuration loader
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfLoader = void 0;
/**
 * Load system configuration from JSON
 *
 * @example
 *   ConfLoader( require( 'fs' ), SomeStore )
 *     .fromFile( 'conf/vanilla-server.json' )
 *     .then( conf => conf.get( 'foo' ) );
 *
 * TODO: Merging multiple configuration files would be convenient for
 * modular configuration.
 */
var ConfLoader = /** @class */ (function () {
    /**
     * Initialize with provided filesystem module and Store constructor
     *
     * The module should implement `#readFile` compatible with
     * Node.js'.  The Store constructor `store_ctor` is used to instantiate
     * new stores to be populated with configuration data.
     *
     * @param fs         - filesystem module
     * @param store_ctor - Store object constructor
     */
    function ConfLoader(_fs, _storeCtor) {
        this._fs = _fs;
        this._storeCtor = _storeCtor;
    }
    /**
     * Produce configuration from file
     *
     * A Store will be produced, populated with the configuration data.
     *
     * @param filename - path to configuration JSON
     *
     * @return a promise of a populated Store
     */
    ConfLoader.prototype.fromFile = function (filename) {
        var _this = this;
        return new Promise(function (resolve, reject) {
            _this._fs.readFile(filename, 'utf8', function (err, data) {
                if (err) {
                    reject(err);
                    return;
                }
                try {
                    var store_1 = _this._storeCtor();
                    resolve(_this.parseConfData(data)
                        .then(function (parsed) { return store_1.populate(parsed); })
                        .then(function (_) { return store_1; }));
                }
                catch (e) {
                    reject(e);
                }
            });
        });
    };
    /**
     * Parse raw configuration string
     *
     * Parses configuration string as JSON.
     *
     * @param data raw configuration data
     *
     * @return `data` parsed as JSON
     */
    ConfLoader.prototype.parseConfData = function (data) {
        return Promise.resolve(JSON.parse(data));
    };
    return ConfLoader;
}());
exports.ConfLoader = ConfLoader;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiQ29uZkxvYWRlci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIkNvbmZMb2FkZXIudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBOzs7Ozs7Ozs7Ozs7Ozs7Ozs7O0dBbUJHOzs7QUFLSDs7Ozs7Ozs7OztHQVVHO0FBQ0g7SUFDRTs7Ozs7Ozs7O09BU0c7SUFDSCxvQkFDVSxHQUFnQyxFQUNoQyxVQUF1QjtRQUR2QixRQUFHLEdBQUgsR0FBRyxDQUE2QjtRQUNoQyxlQUFVLEdBQVYsVUFBVSxDQUFhO0lBQzlCLENBQUM7SUFFSjs7Ozs7Ozs7T0FRRztJQUNILDZCQUFRLEdBQVIsVUFBUyxRQUFnQjtRQUF6QixpQkFxQkM7UUFwQkMsT0FBTyxJQUFJLE9BQU8sQ0FBQyxVQUFDLE9BQU8sRUFBRSxNQUFNO1lBQ2pDLEtBQUksQ0FBQyxHQUFHLENBQUMsUUFBUSxDQUFDLFFBQVEsRUFBRSxNQUFNLEVBQUUsVUFBQyxHQUFHLEVBQUUsSUFBSTtnQkFDNUMsSUFBSSxHQUFHLEVBQUU7b0JBQ1AsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDO29CQUNaLE9BQU87aUJBQ1I7Z0JBRUQsSUFBSTtvQkFDRixJQUFNLE9BQUssR0FBRyxLQUFJLENBQUMsVUFBVSxFQUFFLENBQUM7b0JBRWhDLE9BQU8sQ0FDTCxLQUFJLENBQUMsYUFBYSxDQUFDLElBQUksQ0FBQzt5QkFDckIsSUFBSSxDQUFDLFVBQUEsTUFBTSxJQUFJLE9BQUEsT0FBSyx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