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, deck) {
        if (properties.key != undefined && properties.key.length > 0) {
            if (properties.color != undefined) {
                var location = Undecked.Pages.KeyManager.getLocation(properties.key);
                if (location) {
                    if (Undecked.Pages.exists(location.pageID)) {
                        var page = Undecked.Pages.get(location.pageID);
                        if (page.hasKey(location.x, location.y)) {
                            var key = page.getKey(location.x, location.y);
                            if (key.appearence == undefined)
                                key.appearence = {};
                            if (key.appearence.background == undefined)
                                key.appearence.background = { color: null };
                            key.appearence.background.color = properties.color;
                            page.updateKey(location.x, location.y, key, null);
                            status('Key color has been updated', 'info');
                        }
                        else
                            status('Unable to find locaton', 'error');
                    }
                    else
                        status('Unable to find page', 'error');
                }
                else
                    status('Invalid key ID. Maybe it was removed?', 'error');
            }
            else
                status('No color specified', 'error');
        }
        else
            status('No key specified', 'error');
    });
    actionAPI.onOpenEditor(function (editorAPI, properties) {
        var lastPageState = 'current';
        var initKey = 'current';
        var initColor = properties.color != undefined ? properties.color : '#ff0000';
        if (properties.key != undefined) {
            var location = Undecked.Pages.KeyManager.getLocation(properties.key);
            if (location) {
                lastPageState = location.pageID == editorAPI.keyPageID ? 'current' : location.pageID;
            }
            initKey = properties.key == editorAPI.key.id ? 'current' : properties.key;
        }
        var fields = [
            {
                id: 'page',
                name: 'Page',
                type: 'select',
                value: lastPageState,
                values: getPageValues()
            },
            {
                id: 'key',
                name: 'Key',
                type: 'select',
                value: initKey,
                values: getPageKeyValues(lastPageState)
            },
            {
                id: 'color',
                name: 'Color',
                type: 'color',
                value: initColor
            }
        ];
        editorAPI.saveProperties({
            key: initKey == 'current' ? editorAPI.key.id : initKey,
            color: initColor
        });
        editorAPI.onFieldChanges(function (changedFields) {
            var fieldObject = editorAPI.tools.objectifyFieldsValues(changedFields);
            fields[0].value = fieldObject.page;
            fields[1].value = fieldObject.key;
            fields[2].value = fieldObject.color;
            if (fieldObject.page != lastPageState) {
                fields[1].values = getPageKeyValues(fieldObject.page);
                editorAPI.setFields(fields);
            }
            editorAPI.saveProperties({
                key: fieldObject.key == 'current' ? editorAPI.key.id : fieldObject.key,
                color: fieldObject.color
            });
        });
        editorAPI.setFields(fields);
        function getPageValues() {
            return __spreadArray([
                { id: 'current', text: 'Current Page' }
            ], Undecked.Pages.getNames().map(function (page) {
                return { id: page.pageID, text: page.name };
            }), true);
        }
        function getPageKeyValues(pageID) {
            var values = [
                { id: 'current', text: 'Current Key' }
            ];
            if (pageID == 'current')
                pageID = editorAPI.keyPageID;
            else
                values = [];
            if (Undecked.Pages.exists(pageID))
                return __spreadArray(__spreadArray([], values, true), Undecked.Pages.get(pageID).getKeyTextList().map(function (key) {
                    return { id: key.id, text: "".concat(key.x, ",").concat(key.y, " - ").concat(key.text != null ? key.text : 'Empty') };
                }), true);
            else
                return [];
        }
    });
};
//# sourceMappingURL=setbackground.js.map