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.text != 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.text == undefined)
                                key.appearence.text = {
                                    offsetX: 0,
                                    offsetY: 0,
                                    size: 20,
                                    value: null,
                                    color: '#ffffff'
                                };
                            key.appearence.text.value = properties.text;
                            page.updateKey(location.x, location.y, key, null);
                            status('Key text 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 text specified', 'error');
        }
        else
            status('No key specified', 'error');
    });
    actionAPI.onOpenEditor(function (editorAPI, properties) {
        var lastPageState = 'current';
        var initKey = '';
        var initText = properties.text != undefined ? properties.text : '';
        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: 'text',
                name: 'Text',
                type: 'text',
                value: initText
            }
        ];
        editorAPI.saveProperties({
            key: initKey == 'current' ? editorAPI.key.id : initKey,
            text: initText
        });
        editorAPI.onFieldChanges(function (changedFields) {
            var fieldObject = editorAPI.tools.objectifyFieldsValues(changedFields);
            fields[0].value = fieldObject.page;
            fields[1].value = fieldObject.key;
            fields[2].value = fieldObject.text;
            if (fieldObject.page != lastPageState) {
                fields[1].values = getPageKeyValues(fieldObject.page);
                editorAPI.setFields(fields);
            }
            editorAPI.saveProperties({
                key: fieldObject.key == 'current' ? editorAPI.key.id : fieldObject.key,
                text: fieldObject.text
            });
        });
        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=settext.js.map