"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
/*
  The MIT License

  Copyright (c) 2017-2019 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 maxBy_1 = tslib_1.__importDefault(require("lodash/maxBy"));
var core_1 = require("@angular/core");
var core_2 = require("@jsonforms/core");
var unknown_component_1 = require("./unknown.component");
var base_renderer_1 = require("./base.renderer");
var control_1 = require("./control");
var jsonforms_service_1 = require("./jsonforms.service");
var lodash_1 = require("lodash");
var areEqual = function (prevProps, nextProps) {
    return lodash_1.get(prevProps, 'renderers.length') === lodash_1.get(nextProps, 'renderers.length')
        && lodash_1.get(prevProps, 'cells.length') === lodash_1.get(nextProps, 'cells.length')
        && lodash_1.get(prevProps, 'uischemas.length') === lodash_1.get(nextProps, 'uischemas.length')
        && lodash_1.get(prevProps, 'schema') === lodash_1.get(nextProps, 'schema')
        && lodash_1.get(prevProps, 'uischema') === lodash_1.get(nextProps, 'uischema')
        && lodash_1.get(prevProps, 'path') === lodash_1.get(nextProps, 'path');
};
var ɵ0 = areEqual;
exports.ɵ0 = ɵ0;
var JsonFormsOutlet = /** @class */ (function (_super) {
    tslib_1.__extends(JsonFormsOutlet, _super);
    function JsonFormsOutlet(viewContainerRef, componentFactoryResolver, jsonformsService) {
        var _this = _super.call(this) || this;
        _this.viewContainerRef = viewContainerRef;
        _this.componentFactoryResolver = componentFactoryResolver;
        _this.jsonformsService = jsonformsService;
        return _this;
    }
    Object.defineProperty(JsonFormsOutlet.prototype, "renderProps", {
        set: function (renderProps) {
            this.path = renderProps.path;
            this.schema = renderProps.schema;
            this.uischema = renderProps.uischema;
            this.update(this.jsonformsService.getState());
        },
        enumerable: true,
        configurable: true
    });
    JsonFormsOutlet.prototype.ngOnInit = function () {
        var _this = this;
        this.subscription = this.jsonformsService.$state.subscribe({
            next: function (state) { return _this.update(state); }
        });
    };
    JsonFormsOutlet.prototype.update = function (state) {
        var props = core_2.mapStateToJsonFormsRendererProps(state, {
            schema: this.schema,
            uischema: this.uischema,
            path: this.path
        });
        if (areEqual(this.previousProps, props)) {
            return;
        }
        else {
            this.previousProps = props;
        }
        var renderers = props.renderers;
        var schema = this.schema || props.schema;
        var uischema = this.uischema || props.uischema;
        var renderer = maxBy_1.default(renderers, function (r) { return r.tester(uischema, schema); });
        var bestComponent = unknown_component_1.UnknownRenderer;
        if (renderer !== undefined && renderer.tester(uischema, schema) !== -1) {
            bestComponent = renderer.renderer;
        }
        var componentFactory = this.componentFactoryResolver.resolveComponentFactory(bestComponent);
        this.viewContainerRef.clear();
        var currentComponentRef = this.viewContainerRef.createComponent(componentFactory);
        if (currentComponentRef.instance instanceof base_renderer_1.JsonFormsBaseRenderer) {
            var instance = currentComponentRef.instance;
            instance.uischema = uischema;
            instance.schema = schema;
            instance.path = this.path;
            if (instance instanceof control_1.JsonFormsControl) {
                var controlInstance = instance;
                if (controlInstance.id === undefined) {
                    var id = core_2.isControl(props.uischema)
                        ? core_2.createId(props.uischema.scope)
                        : undefined;
                    instance.id = id;
                }
            }
        }
    };
    JsonFormsOutlet.prototype.ngOnDestroy = function () {
        if (this.subscription) {
            this.subscription.unsubscribe();
        }
    };
    tslib_1.__decorate([
        core_1.Input(),
        tslib_1.__metadata("design:type", Object),
        tslib_1.__metadata("design:paramtypes", [Object])
    ], JsonFormsOutlet.prototype, "renderProps", null);
    JsonFormsOutlet = tslib_1.__decorate([
        core_1.Directive({
            selector: 'jsonforms-outlet'
        }),
        tslib_1.__metadata("design:paramtypes", [core_1.ViewContainerRef,
            core_1.ComponentFactoryResolver,
            jsonforms_service_1.JsonFormsAngularService])
    ], JsonFormsOutlet);
    return JsonFormsOutlet;
}(base_renderer_1.JsonFormsBaseRenderer));
exports.JsonFormsOutlet = JsonFormsOutlet;
//# sourceMappingURL=jsonforms.component.js.map