"use strict";
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.
*/
const React = require("react");
const ReactDOM = require("react-dom");
const JsonRefs = require("json-refs");
const react_redux_1 = require("react-redux");
const core_1 = require("@jsonforms/core");
const 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
const CustomElement = (config) => (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
 */
let JsonFormsElement = class JsonFormsElement extends HTMLElement {
    /**
     * Constructor.
     */
    constructor() {
        super();
        this.InnerComponent = react_1.JsonForms;
        this.innerComponentParameters = {};
        this.allowDynamicUpdate = false;
    }
    /**
     * Called when this element is inserted into a document.
     */
    connectedCallback() {
        this.allowDynamicUpdate = true;
        this.render();
    }
    /**
     * Set the store to be used by the element
     *
     * @package
     * @param {Object} store the store containing the jsonforms state and reducer
     */
    set store(store) {
        const setupStore = (schema, uischema, d) => {
            store.dispatch(core_1.Actions.init(d, schema, uischema));
            return store;
        };
        const data = core_1.getData(store.getState()) || {};
        JsonRefs
            .resolveRefs(core_1.getSchema(store.getState()) || core_1.Generate.jsonSchema(data), { includeInvalid: true }).then(result => {
            this._store = setupStore(result.resolved, core_1.getUiSchema(store.getState()), data);
            this.render();
        });
    }
    get store() {
        return this._store;
    }
    /**
     * 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
     */
    setInnerComponent(InnerComponent, parameters = {}) {
        this.InnerComponent = InnerComponent;
        this.innerComponentParameters = parameters;
    }
    render() {
        if (!this.allowDynamicUpdate) {
            return;
        }
        if (this._store === null || this._store === undefined) {
            return;
        }
        const storeId = new Date().toISOString();
        ReactDOM.render(React.createElement(react_redux_1.Provider, { store: this._store, key: `${storeId}-store` },
            React.createElement(this.InnerComponent, Object.assign({}, this.innerComponentParameters))), this);
    }
};
JsonFormsElement = __decorate([
    CustomElement({
        selector: 'json-forms'
    }),
    __metadata("design:paramtypes", [])
], JsonFormsElement);
exports.JsonFormsElement = JsonFormsElement;
//# sourceMappingURL=data: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