"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*
  The MIT License
  
  Copyright (c) 2017-2020 EclipseSource Munich
  https://github.com/eclipsesource/jsonforms
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
var core_1 = require("@jsonforms/core");
var rxjs_1 = require("rxjs");
var lodash_1 = require("lodash");
exports.USE_STATE_VALUE = Symbol('Marker to use state value');
var JsonFormsAngularService = /** @class */ (function () {
    function JsonFormsAngularService() {
    }
    JsonFormsAngularService.prototype.init = function (initialState) {
        if (initialState === void 0) { initialState = { core: { data: undefined, schema: undefined, uischema: undefined } }; }
        var _a, _b;
        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 });
        var data = initialState.core.data;
        var schema = (_a = initialState.core.schema) !== null && _a !== void 0 ? _a : core_1.generateJsonSchema(data);
        var uischema = (_b = initialState.core.uischema) !== null && _b !== void 0 ? _b : core_1.generateDefaultUISchema(schema);
        this.updateCore(core_1.Actions.init(data, schema, uischema));
    };
    Object.defineProperty(JsonFormsAngularService.prototype, "$state", {
        get: function () {
            if (!this.state) {
                throw new Error('Please call init first!');
            }
            return this.state.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    /**
     * @deprecated use {@link JsonFormsAngularService.addRenderer}
     */
    JsonFormsAngularService.prototype.registerRenderer = function (renderer, tester) {
        this.addRenderer(renderer, tester);
    };
    JsonFormsAngularService.prototype.addRenderer = function (renderer, tester) {
        this._state.renderers.push({ renderer: renderer, tester: tester });
        this.updateSubject();
    };
    /**
     * @deprecated use {@link JsonFormsAngularService.setRenderer}
     */
    JsonFormsAngularService.prototype.registerRenderers = function (renderers) {
        this.setRenderers(renderers);
    };
    JsonFormsAngularService.prototype.setRenderers = function (renderers) {
        this._state.renderers = renderers;
        this.updateSubject();
    };
    /**
     * @deprecated use {@link JsonFormsAngularService.removeRenderer}
     */
    JsonFormsAngularService.prototype.unregisterRenderer = function (tester) {
        this.removeRenderer(tester);
    };
    JsonFormsAngularService.prototype.removeRenderer = 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);
        if (coreState !== this._state.core) {
            this._state.core = coreState;
            this.updateSubject();
        }
        return coreAction;
    };
    /**
     * @deprecated use {@link JsonFormsAngularService.setUiSchemas}
     */
    JsonFormsAngularService.prototype.updateUiSchema = function (uischemaAction) {
        var uischemaState = core_1.uischemaRegistryReducer(this._state.uischemas, uischemaAction);
        this._state.uischemas = uischemaState;
        this.updateSubject();
        return uischemaAction;
    };
    JsonFormsAngularService.prototype.setUiSchemas = function (uischemas) {
        this._state.uischemas = uischemas;
        this.updateSubject();
    };
    JsonFormsAngularService.prototype.updateConfig = function (setConfigAction) {
        var configState = core_1.configReducer(this._state.config, setConfigAction);
        this._state.config = configState;
        this.updateSubject();
        return setConfigAction;
    };
    JsonFormsAngularService.prototype.setUiSchema = function (uischema) {
        var newUiSchema = uischema !== null && uischema !== void 0 ? uischema : core_1.generateDefaultUISchema(this._state.core.schema);
        var coreState = core_1.coreReducer(this._state.core, core_1.Actions.updateCore(this._state.core.data, this._state.core.schema, newUiSchema));
        if (coreState !== this._state.core) {
            this._state.core = coreState;
            this.updateSubject();
        }
    };
    JsonFormsAngularService.prototype.setSchema = function (schema) {
        var coreState = core_1.coreReducer(this._state.core, core_1.Actions.updateCore(this._state.core.data, schema !== null && schema !== void 0 ? schema : core_1.generateJsonSchema(this._state.core.data), this._state.core.uischema));
        if (coreState !== this._state.core) {
            this._state.core = coreState;
            this.updateSubject();
        }
    };
    JsonFormsAngularService.prototype.setData = function (data) {
        var coreState = core_1.coreReducer(this._state.core, core_1.Actions.updateCore(data, this._state.core.schema, this._state.core.uischema));
        if (coreState !== this._state.core) {
            this._state.core = coreState;
            this.updateSubject();
        }
    };
    JsonFormsAngularService.prototype.setLocale = function (locale) {
        this._state.i18n.locale = locale;
        this.updateSubject();
    };
    JsonFormsAngularService.prototype.setReadonly = function (readonly) {
        this._state.readonly = readonly;
        this.updateSubject();
    };
    JsonFormsAngularService.prototype.getState = function () {
        return lodash_1.cloneDeep({ jsonforms: this._state });
    };
    JsonFormsAngularService.prototype.refresh = function () {
        this.updateSubject();
    };
    JsonFormsAngularService.prototype.updateCoreState = function (data, schema, uischema, ajv, validationMode) {
        var newData = data === exports.USE_STATE_VALUE ? this._state.core.data : data;
        var newSchema = schema === exports.USE_STATE_VALUE ? this._state.core.schema : schema !== null && schema !== void 0 ? schema : core_1.generateJsonSchema(newData);
        var newUischema = uischema === exports.USE_STATE_VALUE ? this._state.core.uischema : uischema !== null && uischema !== void 0 ? uischema : core_1.generateDefaultUISchema(newSchema);
        var newAjv = ajv === exports.USE_STATE_VALUE ? this._state.core.ajv : ajv;
        var newValidationMode = validationMode === exports.USE_STATE_VALUE ? this._state.core.validationMode : validationMode;
        this.updateCore(core_1.Actions.updateCore(newData, newSchema, newUischema, { ajv: newAjv, validationMode: newValidationMode }));
    };
    JsonFormsAngularService.prototype.updateSubject = function () {
        this.state.next({ jsonforms: this._state });
    };
    return JsonFormsAngularService;
}());
exports.JsonFormsAngularService = JsonFormsAngularService;
//# sourceMappingURL=jsonforms.service.js.map