"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var core_1 = require("@jsonforms/core");
var rxjs_1 = require("rxjs");
var lodash_1 = require("lodash");
var core_2 = require("@angular/core");
var i0 = tslib_1.__importStar(require("@angular/core"));
var JsonFormsAngularService = /** @class */ (function () {
    function JsonFormsAngularService() {
    }
    JsonFormsAngularService.prototype.init = function (initialState) {
        if (initialState === void 0) { initialState = {}; }
        this._state = initialState;
        this._state.config = core_1.configReducer(undefined, core_1.setConfig(this._state.config));
        this.state = new rxjs_1.BehaviorSubject({ jsonforms: this._state });
    };
    Object.defineProperty(JsonFormsAngularService.prototype, "$state", {
        get: function () {
            if (!this.state) {
                this.init();
            }
            return this.state.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    JsonFormsAngularService.prototype.registerRenderer = function (renderer, tester) {
        this._state.renderers.push({ renderer: renderer, tester: tester });
        this.updateSubject();
    };
    JsonFormsAngularService.prototype.registerRenderers = function (renderers) {
        this._state.renderers = renderers;
        this.updateSubject();
    };
    JsonFormsAngularService.prototype.unregisterRenderer = function (tester) {
        var findIndex = this._state.renderers.findIndex(function (v) { return v.tester === tester; });
        if (findIndex === -1) {
            return;
        }
        var renderers = this._state.renderers.filter(function (v) { return v.tester !== tester; });
        this._state.renderers = renderers;
        this.updateSubject();
    };
    JsonFormsAngularService.prototype.updateValidationMode = function (validationMode) {
        var coreState = core_1.coreReducer(this._state.core, core_1.Actions.setValidationMode(validationMode));
        this._state.core = coreState;
        this.updateSubject();
    };
    JsonFormsAngularService.prototype.updateLocale = function (localeAction) {
        var localeState = core_1.i18nReducer(this._state.i18n, localeAction);
        this._state.i18n = localeState;
        this.updateSubject();
        return localeAction;
    };
    JsonFormsAngularService.prototype.updateCore = function (coreAction) {
        var coreState = core_1.coreReducer(this._state.core, coreAction);
        this._state.core = coreState;
        this.updateSubject();
        return coreAction;
    };
    JsonFormsAngularService.prototype.updateUiSchema = function (uischemaAction) {
        var uischemaState = core_1.uischemaRegistryReducer(this._state.uischemas, uischemaAction);
        this._state.uischemas = uischemaState;
        this.updateSubject();
        return uischemaAction;
    };
    JsonFormsAngularService.prototype.updateConfig = function (setConfigAction) {
        var configState = core_1.configReducer(this._state.config, setConfigAction);
        this._state.config = configState;
        this.updateSubject();
        return setConfigAction;
    };
    JsonFormsAngularService.prototype.getState = function () {
        return lodash_1.cloneDeep({ jsonforms: this._state });
    };
    JsonFormsAngularService.prototype.updateSubject = function () {
        this.state.next({ jsonforms: this._state });
    };
    JsonFormsAngularService.ɵprov = i0.ɵɵdefineInjectable({ factory: function JsonFormsAngularService_Factory() { return new JsonFormsAngularService(); }, token: JsonFormsAngularService, providedIn: "root" });
    JsonFormsAngularService = tslib_1.__decorate([
        core_2.Injectable({
            providedIn: 'root'
        })
    ], JsonFormsAngularService);
    return JsonFormsAngularService;
}());
exports.JsonFormsAngularService = JsonFormsAngularService;
//# sourceMappingURL=jsonforms.service.js.map