"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    }
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
/*
  The MIT License
  
  Copyright (c) 2018 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 React = require("react");
var ReactDOM = require("react-dom");
var JsonRefs = require("json-refs");
var react_redux_1 = require("react-redux");
var core_1 = require("@jsonforms/core");
var react_1 = require("@jsonforms/react");
/**
 * Annotation that registered the given config and class as a custom element
 * @param {CustomElementConfig} config the configuration object for the custom element
 * @constructor
 */
// Usage as decorator
// tslint:disable:variable-name
var CustomElement = function (config) { return function (cls) {
    // tslint:enable:variable-name
    if (customElements.get(config.selector)) {
        return;
    }
    customElements.define(config.selector, cls);
}; };
// TODO: Type parameter
/**
 * HTML element that represents the entry point
 */
var JsonFormsElement = /** @class */ (function (_super) {
    __extends(JsonFormsElement, _super);
    /**
     * Constructor.
     */
    function JsonFormsElement() {
        var _this = _super.call(this) || this;
        _this.InnerComponent = react_1.JsonForms;
        _this.innerComponentParameters = {};
        _this.allowDynamicUpdate = false;
        return _this;
    }
    /**
     * Called when this element is inserted into a document.
     */
    JsonFormsElement.prototype.connectedCallback = function () {
        this.allowDynamicUpdate = true;
        this.render();
    };
    Object.defineProperty(JsonFormsElement.prototype, "store", {
        get: function () {
            return this._store;
        },
        /**
         * Set the store to be used by the element
         *
         * @package
         * @param {Object} store the store containing the jsonforms state and reducer
         */
        set: function (store) {
            var _this = this;
            var setupStore = function (schema, uischema, d) {
                store.dispatch(core_1.Actions.init(d, schema, uischema));
                return store;
            };
            var data = core_1.getData(store.getState()) || {};
            JsonRefs
                .resolveRefs(core_1.getSchema(store.getState()) || core_1.Generate.jsonSchema(data), { includeInvalid: true }).then(function (result) {
                _this._store = setupStore(result.resolved, core_1.getUiSchema(store.getState()), data);
                _this.render();
            });
        },
        enumerable: true,
        configurable: true
    });
    /**
     * Set the inner component used by this json forms element.
     * By default the JsonForms component is used.
     *
     * @param InnerComponent The component to use instead of JsonForms
     * @param parameters The parameters used when instantiating the given component
     */
    JsonFormsElement.prototype.setInnerComponent = function (InnerComponent, parameters) {
        if (parameters === void 0) { parameters = {}; }
        this.InnerComponent = InnerComponent;
        this.innerComponentParameters = parameters;
    };
    JsonFormsElement.prototype.render = function () {
        if (!this.allowDynamicUpdate) {
            return;
        }
        if (this._store === null || this._store === undefined) {
            return;
        }
        var storeId = new Date().toISOString();
        ReactDOM.render(React.createElement(react_redux_1.Provider, { store: this._store, key: storeId + "-store" },
            React.createElement(this.InnerComponent, __assign({}, this.innerComponentParameters))), this);
    };
    JsonFormsElement = __decorate([
        CustomElement({
            selector: 'json-forms'
        }),
        __metadata("design:paramtypes", [])
    ], JsonFormsElement);
    return JsonFormsElement;
}(HTMLElement));
exports.JsonFormsElement = JsonFormsElement;
//# sourceMappingURL=JsonFormsElement.js.map