'use strict';

var index = require('./index-DuCXzb-X.js');

const spwRadioCss = () => `html{line-height:1.3;-webkit-text-size-adjust:100%;}h1,h2,h3,h4,h5,h6,p,ul,li{padding:0;margin:0}ul{list-style:none}:host{box-sizing:border-box;line-height:1.3;-webkit-text-size-adjust:100%;font-size:100%}:host *,:host *::before,:host *::after{box-sizing:inherit;font-family:var(--spw-ds-font-main)}body{margin:0}hr{box-sizing:content-box;height:0;overflow:visible;}pre{font-family:monospace, monospace;font-size:1em;}a{background-color:transparent}abbr[title]{border-bottom:none;text-decoration:underline;text-decoration:underline dotted;}b,strong{font-weight:bolder}code,kbd,samp{font-family:monospace, monospace;font-size:1em;}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-0.25em}sup{top:-0.5em}img{border-style:none}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;line-height:1.15;margin:0;}button,input{overflow:visible}button,select{text-transform:none}button,[type=button],[type=reset],[type=submit]{-webkit-appearance:button}button::-moz-focus-inner,[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner{border-style:none;padding:0}button:-moz-focusring,[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring{outline:1px dotted ButtonText}fieldset{padding:0.35em 0.75em 0.625em}legend{box-sizing:border-box;color:inherit;display:table;max-width:100%;padding:0;white-space:normal;}progress{vertical-align:baseline}textarea{overflow:auto}[type=checkbox],[type=radio]{box-sizing:border-box;padding:0;}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px;}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit;}details{display:block}summary{display:list-item}template{display:none}[hidden]{display:none}:root{--spw-ds-container-xl:1232px;--spw-ds-container-lg:1024px;--spw-ds-container-md:768px;--spw-ds-container-sm:640px}.spw-ds-container{width:100%;padding-left:16px;padding-right:16px;margin-left:auto;margin-right:auto}@media (min-width: 460px){.spw-ds-container{max-width:var(--spw-ds-container-sm)}}@media (min-width: 768px){.spw-ds-container{max-width:var(--spw-ds-container-md)}}@media (min-width: 1024px){.spw-ds-container{max-width:var(--spw-ds-container-lg)}}@media (min-width: 1350px){.spw-ds-container{max-width:var(--spw-ds-container-xl)}}:host{display:inline-flex;vertical-align:top}.spw-radio{display:inline-flex;flex-direction:row;align-items:flex-start;padding:0}.spw-radio__wrapper{display:flex;position:relative;justify-content:center;align-items:center;width:20px;height:20px;background-color:var(--spw-ds-white);border:2px solid var(--spw-color-themes-grey-grey-900);border-radius:var(--spw-spacings-border-radius-full);font-size:var(--spw-typography-font-size-xs);flex:none;order:0;flex-grow:0}.spw-radio__wrapper--error{border-color:var(--spw-color-states-error-error-300)}.spw-radio__wrapper:not(.spw-radio__wrapper--disabled):hover{outline:4px solid var(--spw-color-themes-grey-grey-300)}.spw-radio__wrapper:not(.spw-radio__wrapper--disabled):focus-visible,.spw-radio__wrapper:not(.spw-radio__wrapper--disabled):active,.spw-radio__wrapper:not(.spw-radio__wrapper--disabled):focus-within{outline:2px solid white;outline-offset:0;box-shadow:0 0 0 4px black}.spw-radio__wrapper--disabled{border-color:var(--spw-color-themes-grey-grey-500)}.spw-radio__wrapper--checked{border-color:var(--spw-ds-primary);color:var(--spw-ds-primary)}.spw-radio__wrapper--checked--disabled{color:var(--spw-color-themes-grey-grey-500)}.spw-radio__circle{width:10px;height:10px;position:absolute;top:50%;left:50%;transform:translate(-50%, -50%);border-radius:var(--spw-spacings-border-radius-full);background:var(--spw-ds-primary);display:block}.spw-radio__label{color:var(--spw-color-themes-grey-grey-900);word-wrap:break-word;order:1;padding-top:1px;user-select:none;padding-left:var(--spw-spacings-spacing-sm)}.spw-radio__label:hover~.spw-radio__wrapper:not(.spw-radio__wrapper--disabled){outline:4px solid var(--spw-color-themes-grey-grey-300)}.spw-radio__label:active~.spw-radio__wrapper:not(.spw-radio__wrapper--disabled){outline:2px solid white;outline-offset:0;box-shadow:0 0 0 4px black}.spw-radio__label--disabled{color:var(--spw-color-themes-grey-grey-600)}.spw-radio__wrapper:hover,.spw-radio__label:hover{cursor:pointer}.spw-radio__wrapper--disabled:hover,.spw-radio__label--disabled:hover{cursor:not-allowed}`;

const SpwRadio = class {
    constructor(hostRef) {
        index.registerInstance(this, hostRef);
        this.spwChange = index.createEvent(this, "spwChange", 7);
        if (hostRef.$hostElement$["s-ei"]) {
            this.internals = hostRef.$hostElement$["s-ei"];
        }
        else {
            this.internals = hostRef.$hostElement$.attachInternals();
            hostRef.$hostElement$["s-ei"] = this.internals;
        }
        /** 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 (index.h("div", { key: 'a781adf38f9770b007e32e827af954e56a30c7f2', class: "spw-radio" }, index.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 && (index.h("label", { key: '8c06d8b2581204f2ddbdbd1440694052039498a0', class: `spw-radio__label ${this.disabled ? 'spw-radio__label--disabled' : ''}`, onClick: e => this.handleRadio(e) }, index.h("slot", { key: '9434b6826bc54470f91e5529acf6150348b8b437' }))), index.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 && index.h("span", { key: '16d4f2247dd961d5d9f5c9dfccc600dab309548d', class: "spw-radio__circle" }))));
    }
    static get formAssociated() { return true; }
    get el() { return index.getElement(this); }
    static get watchers() { return {
        "checked": [{
                "checkedChanged": 0
            }]
    }; }
};
SpwRadio.style = spwRadioCss();

exports.spw_radio = SpwRadio;
