import { NativeValidationStrategy } from '../../../utils/native-validation-strategy';
let radioGroupId = 0;
export class RadioGroup {
    constructor() {
        this.inputId = `anj-rg-${radioGroupId++}`;
        this.radios = [];
        /**
         * Make the control read only
         */
        this.disabled = false;
        /**
         * The input name name
         */
        this.name = this.inputId;
        /**
         * @internal
         * The validation strategy to use
         */
        this.validationStrategy = new NativeValidationStrategy();
    }
    disabledChanged() {
        this.radios.forEach(control => {
            control.disabled = this.disabled;
        });
    }
    onInvalidChange() {
        this.radios.forEach(r => r.invalid = this.invalid);
    }
    valueChanged(value) {
        this.updateRadios();
        this.checkValidity();
        this.anjChange.emit({ value });
    }
    onRadioDidLoad(event) {
        const target = event.target;
        target.name = this.name;
        target.required = this.required;
        this.radios.push(target);
        if (!this.value && target.checked) {
            this.value = target.value;
        }
        else {
            this.updateRadios();
        }
    }
    onRadioDidUnload(event) {
        const target = event.target;
        this.radios = this.radios.filter((radio) => {
            return radio.value !== target.value;
        });
    }
    onRadioSelect(event) {
        const target = event.target;
        this.value = target.value;
    }
    onValidChange() {
        this.checkValidity();
    }
    /**
     * Checks the native element validity use native validators
     */
    async checkValidity() {
        let valid = false;
        await new Promise(resolve => {
            this.radios.forEach(async (radio) => {
                valid = await radio.checkValidity();
            });
            resolve();
        });
        this.invalid = !valid;
        return valid;
    }
    updateRadios() {
        this.radios.forEach((radio) => {
            radio.checked = radio.value === this.value;
        });
    }
    static get is() { return "anj-radio-group"; }
    static get originalStyleUrls() { return {
        "$": ["radio-group.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["radio-group.css"]
    }; }
    static get properties() { return {
        "disabled": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Make the control read only"
            },
            "attribute": "disabled",
            "reflect": false,
            "defaultValue": "false"
        },
        "invalid": {
            "type": "boolean",
            "mutable": true,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Whether the control is valid"
            },
            "attribute": "invalid",
            "reflect": true
        },
        "name": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "The input name name"
            },
            "attribute": "name",
            "reflect": false,
            "defaultValue": "this.inputId"
        },
        "required": {
            "type": "boolean",
            "mutable": false,
            "complexType": {
                "original": "boolean",
                "resolved": "boolean",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Is it required"
            },
            "attribute": "required",
            "reflect": false
        },
        "validationStrategy": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "NativeValidationStrategy",
                "resolved": "NativeValidationStrategy",
                "references": {
                    "NativeValidationStrategy": {
                        "location": "import",
                        "path": "../../../utils/native-validation-strategy"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [{
                        "text": "The validation strategy to use",
                        "name": "internal"
                    }],
                "text": ""
            },
            "defaultValue": "new NativeValidationStrategy()"
        },
        "value": {
            "type": "any",
            "mutable": true,
            "complexType": {
                "original": "any | null",
                "resolved": "any",
                "references": {}
            },
            "required": false,
            "optional": true,
            "docs": {
                "tags": [],
                "text": "the value of the radio group."
            },
            "attribute": "value",
            "reflect": false
        }
    }; }
    static get events() { return [{
            "method": "anjChange",
            "name": "anjChange",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Emitted when the value has changed."
            },
            "complexType": {
                "original": "InputChangeEvent",
                "resolved": "InputChangeEvent",
                "references": {
                    "InputChangeEvent": {
                        "location": "import",
                        "path": "../../../types/types"
                    }
                }
            }
        }]; }
    static get methods() { return {
        "checkValidity": {
            "complexType": {
                "signature": "() => Promise<boolean>",
                "parameters": [],
                "references": {
                    "Promise": {
                        "location": "global"
                    },
                    "HTMLAnjRadioElement": {
                        "location": "global"
                    }
                },
                "return": "Promise<boolean>"
            },
            "docs": {
                "text": "Checks the native element validity use native validators",
                "tags": []
            }
        }
    }; }
    static get watchers() { return [{
            "propName": "disabled",
            "methodName": "disabledChanged"
        }, {
            "propName": "invalid",
            "methodName": "onInvalidChange"
        }, {
            "propName": "value",
            "methodName": "valueChanged"
        }]; }
    static get listeners() { return [{
            "name": "anjRadioDidLoad",
            "method": "onRadioDidLoad",
            "target": undefined,
            "capture": false,
            "passive": false
        }, {
            "name": "anjRadioDidUnload",
            "method": "onRadioDidUnload",
            "target": undefined,
            "capture": false,
            "passive": false
        }, {
            "name": "anjSelect",
            "method": "onRadioSelect",
            "target": undefined,
            "capture": false,
            "passive": false
        }, {
            "name": "anjInvalid",
            "method": "onValidChange",
            "target": undefined,
            "capture": false,
            "passive": false
        }]; }
}
