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 });
exports.EditorAPI = void 0;
var Logger_1 = require("../Logger");
var EditorAPI = /** @class */ (function () {
    function EditorAPI(settings) {
        this._actionInstance = settings.actionInstance;
        this._integrationID = settings.integrationID;
        this.keyPageID = settings.pageID;
        this._editorID = settings.editorID;
        this.keyX = settings.x;
        this.keyY = settings.y;
        this.key = settings.key;
        this.isOpen = false;
        this.tools = {
            getFieldFromFields: function (fields, fieldID) {
                for (var i = 0; i < fields.length; i++)
                    if ((fields[i].id = fieldID))
                        return fields[i];
                return null;
            },
            objectifyFields: function (fields) {
                var fieldObject = {};
                for (var i = 0; i < fields.length; i++)
                    fieldObject[fields[i].id] = fields[i];
                return fieldObject;
            },
            objectifyFieldsValues: function (fields) {
                var fieldObject = {};
                for (var i = 0; i < fields.length; i++)
                    fieldObject[fields[i].id] = fields[i].value;
                return fieldObject;
            }
        };
    }
    EditorAPI.prototype._emit = function (query) {
        var _a;
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        if (this.isOpen)
            (_a = Undecked.SocketServer).broadcast.apply(_a, __spreadArray(["AE_".concat(this._editorID), query], args, false));
    };
    EditorAPI.prototype.close = function () {
        this.isOpen = false;
    };
    EditorAPI.prototype.saveProperties = function (properties) {
        this._actionInstance.properties = properties;
        if (Undecked.Pages.exists(this.keyPageID))
            Undecked.Pages.get(this.keyPageID).save();
    };
    EditorAPI.prototype.onFieldChanges = function (callback) {
        if (this._change == undefined) {
            this._change = callback;
        }
        else
            (0, Logger_1.Log)('error', "Unable to register two 'onChange' events");
    };
    EditorAPI.prototype.setFields = function (fields) {
        for (var i = 0; i < fields.length; i++)
            if (fields[i].type == 'connection') {
                fields[i].values = [
                    { id: 'none', text: 'None' }
                ];
                var connectionType = fields[i].connectionType;
                if (connectionType) {
                    fields[i].values = __spreadArray([
                        { id: 'none', text: 'None' }
                    ], Undecked.Connections.getConnections(this._integrationID, connectionType), true);
                }
            }
        this._emit('fields', fields);
    };
    return EditorAPI;
}());
exports.EditorAPI = EditorAPI;
//# sourceMappingURL=EditorAPI.js.map