var __extends = (this && this.__extends) || (function () {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
        function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import { CwModel } from './util/CwModel';
import { ParameterDefinition } from './util/CwInputModel';
import { FormControl, Validators } from '@angular/forms';
import { CustomValidators } from './validation/CustomValidators';
var ServerSideFieldModel = (function (_super) {
    __extends(ServerSideFieldModel, _super);
    function ServerSideFieldModel(key, type, priority, loggerService) {
        if (priority === void 0) { priority = 1; }
        var _this = _super.call(this, key) || this;
        _this.loggerService = loggerService;
        _this.parameters = {};
        _this.parameterDefs = {};
        return _this;
    }
    ServerSideFieldModel.createNgControl = function (model, paramName) {
        var param = model.parameters[paramName];
        var paramDef = model.parameterDefs[paramName];
        var vFn = paramDef.inputType.dataType.validators();
        vFn.push(CustomValidators.noQuotes());
        var control = new FormControl(model.getParameterValue(param.key), Validators.compose(vFn));
        return control;
    };
    Object.defineProperty(ServerSideFieldModel.prototype, "type", {
        get: function () {
            return this._type;
        },
        set: function (type) {
            var _this = this;
            if (type && this._type !== type) {
                this._type = type;
                this.parameterDefs = {};
                this.parameters = {};
                Object.keys(type.parameters).forEach(function (key) {
                    var x = type.parameters[key];
                    var paramDef = ParameterDefinition.fromJson(x);
                    var defaultValue = paramDef.defaultValue || paramDef.inputType.dataType.defaultValue;
                    _this.parameterDefs[key] = paramDef;
                    _this.parameters[key] = {
                        key: key,
                        priority: paramDef.priority,
                        value: defaultValue
                    };
                });
            }
        },
        enumerable: true,
        configurable: true
    });
    ServerSideFieldModel.prototype.setParameter = function (key, value, priority) {
        if (priority === void 0) { priority = 1; }
        if (this.parameterDefs[key] === undefined) {
            this.loggerService.info('Unsupported parameter: ', key, 'Valid parameters: ', Object.keys(this.parameterDefs));
            return;
        }
        this.parameters[key] = { key: key, priority: priority, value: value };
    };
    ServerSideFieldModel.prototype.getParameter = function (key) {
        var v = '';
        if (this.parameters[key] !== undefined) {
            v = this.parameters[key];
        }
        return v;
    };
    ServerSideFieldModel.prototype.getParameterValue = function (key) {
        var v = null;
        if (this.parameters[key] !== undefined) {
            v = this.parameters[key].value;
        }
        return v;
    };
    ServerSideFieldModel.prototype.getParameterDef = function (key) {
        var v = '';
        if (this.parameterDefs[key] !== undefined) {
            v = this.parameterDefs[key];
        }
        return v;
    };
    ServerSideFieldModel.prototype.isValid = function () {
        var _this = this;
        var valid = true;
        if (this.parameterDefs) {
            Object.keys(this.parameterDefs).some(function (key) {
                var paramDef = _this.getParameterDef(key);
                var param = _this.parameters[key];
                var value = param.value;
                try {
                    valid = valid && paramDef.inputType.verify(value) == null;
                }
                catch (e) {
                    _this.loggerService.error(e);
                }
                if (paramDef.inputType.name === 'comparison' &&
                    paramDef.inputType['options'][value].rightHandArgCount === 0) {
                    return true;
                }
            });
        }
        valid = valid && this._type && this._type.key && this._type.key !== 'NoSelection';
        return valid;
    };
    return ServerSideFieldModel;
}(CwModel));
export { ServerSideFieldModel };
var ServerSideTypeModel = (function () {
    function ServerSideTypeModel(key, i18nKey, parameters) {
        if (key === void 0) { key = 'NoSelection'; }
        if (i18nKey === void 0) { i18nKey = null; }
        if (parameters === void 0) { parameters = {}; }
        this.key = key ? key : 'NoSelection';
        this.i18nKey = i18nKey;
        this.parameters = parameters;
    }
    ServerSideTypeModel.fromJson = function (json) {
        return new ServerSideTypeModel(json.key, json.i18nKey, json.parameterDefinitions);
    };
    ServerSideTypeModel.prototype.isValid = function () {
        return !!this.i18nKey && !!this.parameters;
    };
    return ServerSideTypeModel;
}());
export { ServerSideTypeModel };
//# sourceMappingURL=ServerSideFieldModel.js.map