import { h } from "@stencil/core";
export class SpwRadio {
    constructor() {
        /** Indique si le bouton radio est désactivé */
        this.disabled = false;
        /** Indique si le bouton radio est sélectionné (coché), mutable et reflété dans le DOM */
        this.checked = false;
        /** Indique s'il y a une erreur visuelle associée au bouton radio */
        this.error = false;
    }
    handleRadio(event) {
        if (this.disabled) {
            event === null || event === void 0 ? void 0 : event.preventDefault();
            event === null || event === void 0 ? void 0 : event.stopPropagation();
            return;
        }
        if (!this.internalChecked) {
            this.setCheckedState(true);
            this.spwChange.emit({ checked: this.internalChecked, value: this.value });
            this.uncheckOthers();
        }
    }
    uncheckOthers() {
        const form = this.el.closest('form');
        if (form) {
            const radios = Array.from(form.querySelectorAll(`spw-radio[name="${this.name}"]`));
            radios.forEach(radio => {
                if (radio !== this.el) {
                    radio.setChecked(false);
                }
            });
        }
    }
    /** Méthode pour changer l'état de la case à cocher. */
    async setChecked(checked) {
        if (this.disabled)
            return;
        this.setCheckedState(checked);
    }
    checkedChanged(newValue) {
        this.setCheckedState(newValue);
        if (newValue) {
            this.uncheckOthers();
        }
    }
    setCheckedState(checked) {
        this.internalChecked = checked;
        this.checked = checked;
        if (this.inputElement) {
            this.inputElement.checked = checked;
        }
        this.updateFormValue();
    }
    updateFormValue() {
        if (this.internalChecked) {
            this.internals.setFormValue(this.value);
        }
        else {
            this.internals.setFormValue(null);
        }
    }
    handleKeydown(event) {
        if (event.key === ' ' || event.key === 'Enter') {
            this.handleRadio();
            event.preventDefault();
        }
    }
    componentWillLoad() {
        this.internalChecked = this.checked;
        this.defaultChecked = this.checked;
    }
    componentDidLoad() {
        var _a;
        this.setCheckedState(this.internalChecked);
        (_a = this.el.closest('form')) === null || _a === void 0 ? void 0 : _a.addEventListener('formdata', (e) => {
            if (this.internalChecked) {
                e.formData.set(this.name, this.value);
            }
        });
    }
    formResetCallback() {
        this.setCheckedState(this.defaultChecked);
    }
    get elementClass() {
        return {
            'spw-radio__wrapper': true,
            'spw-radio__wrapper--disabled': this.disabled,
            'spw-radio__wrapper--checked': this.internalChecked,
            'spw-radio__wrapper--error': this.error,
        };
    }
    render() {
        const slot = this.el.innerHTML;
        const hasSlot = Boolean(slot);
        return (h("div", { key: 'a781adf38f9770b007e32e827af954e56a30c7f2', class: "spw-radio" }, h("input", { key: '8a69300ce3b900c0ee7ea36253cf7ec816f70398', type: "radio", ref: el => (this.inputElement = el), checked: this.internalChecked, "aria-checked": this.internalChecked ? 'true' : 'false', onChange: this.handleRadio.bind(this), name: this.name, value: this.value, disabled: this.disabled, style: { display: 'none' } }), hasSlot && (h("label", { key: '8c06d8b2581204f2ddbdbd1440694052039498a0', class: `spw-radio__label ${this.disabled ? 'spw-radio__label--disabled' : ''}`, onClick: e => this.handleRadio(e) }, h("slot", { key: '9434b6826bc54470f91e5529acf6150348b8b437' }))), h("div", { key: '34ce75e3e5ef4bfbc3893d77fa585ad952770275', tabIndex: this.disabled ? -1 : 0, onClick: e => this.handleRadio(e), onKeyDown: this.handleKeydown.bind(this), role: "radio", "aria-checked": this.internalChecked ? 'true' : 'false', "aria-disabled": this.disabled, "aria-label": this.accAriaLabel || this.name, class: this.elementClass }, this.internalChecked && h("span", { key: '16d4f2247dd961d5d9f5c9dfccc600dab309548d', class: "spw-radio__circle" }))));
    }
    static get is() { return "spw-radio"; }
    static get encapsulation() { return "shadow"; }
    static get formAssociated() { return true; }
    static get originalStyleUrls() {
        return {
            "$": ["spw-radio.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["spw-radio.css"]
        };
    }
    static get properties() {
        return {
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Indique si le bouton radio est d\u00E9sactiv\u00E9"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "disabled",
                "defaultValue": "false"
            },
            "checked": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Indique si le bouton radio est s\u00E9lectionn\u00E9 (coch\u00E9), mutable et refl\u00E9t\u00E9 dans le DOM"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "checked",
                "defaultValue": "false"
            },
            "error": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Indique s'il y a une erreur visuelle associ\u00E9e au bouton radio"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "error",
                "defaultValue": "false"
            },
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Nom du bouton radio, utilis\u00E9 pour le grouper avec d'autres boutons radio dans un formulaire"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "name"
            },
            "value": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Valeur associ\u00E9e au bouton radio"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "value"
            },
            "accAriaLabel": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Remplit l'attribut aria-label, utilis\u00E9 \u00E0 des fins d'accessibilit\u00E9"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "acc-aria-label"
            }
        };
    }
    static get states() {
        return {
            "internalChecked": {}
        };
    }
    static get events() {
        return [{
                "method": "spwChange",
                "name": "spwChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "\u00C9v\u00E9nement d\u00E9clench\u00E9 lorsque l'\u00E9tat du bouton radio change (coch\u00E9 ou d\u00E9coch\u00E9)"
                },
                "complexType": {
                    "original": "{ checked: boolean; value: string }",
                    "resolved": "{ checked: boolean; value: string; }",
                    "references": {}
                }
            }];
    }
    static get methods() {
        return {
            "setChecked": {
                "complexType": {
                    "signature": "(checked: boolean) => Promise<void>",
                    "parameters": [{
                            "name": "checked",
                            "type": "boolean",
                            "docs": ""
                        }],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<void>"
                },
                "docs": {
                    "text": "M\u00E9thode pour changer l'\u00E9tat de la case \u00E0 cocher.",
                    "tags": []
                }
            }
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "checked",
                "methodName": "checkedChanged"
            }];
    }
    static get attachInternalsMemberName() { return "internals"; }
}
