import { h, } from "@stencil/core";
import { isNestedInIfxComponent } from "../..//shared/utils/dom-utils";
import { detectFramework } from "../..//shared/utils/framework-detection";
import { trackComponent } from "../../shared/utils/tracking";
export class CheckboxGroup {
    errorStates = new Map();
    el;
    /** Layout direction of the radio group */
    alignment = "vertical";
    /** Size of the radio group */
    size;
    /** Wether to show the group label. */
    showGroupLabel;
    /** Text used for the group label. */
    groupLabelText = "Group Label Text";
    /** Wether to show a caption under the group. */
    showCaption;
    /** Text used for the caption. */
    captionText;
    /** Wether to show an icon next to the caption. */
    showCaptionIcon;
    /** Wether selecting an option is required. */
    required = false;
    hasErrors = false;
    handleCheckboxError(event) {
        const checkbox = event.target;
        if (checkbox.tagName.toLowerCase() === "ifx-checkbox") {
            this.errorStates.set(checkbox, event.detail);
            this.updateHasErrors();
        }
    }
    /** Method to set the error state of all checkboxes in the group. */
    async setGroupError(error) {
        const checkboxes = Array.from(this.el.querySelectorAll("ifx-checkbox"));
        checkboxes.forEach((checkbox) => {
            checkbox.error = error;
        });
    }
    componentWillLoad() {
        this.initializeState();
    }
    async componentDidLoad() {
        if (!isNestedInIfxComponent(this.el)) {
            const framework = detectFramework();
            trackComponent("ifx-checkbox-group", await framework);
        }
    }
    handleSlotChange = () => {
        this.initializeState();
    };
    initializeState() {
        this.errorStates.clear();
        const checkboxes = Array.from(this.el.querySelectorAll("ifx-checkbox"));
        checkboxes.forEach((checkbox) => {
            if (!this.errorStates.has(checkbox)) {
                this.errorStates.set(checkbox, checkbox.error || false);
            }
        });
        this.updateHasErrors();
    }
    updateHasErrors() {
        this.hasErrors = Array.from(this.errorStates.values()).some((error) => error);
    }
    render() {
        return (h("div", { key: 'a1e3451daad606492f8c667a424bca92bcbf8968', class: "checkbox-group-container" }, this.showGroupLabel && (h("div", { key: '523e9597394ba4f5b91bae7345a7fd16898a3f97', class: "group-label" }, this.groupLabelText, this.required && (h("span", { key: '3f40ced787640ca76fb9961c2569bd2f26e43894', class: `required ${this.hasErrors ? "error" : ""}` }, "*")))), h("div", { key: '67eeb3658426d2e0510cc4e6c9b7249064efae76', class: `checkbox-group ${this.alignment} ${this.size}` }, h("slot", { key: '6941d0650ce6ac373e562d33962b1a761b336573', 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-checkbox-group"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["checkbox-group.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["checkbox-group.css"]
        };
    }
    static get properties() {
        return {
            "alignment": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "\"horizontal\" | \"vertical\"",
                    "resolved": "\"horizontal\" | \"vertical\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Layout direction of the radio group"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "alignment",
                "defaultValue": "\"vertical\""
            },
            "size": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Size of the radio group"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "size"
            },
            "showGroupLabel": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Wether to show the group label."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "show-group-label"
            },
            "groupLabelText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Text used for the group label."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "group-label-text",
                "defaultValue": "\"Group Label Text\""
            },
            "showCaption": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Wether to show a caption under the group."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "show-caption"
            },
            "captionText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Text used for the caption."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "caption-text"
            },
            "showCaptionIcon": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Wether to show an icon next to the caption."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "show-caption-icon"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Wether selecting an option is required."
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "required",
                "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": "Method to set the error state of all checkboxes in the group.",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get listeners() {
        return [{
                "name": "ifxError",
                "method": "handleCheckboxError",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
