var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
module.exports = function (actionAPI) {
    actionAPI.handle(function (properties, status, executed) {
        if (properties.page != undefined) {
            if (Undecked.Pages.exists(properties.page)) {
                var page = Undecked.Pages.get(properties.page);
                if (properties.decks != undefined && properties.decks.length > 0) {
                    var changed = [];
                    var failed = [];
                    for (var i = 0; i < properties.decks.length; i++) {
                        var serialNumber = properties.decks[i];
                        if (Undecked.Decks.hasDeck(serialNumber) || serialNumber == 'current') {
                            var deck = serialNumber != 'current' ? Undecked.Decks.getDeck(serialNumber) : executed;
                            deck.setPageID(page.getID());
                            changed.push(serialNumber);
                        }
                        else
                            failed.push(serialNumber);
                    }
                    if (failed.length == 0)
                        status("Changed ".concat(changed.length, " deck(s) to page '").concat(page.name, "'"), 'info');
                    else
                        status("Deck(s) '".concat(failed.join(', '), "' were not found, only changed ").concat(changed.length, " deck(s) to page '").concat(page.name, "'"), 'warn');
                }
                else
                    status('No decks selected', 'error');
            }
            else
                status('Page does not exist. Maybe it was removed?', 'error');
        }
        else
            status('Missing page property', 'error');
    });
    actionAPI.onOpenEditor(function (editorAPI, properties) {
        editorAPI.onFieldChanges(function (fields) {
            var fieldsObject = editorAPI.tools.objectifyFieldsValues(fields);
            editorAPI.saveProperties({
                decks: fieldsObject.deck,
                page: fieldsObject.page
            });
        });
        editorAPI.setFields([
            {
                id: 'deck',
                name: 'Deck',
                type: 'select',
                multi: true,
                value: properties.decks != undefined
                    ? properties.decks
                    : [
                        'current'
                    ],
                values: getDecksValues()
            },
            {
                id: 'page',
                name: 'Page',
                type: 'select',
                value: properties.page != undefined ? properties.page : '',
                values: getPageValues()
            }
        ]);
    });
    function getDecksValues() {
        return __spreadArray([
            { id: 'current', text: 'Current Deck' }
        ], Undecked.Decks.getList().map(function (deck) {
            return { id: deck.serialNumber, text: deck.name };
        }), true);
    }
    function getPageValues() {
        return Undecked.Pages.getNames().map(function (page) {
            return { id: page.pageID, text: page.name };
        });
    }
};
//# sourceMappingURL=changepage.js.map