"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
/*
  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("@angular/core");
var core_2 = require("@jsonforms/core");
var jsonforms_service_1 = require("./jsonforms.service");
var JsonForms = /** @class */ (function () {
    function JsonForms(jsonformsService) {
        this.jsonformsService = jsonformsService;
        this.dataChange = new core_1.EventEmitter();
        this.initialized = false;
    }
    JsonForms.prototype.ngOnInit = function () {
        var _this = this;
        this.jsonformsService.init({
            core: {
                data: this.data,
                uischema: this.uischema,
                schema: this.schema,
                ajv: this.ajv,
                validationMode: this.validationMode
            },
            uischemas: this.uischemas,
            i18n: { locale: this.locale, localizedSchemas: undefined, localizedUISchemas: undefined },
            renderers: this.renderers,
            config: this.config,
            readonly: this.readonly
        });
        this.jsonformsService.$state.subscribe(function (state) {
            var _a, _b;
            var data = (_b = (_a = state === null || state === void 0 ? void 0 : state.jsonforms) === null || _a === void 0 ? void 0 : _a.core) === null || _b === void 0 ? void 0 : _b.data;
            if (data) {
                _this.dataChange.emit(data);
            }
        });
        this.initialized = true;
    };
    // tslint:disable-next-line: cyclomatic-complexity
    JsonForms.prototype.ngOnChanges = function (changes) {
        if (!this.initialized) {
            return;
        }
        var newData = changes.data;
        var newSchema = changes.schema;
        var newUiSchema = changes.uischema;
        var newRenderers = changes.renderers;
        var newUischemas = changes.uischemas;
        var newLocale = changes.locale;
        var newReadonly = changes.readonly;
        var newValidationMode = changes.validationMode;
        var newAjv = changes.ajv;
        var newConfig = changes.config;
        if (newData || newSchema || newUiSchema || newValidationMode || newAjv) {
            this.jsonformsService.updateCoreState(newData ? newData.currentValue : jsonforms_service_1.USE_STATE_VALUE, newSchema ? newSchema.currentValue : jsonforms_service_1.USE_STATE_VALUE, newUiSchema ? newUiSchema.currentValue : jsonforms_service_1.USE_STATE_VALUE, newAjv ? newAjv.currentValue : jsonforms_service_1.USE_STATE_VALUE, newValidationMode ? newValidationMode.currentValue : jsonforms_service_1.USE_STATE_VALUE);
        }
        if (newRenderers && !newRenderers.isFirstChange()) {
            this.jsonformsService.setRenderers(newRenderers.currentValue);
        }
        if (newUischemas && !newUischemas.isFirstChange()) {
            this.jsonformsService.setUiSchemas(newUischemas.currentValue);
        }
        if (newLocale && !newLocale.isFirstChange()) {
            this.jsonformsService.setLocale(newLocale.currentValue);
        }
        if (newReadonly && !newReadonly.isFirstChange()) {
            this.jsonformsService.setReadonly(newReadonly.currentValue);
        }
        if (newConfig && !newConfig.isFirstChange()) {
            this.jsonformsService.updateConfig(core_2.Actions.setConfig(newConfig.currentValue));
        }
    };
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Object)
    ], JsonForms.prototype, "uischema", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Object)
    ], JsonForms.prototype, "schema", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Object)
    ], JsonForms.prototype, "data", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Array)
    ], JsonForms.prototype, "renderers", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Array)
    ], JsonForms.prototype, "uischemas", void 0);
    tslib_1.__decorate([
        core_1.Output(),
        tslib_1.__metadata("design:type", Object)
    ], JsonForms.prototype, "dataChange", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", String)
    ], JsonForms.prototype, "locale", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Boolean)
    ], JsonForms.prototype, "readonly", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", String)
    ], JsonForms.prototype, "validationMode", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Object)
    ], JsonForms.prototype, "ajv", void 0);
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Object)
    ], JsonForms.prototype, "config", void 0);
    JsonForms = tslib_1.__decorate([
        core_1.Component({
            selector: 'jsonforms',
            template: '<jsonforms-outlet></jsonforms-outlet>',
            providers: [jsonforms_service_1.JsonFormsAngularService]
        }),
        tslib_1.__metadata("design:paramtypes", [jsonforms_service_1.JsonFormsAngularService])
    ], JsonForms);
    return JsonForms;
}());
exports.JsonForms = JsonForms;
//# sourceMappingURL=jsonforms-root.component.js.map