import * as tslib_1 from "tslib";
import set from 'lodash/set';
import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep';
var FormStore = (function () {
    function FormStore(values, rules) {
        if (values === void 0) { values = {}; }
        if (rules === void 0) { rules = {}; }
        this.listeners = [];
        this.errors = {};
        this.defaultFieldsValue = values;
        this.fieldValues = cloneDeep(values);
        this.rules = rules;
    }
    FormStore.prototype.getFieldValue = function (name) {
        return get(this.fieldValues, name);
    };
    FormStore.prototype.getFieldValues = function (names) {
        var _this = this;
        if (names) {
            var values_1 = {};
            Object.keys(names).forEach(function (name) {
                values_1[name] = get(_this.fieldValues, name);
            });
            return values_1;
        }
        else {
            return this.fieldValues;
        }
    };
    FormStore.prototype.setFieldValue = function (name, value) {
        set(this.fieldValues, name, value);
        this.notify(name);
    };
    FormStore.prototype.setFieldValues = function (fields) {
        var _this = this;
        if (fields === void 0) { fields = {}; }
        Object.keys(fields).forEach(function (name) { return _this.setFieldValue(name, fields[name]); });
    };
    FormStore.prototype.resetFields = function () {
        this.errors = {};
        this.fieldValues = cloneDeep(this.defaultFieldsValue);
        this.notify();
    };
    FormStore.prototype.validateField = function (name) {
        var validator = this.rules[name];
        var value = this.getFieldValue(name);
        this.errors[name] = validator ? validator(value) : true;
    };
    FormStore.prototype.validateFields = function (fn) {
        var _this = this;
        Object.keys(this.fieldValues).forEach(function (name) {
            _this.validateField(name);
        });
        this.notify();
        fn && fn(this.errors, this.fieldValues);
    };
    FormStore.prototype.setFieldRules = function (rules) {
        this.rules = tslib_1.__assign({}, this.rules, rules);
    };
    FormStore.prototype.setFieldValidator = function (name, validator) {
        this.rules[name] = validator;
    };
    FormStore.prototype.getFieldError = function (name) {
        return this.errors[name];
    };
    FormStore.prototype.getFieldErrors = function (names) {
        var _this = this;
        if (names) {
            var errors_1 = {};
            Object.keys(names).forEach(function (name) {
                errors_1[name] = _this.getFieldError(name);
            });
            return errors_1;
        }
        else {
            return this.errors;
        }
    };
    FormStore.prototype.setFieldError = function (name, error) {
        this.errors[name] = error;
    };
    FormStore.prototype.setFieldErrors = function (fields) {
        var _this = this;
        if (fields === void 0) { fields = {}; }
        Object.keys(fields).forEach(function (name) { return _this.setFieldError(name, fields[name]); });
    };
    FormStore.prototype.subscribe = function (name, fn) {
        var _this = this;
        this.listeners.push({ name: name, fn: fn });
        return function () {
            var idx = _this.listeners.findIndex(function (listener) { return listener.name === name; });
            (idx > -1) && _this.listeners.splice(idx, 1);
        };
    };
    FormStore.prototype.notify = function (name) {
        if (name) {
            var idx = this.listeners.findIndex(function (listener) { return listener.name === name; });
            (idx > -1) && this.listeners[idx].fn(true);
        }
        else {
            this.listeners.forEach(function (listener) {
                listener.fn(false);
            });
        }
    };
    return FormStore;
}());
export default FormStore;
//# sourceMappingURL=form-store.js.map