'use strict';

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

const core = require('./core-0dcf683d.js');
require('./_commonjsHelpers-ab75601c.js');
const utils = require('./utils-19525089.js');

const PwcDynamicFormContent = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.fieldChanged = core.createEvent(this, "fieldChanged", 7);
    }
    onItemsChanged(items) {
        this.resolvedItems = utils.resolveJson(items);
    }
    componentWillLoad() {
        this.onItemsChanged(this.items);
    }
    handleFieldChange(eventPayload) {
        this.fieldChanged.emit(eventPayload);
    }
    constructField(field) {
        let castedField;
        const label = field.label;
        delete field.label;
        switch (field.type) {
            case "color":
                castedField = field;
                return (core.h("div", { class: "form-group" }, core.h("label", null, label, core.h("color-picker", Object.assign({}, castedField)))));
            // Special handle reason: using pwc-choices.
            case "select-single":
                castedField = field;
                castedField.type = "single";
                return this.constructPwcChoices(label, castedField);
            // Special handle reason: using pwc-choices.
            case "select-multi":
                castedField = field;
                castedField.type = "multi";
                return this.constructPwcChoices(label, castedField);
            // Special handle reason: label needs to be placed after the input element.
            case "checkbox":
                castedField = field;
                return (core.h("div", { class: "form-group" }, core.h("label", null, core.h("input", Object.assign({}, castedField)), label)));
            default:
                castedField = field;
                return (core.h("div", { class: "form-group" }, core.h("label", null, label, core.h("input", Object.assign({}, castedField)))));
        }
    }
    constructPwcChoices(label, castedField) {
        return (core.h("div", { class: "form-group" }, core.h("label", null, label, core.h("pwc-choices", Object.assign({}, castedField)))));
    }
    init() {
        const colorPickers = this.rootElement.querySelectorAll("color-picker");
        colorPickers.forEach(cp => {
            cp.addEventListener("colorPickedEvent", originalEvent => {
                const fieldChangedEventPayload = {
                    element: cp,
                    newValue: cp.activeColor,
                    originalEvent
                };
                this.handleFieldChange(fieldChangedEventPayload);
            });
        });
        const pwcChoicesElements = this.rootElement.querySelectorAll("pwc-choices");
        pwcChoicesElements.forEach(pce => {
            pce.addEventListener("selectedOptionsChanged", (event) => {
                const fieldChangedEventPayload = {
                    element: pce,
                    newValue: event.detail,
                    originalEvent: event
                };
                this.handleFieldChange(fieldChangedEventPayload);
            });
        });
        // vanilla html inputs
        const vanillaInputs = utils.getVanillaHtmlInputs(this.rootElement, true);
        vanillaInputs.forEach(vf => {
            vf.addEventListener("change", e => {
                const fieldChangedEventPayload = {
                    element: vf,
                    newValue: vf.value,
                    originalEvent: e
                };
                this.handleFieldChange(fieldChangedEventPayload);
            });
        });
    }
    componentDidLoad() {
        this.init();
    }
    componentDidUpdate() {
        this.init();
    }
    render() {
        return (core.h("div", null, this.resolvedItems
            ? this.resolvedItems.map(field => this.constructField(field))
            : ""));
    }
    get rootElement() { return core.getElement(this); }
    static get watchers() { return {
        "items": ["onItemsChanged"]
    }; }
    static get style() { return ""; }
};

exports.pwc_dynamic_form_content = PwcDynamicFormContent;
