"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.ResolvedJsonForms;
        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;
//# sourceMapping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