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 { Validators } from '@angular/forms';
import { CustomValidators } from '../validation/CustomValidators';
var CwValidationResults = (function () {
    function CwValidationResults(valid) {
        this.valid = valid;
    }
    return CwValidationResults;
}());
export { CwValidationResults };
var VALIDATIONS = {
    maxLength: {
        key: 'maxLength',
        providerFn: function (constraint) {
            return CustomValidators.maxLength(constraint.args['value']);
        }
    },
    maxValue: {
        key: 'maxValue',
        providerFn: function (constraint) { return CustomValidators.max(constraint.args['value']); }
    },
    minLength: {
        key: 'minLength',
        providerFn: function (constraint) {
            return CustomValidators.minLength(constraint.args['value']);
        }
    },
    minValue: {
        key: 'minValue',
        providerFn: function (constraint) { return CustomValidators.min(constraint.args['value']); }
    },
    required: {
        key: 'required',
        providerFn: function (constraint) { return CustomValidators.required(); }
    }
};
var Registry = {};
var DataTypeModel = (function () {
    function DataTypeModel(id, errorMessageKey, _constraints, defaultValue) {
        if (defaultValue === void 0) { defaultValue = null; }
        this.id = id;
        this.errorMessageKey = errorMessageKey;
        this._constraints = _constraints;
        this.defaultValue = defaultValue;
    }
    DataTypeModel.registerType = function (typeId, type) {
        Registry[typeId] = type;
    };
    DataTypeModel.prototype.validators = function () {
        var _this = this;
        if (this._vFns == null) {
            this._vFns = [];
            Object.keys(VALIDATIONS).forEach(function (vDefKey) {
                var vDef = VALIDATIONS[vDefKey];
                var constraint = _this._constraints[vDef.key];
                if (constraint) {
                    var fn = vDef.providerFn(constraint);
                    _this._vFns.push(fn);
                }
            });
        }
        return this._vFns;
    };
    DataTypeModel.prototype.validator = function () {
        return Validators.compose(this.validators());
    };
    return DataTypeModel;
}());
export { DataTypeModel };
var CwInputDefinition = (function () {
    function CwInputDefinition(json, type, name, placeholder, dataType, _validators) {
        if (_validators === void 0) { _validators = []; }
        this.json = json;
        this.type = type;
        this.name = name;
        this.placeholder = placeholder;
        this.dataType = dataType;
        this._validators = _validators;
    }
    CwInputDefinition.registerType = function (typeId, type) {
        Registry[typeId] = type;
    };
    CwInputDefinition.fromJson = function (json, name) {
        var typeId = json.id || json.type;
        var type = Registry[typeId];
        if (!type) {
            var msg = 'No input definition registered for \'' +
                (json.id || json.type) +
                '\'. Using default.';
            // tslint:disable-next-line:no-console
            console.error(msg, json);
            type = 'text';
        }
        var dataType = null;
        if (json.dataType) {
            dataType = new DataTypeModel(json.dataType.id, json.dataType.errorMessageKey, json.dataType.constraints, json.dataType.defaultValue);
        }
        return new type(json, typeId, name, json.placeholder, dataType);
    };
    CwInputDefinition.prototype.validators = function () {
        if (this._vFns == null) {
            this._vFns = this.dataType.validators().concat(this._validators);
        }
        return this._vFns;
    };
    CwInputDefinition.prototype.validator = function () {
        if (this._validator == null) {
            this._vFns = this.validators();
            if (this._vFns && this._vFns.length) {
                this._validator = Validators.compose(this._vFns);
            }
            else {
                this._validator = function () {
                    return null;
                };
            }
        }
        return this._validator;
    };
    CwInputDefinition.prototype.verify = function (value) {
        return this.validator()({ value: value });
    };
    return CwInputDefinition;
}());
export { CwInputDefinition };
var CwSpacerInputDefinition = (function (_super) {
    __extends(CwSpacerInputDefinition, _super);
    function CwSpacerInputDefinition(flex) {
        var _this = _super.call(this, {}, 'spacer', null, null, null) || this;
        _this.flex = flex;
        return _this;
    }
    return CwSpacerInputDefinition;
}(CwInputDefinition));
export { CwSpacerInputDefinition };
CwInputDefinition.registerType('text', CwInputDefinition);
CwInputDefinition.registerType('datetime', CwInputDefinition);
CwInputDefinition.registerType('number', CwInputDefinition);
var CwDropdownInputModel = (function (_super) {
    __extends(CwDropdownInputModel, _super);
    function CwDropdownInputModel(json, type, name, placeholder, dataType) {
        var _this = _super.call(this, json, type, name, placeholder, dataType, CwDropdownInputModel.createValidators(json)) || this;
        _this.minSelections = 0;
        _this.maxSelections = 1;
        _this.selected = [];
        _this.options = json.options;
        _this.allowAdditions = json.allowAdditions;
        _this.minSelections = json.minSelections;
        _this.maxSelections = json.maxSelections;
        var defV = json.dataType.defaultValue;
        _this.selected = defV == null || defV === '' ? [] : [defV];
        return _this;
    }
    CwDropdownInputModel.createValidators = function (json) {
        var ary = [];
        ary.push(CustomValidators.minSelections(json.minSelections || 0));
        ary.push(CustomValidators.maxSelections(json.maxSelections || 1));
        return ary;
    };
    return CwDropdownInputModel;
}(CwInputDefinition));
export { CwDropdownInputModel };
CwInputDefinition.registerType('dropdown', CwDropdownInputModel);
var CwRestDropdownInputModel = (function (_super) {
    __extends(CwRestDropdownInputModel, _super);
    function CwRestDropdownInputModel(json, type, name, placeholder, dataType) {
        var _this = _super.call(this, json, type, name, placeholder, dataType, CwDropdownInputModel.createValidators(json)) || this;
        _this.minSelections = 0;
        _this.maxSelections = 1;
        _this.selected = [];
        _this.optionUrl = json.optionUrl;
        _this.optionValueField = json.jsonValueField;
        _this.optionLabelField = json.jsonLabelField;
        _this.allowAdditions = json.allowAdditions;
        _this.minSelections = json.minSelections;
        _this.maxSelections = json.maxSelections;
        var defV = json.dataType.defaultValue;
        _this.selected = defV == null || defV === '' ? [] : [defV];
        return _this;
    }
    return CwRestDropdownInputModel;
}(CwInputDefinition));
export { CwRestDropdownInputModel };
CwInputDefinition.registerType('restDropdown', CwRestDropdownInputModel);
var ParameterDefinition = (function () {
    function ParameterDefinition() {
    }
    ParameterDefinition.fromJson = function (json) {
        var m = new ParameterDefinition();
        var defV = json.defaultValue;
        m.defaultValue = defV == null || defV === '' ? null : defV;
        m.priority = json.priority;
        m.key = json.key;
        m.inputType = CwInputDefinition.fromJson(json.inputType, m.key);
        m.i18nBaseKey = json.i18nBaseKey;
        return m;
    };
    return ParameterDefinition;
}());
export { ParameterDefinition };
//# sourceMappingURL=CwInputModel.js.map