'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 PwcDynamicForm = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.formChanged = core.createEvent(this, "formChanged", 7);
    }
    handleFieldChanged(fieldChangedEventPayload) {
        const rootElement = this.rootElement;
        this.getFieldValues("value").then(v => {
            const formChangedEventPayload = {
                type: "change",
                fieldChangedEventPayload,
                formResetEvent: null,
                formValues: v,
                formElement: rootElement
            };
            this.formChanged.emit(formChangedEventPayload);
        });
    }
    handleFormReset(formResetEvent) {
        const rootElement = this.rootElement;
        this.getFieldValues("value").then(v => {
            const formChangedEventPayload = {
                type: "reset",
                fieldChangedEventPayload: null,
                formResetEvent,
                formValues: v,
                formElement: rootElement
            };
            this.formChanged.emit(formChangedEventPayload);
        });
    }
    async getFieldValues(pwcChoicesRetreiveMode) {
        const form = this.rootElement.querySelector("form");
        const resultObj = {};
        // vanilla html inputs
        const vanillaInputs = utils.getVanillaHtmlInputs(this.rootElement.querySelector("pwc-dynamic-form-content"), true);
        vanillaInputs.forEach(vf => {
            if (vf.type === "checkbox") {
                resultObj[vf.name] = vf.checked;
            }
            else {
                resultObj[vf.name] = vf.value;
            }
        });
        // pwc-choices
        const pwcChoicesInputs = form.querySelectorAll("pwc-choices");
        for (const key in pwcChoicesInputs) {
            if (pwcChoicesInputs.hasOwnProperty(key)) {
                const ci = pwcChoicesInputs[key];
                const value = await ci.getSelectedOptions(pwcChoicesRetreiveMode);
                resultObj[ci.name] = value;
            }
        }
        // color-picker
        const colorPickers = this.rootElement.querySelectorAll("color-picker");
        for (const key in colorPickers) {
            if (colorPickers.hasOwnProperty(key)) {
                const cp = colorPickers[key];
                const value = cp.activeColor;
                const name = cp.getAttribute("name");
                resultObj[name] = value;
            }
        }
        return resultObj;
    }
    render() {
        return (core.h("form", null, core.h("slot", null)));
    }
    get rootElement() { return core.getElement(this); }
    static get style() { return ""; }
};

exports.pwc_dynamic_form = PwcDynamicForm;
