import { __decorate, __rest } from "tslib";
import { mapDispatchToControlProps, mapStateToJsonFormsRendererProps } from '@jsonforms/core';
import { FASTElement, html, css, observable, attr, customElement } from '@microsoft/fast-element';
import maxBy from 'lodash/maxBy';
const template = html `
  <template>${(x) => x.determinedRenderer.renderer}</template>
`;
const styles = css ``;
const useControl = (props, jsonforms, dispatch, stateMapper) => {
    const control = Object.assign({}, stateMapper({ jsonforms }, props));
    const dispatchMethods = mapDispatchToControlProps === null || mapDispatchToControlProps === void 0 ? void 0 : mapDispatchToControlProps(dispatch);
    return Object.assign(Object.assign({}, control), dispatchMethods);
};
let DispatchRenderer = class DispatchRenderer extends FASTElement {
    constructor() {
        super(...arguments);
        this.createRenderer = () => {
            if (!this.jsonforms || !this.dispatch || !this.props) {
                return;
            }
            const rawProps = mapStateToJsonFormsRendererProps({ jsonforms: this.jsonforms }, this.props);
            const { rootSchema } = rawProps, renderer = __rest(rawProps, ["rootSchema"]);
            this.renderer = renderer;
            this.control = useControl(this.props, this.jsonforms, this.dispatch, 
            // @ts-ignore
            this.determinedRenderer.mapper);
        };
    }
    jsonformsChanged() {
        this.createRenderer();
    }
    propsChanged() {
        this.createRenderer();
    }
    submittedChanged() {
        this.touched = !!this.submitted;
    }
    onBlur() {
        this.touched = true;
    }
    get determinedRenderer() {
        const rendererEntry = maxBy(this.renderer.renderers, (r) => 
        // @ts-ignore
        r.tester(this.renderer.uischema, this.renderer.schema));
        if (rendererEntry === undefined ||
            // @ts-ignore
            rendererEntry.tester(this.renderer.uischema, this.renderer.schema) === -1) {
            console.error('UnknownRenderer this.renderer.uischema', this.renderer.uischema);
            return { renderer: 'UnknownRenderer' };
        }
        else {
            return rendererEntry;
        }
    }
    disconnectedCallback() {
        super.disconnectedCallback();
        this.touched = false;
        this.submitted = false;
    }
};
__decorate([
    observable
], DispatchRenderer.prototype, "jsonforms", void 0);
__decorate([
    observable
], DispatchRenderer.prototype, "props", void 0);
__decorate([
    observable
], DispatchRenderer.prototype, "touched", void 0);
__decorate([
    attr({ mode: 'boolean' })
], DispatchRenderer.prototype, "submitted", void 0);
__decorate([
    observable
], DispatchRenderer.prototype, "dispatch", void 0);
__decorate([
    observable
], DispatchRenderer.prototype, "control", void 0);
__decorate([
    observable
], DispatchRenderer.prototype, "renderer", void 0);
DispatchRenderer = __decorate([
    customElement({
        name: 'dispatch-renderer',
        template: template,
        styles: styles,
    })
], DispatchRenderer);
export { DispatchRenderer };
//# sourceMappingURL=dispatch-renderer.js.map