'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const core = require('./core-6c8c650d.js');
require('./_commonjsHelpers-ab75601c.js');
const utils = require('./utils-27bd9876.js');
const lodash = require('./lodash-23b825de.js');

const PwcDynamicFormContent = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
    }
    itemsWatchHandler(items) {
        this.resolvedItems = utils.resolveJson(items);
    }
    async getFieldRefs() {
        return this.fieldRefs;
    }
    componentWillLoad() {
        this.itemsWatchHandler(this.items);
    }
    handleFieldRef(fieldConfig, fieldRef) {
        if (fieldRef) {
            this.fieldRefs = lodash._.unionBy(this.fieldRefs, [fieldRef], i => i.config.name);
        }
        else {
            lodash._.remove(this.fieldRefs, i => i.config.name === fieldConfig.name);
        }
    }
    constructField(fieldConfig) {
        let nameIndexer = 0;
        const nameGenerator = name => {
            return name || "pwc-dynamic-form___generated-name-" + nameIndexer++;
        };
        fieldConfig.name = nameGenerator(fieldConfig.name);
        return (core.h("pwc-dynamic-form-field", { config: fieldConfig, ref: this.handleFieldRef.bind(this, fieldConfig) }));
    }
    render() {
        if (this.resolvedItems) {
            // we are mutating the config, therefore we have to clone it to leave the user input intact.
            const resolvedItemsClone = lodash._.cloneDeep(this.resolvedItems);
            return resolvedItemsClone.map(this.constructField.bind(this));
        }
        else {
            return "";
        }
    }
    get rootElement() { return core.getElement(this); }
    static get watchers() { return {
        "items": ["itemsWatchHandler"]
    }; }
    static get style() { return ""; }
};

exports.pwc_dynamic_form_content = PwcDynamicFormContent;
