Object.defineProperty(exports, "__esModule", { value: true });
exports.DeckManager = void 0;
var path = require("path");
var Logger_1 = require("../Logger");
var StreamDeck = require("@elgato-stream-deck/node");
var Deck_1 = require("./Deck");
var fs = require("fs-extra");
var writeFile = fs.writeFile, pathExists = fs.pathExists, readJson = fs.readJson;
var DeckManager = /** @class */ (function () {
    function DeckManager() {
    }
    DeckManager.prototype.load = function (callback) {
        var _this = this;
        this.managerConfigPath = path.join(Undecked.dataPath, 'decks.json');
        this.decks = {};
        this.loadConfig(function () {
            for (var i = 0; i < _this.managerConfig.decks.length; i++) {
                var deckConfig = _this.managerConfig.decks[i];
                _this.decks[deckConfig.serialNumber] = new Deck_1.Deck(deckConfig, null);
            }
            _this.ensureDecks(function (newDecks) {
                (0, Logger_1.Log)('info', "Loaded ".concat(Object.keys(_this.decks).length - newDecks, " existing deck(s) and ").concat(newDecks, " new deck(s)"));
                callback();
            });
        });
    };
    DeckManager.prototype.loadConfig = function (callback) {
        var _this = this;
        pathExists(this.managerConfigPath, function (err, exists) {
            if (err)
                throw err;
            if (exists) {
                readJson(_this.managerConfigPath, function (err, json) {
                    if (err)
                        throw err;
                    _this.managerConfig = json;
                    callback();
                });
            }
            else {
                _this.managerConfig = defaultDeckConfig;
                _this.saveConfig(callback);
            }
        });
    };
    DeckManager.prototype.saveConfig = function (callback) {
        var toSave = {
            decks: []
        };
        for (var serialNumber in this.decks)
            toSave.decks.push(this.decks[serialNumber].export());
        writeFile(this.managerConfigPath, JSON.stringify(toSave, null, 4), function (err) {
            if (err)
                (0, Logger_1.Log)('error', 'Error whilst saving manager config', err.message);
            if (callback)
                callback();
        });
    };
    DeckManager.prototype.hasDeck = function (serialNumber) {
        return this.decks[serialNumber] != undefined;
    };
    DeckManager.prototype.getDeck = function (serialNumber) {
        if (this.decks[serialNumber])
            return this.decks[serialNumber];
        return null;
    };
    DeckManager.prototype.ensureDecks = function (callback) {
        var _this = this;
        var currentDecks = Object.keys(this.decks).length;
        var decks = StreamDeck.listStreamDecks();
        for (var i = 0; i < decks.length; i++) {
            if (!this.hasDeck(decks[i].serialNumber))
                this.decks[decks[i].serialNumber] = new Deck_1.Deck({
                    serialNumber: decks[i].serialNumber,
                    model: decks[i].model,
                    name: "Unnamed ".concat(decks[i].model)
                }, decks[i].path);
            else
                this.decks[decks[i].serialNumber].setDevicePath(decks[i].path);
        }
        if (Object.keys(this.decks).length > currentDecks)
            this.saveConfig(function () { return callback(Object.keys(_this.decks).length - currentDecks); });
        else
            callback(0);
    };
    DeckManager.prototype.getList = function () {
        var list = [];
        for (var serialNumber in this.decks) {
            list.push({ serialNumber: serialNumber, name: this.decks[serialNumber].getName() });
        }
        return list;
    };
    return DeckManager;
}());
exports.DeckManager = DeckManager;
var defaultDeckConfig = {
    decks: []
};
//# sourceMappingURL=DeckManager.js.map