'use strict';

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

const core = require('./core-6c8c650d.js');

const PwcDynamicFormField = class {
    constructor(hostRef) {
        core.registerInstance(this, hostRef);
        this.fieldChanged = core.createEvent(this, "fieldChanged", 7);
    }
    changeEventHandler(event) {
        const element = event.target;
        let elementValue;
        if (this.nativeInputRef.type === "checkbox") {
            elementValue = element.checked;
        }
        else {
            elementValue = element.value;
        }
        this.fieldChanged.emit({
            element,
            newValue: elementValue,
            originalEvent: event
        });
    }
    selectedOptionsChangedHandler(event) {
        const element = event.target;
        const value = event.detail;
        this.fieldChanged.emit({
            element,
            newValue: value,
            originalEvent: event
        });
    }
    colorPickedeventHandler(event) {
        event.stopPropagation();
        event.preventDefault();
        const element = event.target;
        const value = event.detail;
        this.fieldChanged.emit({
            element,
            newValue: value,
            originalEvent: event
        });
    }
    async getValue() {
        if (this.nativeInputRef) {
            if (this.nativeInputRef.type === "checkbox") {
                return this.nativeInputRef.checked;
            }
            else {
                return this.nativeInputRef.value;
            }
        }
        if (this.choicesRef) {
            return this.choicesRef.getSelectedOptionsAsValues();
        }
        if (this.colorPickerRef) {
            return this.colorPickerRef.activeColor;
        }
    }
    handleColorPickerRef(ref) {
        this.colorPickerRef = ref;
    }
    handleChoicesRef(ref) {
        this.choicesRef = ref;
    }
    handleNativeInputRef(ref) {
        this.nativeInputRef = ref;
    }
    constructField(field) {
        let castedField;
        const label = field.label;
        delete field.label;
        switch (field.type) {
            case "color":
                castedField = field;
                return [
                    core.h("label", { htmlFor: castedField.name }, label),
                    core.h("pwc-color-picker", Object.assign({}, castedField, { ref: this.handleColorPickerRef.bind(this) }))
                ];
            // 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("input", Object.assign({}, castedField, { ref: this.handleNativeInputRef.bind(this) })),
                    core.h("label", { htmlFor: castedField.name }, label)
                ];
            default:
                castedField = field;
                return [
                    core.h("label", { htmlFor: castedField.name }, label),
                    core.h("input", Object.assign({}, castedField))
                ];
        }
    }
    constructPwcChoices(label, castedField) {
        return [
            core.h("label", { htmlFor: castedField.name }, label),
            core.h("pwc-choices", Object.assign({}, castedField, { ref: this.handleChoicesRef.bind(this) }))
        ];
    }
    render() {
        return this.constructField(this.config);
    }
    get rootElement() { return core.getElement(this); }
    static get style() { return "pwc-dynamic-form-field {\n  display: block;\n}"; }
};

exports.pwc_dynamic_form_field = PwcDynamicFormField;
