import { r as registerInstance, c as createEvent, h, d as getElement } from './core-b6ab53a4.js';
import './_commonjsHelpers-4ab098b6.js';
import { g as getVanillaHtmlInputs } from './utils-b59928e3.js';

const PwcDynamicForm = class {
    constructor(hostRef) {
        registerInstance(this, hostRef);
        this.formChanged = 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 = 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 (h("form", null, h("slot", null)));
    }
    get rootElement() { return getElement(this); }
    static get style() { return ""; }
};

export { PwcDynamicForm as pwc_dynamic_form };
