import { h } from "@stencil/core";
import { trackComponent } from "../../global/utils/tracking";
import { isNestedInIfxComponent } from "../../global/utils/dom-utils";
import { detectFramework } from "../../global/utils/framework-detection";
export class RadioButtonGroup {
    constructor() {
        this.errorStates = new Map();
        this.alignment = 'vertical';
        this.groupLabelText = 'Group Label Text';
        this.required = false;
        this.hasErrors = false;
        this.handleSlotChange = () => {
            this.initializeState();
        };
    }
    handleRadioButtonError(event) {
        const radioButton = event.target;
        if (radioButton.tagName.toLowerCase() === 'ifx-radio-button') {
            this.errorStates.set(radioButton, event.detail);
            this.updateHasErrors();
        }
    }
    // Method to set the error state of all radio-butttons in the group
    async setGroupError(error) {
        const radioButtons = Array.from(this.el.querySelectorAll('ifx-radio-button'));
        radioButtons.forEach((radioButton) => {
            radioButton.error = error;
        });
    }
    componentWillLoad() {
        this.initializeState();
    }
    async componentDidLoad() {
        if (!isNestedInIfxComponent(this.el)) {
            const framework = detectFramework();
            trackComponent('ifx-radio-button-group', await framework);
        }
    }
    initializeState() {
        this.errorStates.clear();
        const radioButtons = Array.from(this.el.querySelectorAll('ifx-radio-button'));
        radioButtons.forEach((radioButton) => {
            if (!this.errorStates.has(radioButton)) {
                this.errorStates.set(radioButton, radioButton.error || false);
            }
        });
        this.updateHasErrors();
    }
    updateHasErrors() {
        console.log('here');
        this.hasErrors = Array.from(this.errorStates.values()).some((error) => error);
    }
    render() {
        return (h("div", { key: 'dcd66919142f41e8d51054f7de1663361c273232', class: 'radio-button-group-container' }, this.showGroupLabel && (h("div", { key: '7765acb090b78c495b03b17f242b0efacba29a10', class: "group-label" }, this.groupLabelText, this.required && h("span", { key: 'e500cc4867c756991d6e76b77640b6fc1f052296', class: `required ${this.hasErrors ? 'error' : ''}` }, "*"))), h("div", { key: '05211a3727a84a135588bccee63d22c36511cbee', class: `radio-button-group ${this.alignment} ${this.size}` }, h("slot", { key: '4d9cbd4f52276f6c101306208bd6156e01287f2f', onSlotchange: this.handleSlotChange })), this.showCaption ? (h("div", { class: `caption ${this.hasErrors ? 'error' : 'default'}` }, this.showCaptionIcon ? h("div", { class: 'caption-icon' }, h("ifx-icon", { icon: "c-info-16" })) : '', h("div", { class: 'caption-text' }, this.captionText))) : ''));
    }
    static get is() { return "ifx-radio-button-group"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["radio-button-group.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["radio-button-group.css"]
        };
    }
    static get properties() {
        return {
            "alignment": {
                "type": "string",
                "attribute": "alignment",
                "mutable": false,
                "complexType": {
                    "original": "'horizontal' | 'vertical'",
                    "resolved": "\"horizontal\" | \"vertical\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "'vertical'"
            },
            "size": {
                "type": "string",
                "attribute": "size",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "showGroupLabel": {
                "type": "boolean",
                "attribute": "show-group-label",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "groupLabelText": {
                "type": "string",
                "attribute": "group-label-text",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "'Group Label Text'"
            },
            "showCaption": {
                "type": "boolean",
                "attribute": "show-caption",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "captionText": {
                "type": "string",
                "attribute": "caption-text",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "showCaptionIcon": {
                "type": "boolean",
                "attribute": "show-caption-icon",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false
            },
            "required": {
                "type": "boolean",
                "attribute": "required",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "defaultValue": "false"
            }
        };
    }
    static get states() {
        return {
            "hasErrors": {}
        };
    }
    static get methods() {
        return {
            "setGroupError": {
                "complexType": {
                    "signature": "(error: boolean) => Promise<void>",
                    "parameters": [{
                            "name": "error",
                            "type": "boolean",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "ifxError",
                "method": "handleRadioButtonError",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
//# sourceMappingURL=radio-button-group.js.map
