import { h, Host } from "@stencil/core";
import { randomIdGenerator } from "../../../global/utils/randomIdGenerator/randomIdGenerator.util";
/**
 * @slot radiobuttons
 *
 * @swedishName Radiogrupp
 */
export class FormRadiogroup {
    constructor() {
        this.radiobuttons = undefined;
        this.afValue = undefined;
        this.value = undefined;
        this.afName = randomIdGenerator('digi-form-radiogroup');
    }
    OnAfValueChange(value) {
        this.value = value;
    }
    OnValueChange(value) {
        this.afValue = value;
        this.setRadiobuttons(value);
    }
    radiobuttonChangeListener(e) {
        if (Array.from(this.radiobuttons).includes(e.target)) {
            e.preventDefault();
            this.afValue = e.detail.target.value;
            this.afOnGroupChange.emit(e);
        }
    }
    componentDidLoad() {
        this.setRadiobuttons(this.value || this.afValue);
        if (this.value)
            this.afValue = this.value;
    }
    setRadiobuttons(value) {
        this.radiobuttons = this.hostElement.querySelectorAll('digi-form-radiobutton');
        this.radiobuttons.forEach((r) => {
            r.afName = this.afName;
            if (value && r.afValue == value) {
                r.setAttribute('af-checked', 'true');
            }
        });
    }
    render() {
        return (h(Host, { key: 'f08c5f7fef0959450fdfac51eb5f65d942f4c857' }, h("div", { key: 'f5a83a8d456f8d8e98bf1614720813d66f5db924', class: {
                'digi-form-radiogroup': true
            } }, h("digi-util-mutation-observer", { key: 'd58acfea2c0554233188a6572a35f58312d24135', onAfOnChange: (e) => this.radiobuttonChangeListener(e) }, h("slot", { key: '6e6fc7086a86f8b5d0b57cd4ad4e8540b212da38' })))));
    }
    static get is() { return "digi-form-radiogroup"; }
    static get encapsulation() { return "scoped"; }
    static get originalStyleUrls() {
        return {
            "$": ["form-radiogroup.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["form-radiogroup.css"]
        };
    }
    static get properties() {
        return {
            "afValue": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Radiogroup value"
                        }],
                    "text": "V\u00E4rdet p\u00E5 radiogruppen"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-value",
                "reflect": false
            },
            "value": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Radiogroup value"
                        }, {
                            "name": "ignore",
                            "text": undefined
                        }],
                    "text": "V\u00E4rdet p\u00E5 radiogruppen"
                },
                "getter": false,
                "setter": false,
                "attribute": "value",
                "reflect": false
            },
            "afName": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Radiogroup name"
                        }],
                    "text": "Namnet p\u00E5 radiogruppen"
                },
                "getter": false,
                "setter": false,
                "attribute": "af-name",
                "reflect": false,
                "defaultValue": "randomIdGenerator('digi-form-radiogroup')"
            }
        };
    }
    static get states() {
        return {
            "radiobuttons": {}
        };
    }
    static get events() {
        return [{
                "method": "afOnGroupChange",
                "name": "afOnGroupChange",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [{
                            "name": "en",
                            "text": "Event when value changes"
                        }],
                    "text": "Event n\u00E4r v\u00E4rdet \u00E4ndras"
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "hostElement"; }
    static get watchers() {
        return [{
                "propName": "afValue",
                "methodName": "OnAfValueChange"
            }, {
                "propName": "value",
                "methodName": "OnValueChange"
            }];
    }
}
