import { h } from "@stencil/core";
export class BpSdkRadioInput {
    constructor() {
        this.answerId = undefined;
        this.questionId = undefined;
        this.fieldType = undefined;
        this.code = undefined;
        this.label = undefined;
        this.labelledBy = undefined;
        this.choices = [];
        this.choiceId = undefined;
        this.errors = undefined;
        this.errorText = undefined;
        this.touched = false;
        this.pristine = true;
        this.required = undefined;
        this.isCompleted = false;
        this.isReadOnly = false;
        this.helpText = undefined;
        this.answeredBySource = undefined;
        this.controlClasses = '';
        this._choiceId = undefined;
        this.useExternal = false;
    }
    onControlChange() {
        const touched = this.touched ? ' bp-sdk-touched' : '';
        this.controlClasses = `bp-sdk-radio__label${touched}`;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-radio-input');
        customElements.whenDefined('bp-sdk-external-radio-input').then(() => {
            this.useExternal = true;
        });
        this.onChoiceIdChange(this.choiceId);
        this.onControlChange();
    }
    onChoiceIdChange(newValue) {
        this._choiceId = newValue;
    }
    onChange(new_value) {
        if (new_value !== this.choiceId) {
            this.removeActiveAnswer.emit();
            this.valueUpdate.emit(new_value);
        }
    }
    renderChoice(choice) {
        // The addition of labelled by ensures this row will be unique within question pools
        const id = `bp-sdk-radio__choice-${this.labelledBy}-${this.code}-${choice.id}`;
        return (h("span", null, h("input", { name: this.code, id: id, "aria-labelledby": this.labelledBy, class: "bp-sdk-radio__choice", type: "radio", value: choice.id, checked: this._choiceId === choice.id, onChange: () => {
                this._choiceId = choice.id;
            } }), h("label", { htmlFor: id, class: this.controlClasses }, h("span", null, choice.value))));
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-radio-input", { answeredBySource: this.answeredBySource, answerId: this.answerId, choiceId: this.choiceId, choices: this.choices, code: this.code, errorText: this.errorText, errors: this.errors, fieldType: this.fieldType, helpText: this.helpText, isCompleted: this.isCompleted, isReadOnly: this.isReadOnly, label: this.label, labelledBy: this.labelledBy, pristine: this.pristine, questionId: this.questionId, required: this.required, touched: this.touched }));
    }
    renderInternal() {
        var _a;
        let input;
        if (this.isReadOnly) {
            const selectedChoice = (this.choices || []).find((choice) => this.choiceId === choice.id);
            input = this.renderReadOnlyElement(selectedChoice === null || selectedChoice === void 0 ? void 0 : selectedChoice.value);
        }
        else {
            input = h("div", { class: "bp-sdk-radio__choice-list" }, this.choices.map((choice) => this.renderChoice(choice)));
        }
        return (h("div", { class: "bp-sdk-radio__container" }, h("div", { role: "group", class: "bp-sdk-radio__wrapper" }, h("fieldset", null, h("legend", { class: "bp-sdk-radio__text" }, h("div", { innerHTML: this.label })), input, this.touched && !this.pristine && ((this.errorText && !this.isCompleted) || !!((_a = this.errors) === null || _a === void 0 ? void 0 : _a.length)) && (h("bp-sdk-errors-container", { answerId: this.answerId, questionId: this.questionId, fieldType: this.fieldType, errors: this.errors, errorText: this.errorText }))))));
    }
    renderReadOnlyElement(value) {
        return h("span", { class: "read-only-value" }, value);
    }
    static get is() { return "bp-sdk-radio-input"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-radio-input.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-radio-input.css"]
        };
    }
    static get properties() {
        return {
            "answerId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Answer id"
                },
                "attribute": "answer-id",
                "reflect": false
            },
            "questionId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question id"
                },
                "attribute": "question_id",
                "reflect": false
            },
            "fieldType": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Field type"
                },
                "attribute": "field_type",
                "reflect": false
            },
            "code": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "question code"
                },
                "attribute": "code",
                "reflect": false
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Label to display"
                },
                "attribute": "label",
                "reflect": false
            },
            "labelledBy": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Labels that describe group"
                },
                "attribute": "labelled-by",
                "reflect": false
            },
            "choices": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IChoice[]",
                    "resolved": "IChoice[]",
                    "references": {
                        "IChoice": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IChoice"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Choices to display"
                },
                "defaultValue": "[]"
            },
            "choiceId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Selected choice"
                },
                "attribute": "choice_id",
                "reflect": false
            },
            "errors": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "string[]",
                    "resolved": "string[]",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error messages"
                }
            },
            "errorText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error text"
                },
                "attribute": "error_text",
                "reflect": false
            },
            "touched": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is touched"
                },
                "attribute": "touched",
                "reflect": false,
                "defaultValue": "false"
            },
            "pristine": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is pristine"
                },
                "attribute": "pristine",
                "reflect": false,
                "defaultValue": "true"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is required"
                },
                "attribute": "required",
                "reflect": false
            },
            "isCompleted": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is answer in complete state"
                },
                "attribute": "is_completed",
                "reflect": false,
                "defaultValue": "false"
            },
            "isReadOnly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "When true, the field will be readonly. It will display only the label and value as plain text."
                },
                "attribute": "is-read-only",
                "reflect": false,
                "defaultValue": "false"
            },
            "helpText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Help text"
                },
                "attribute": "help-text",
                "reflect": false
            },
            "answeredBySource": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | undefined",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Specifically how the question was answered (ie. \"ACORD 126\")"
                },
                "attribute": "answered-by-source",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "controlClasses": {},
            "_choiceId": {},
            "useExternal": {}
        };
    }
    static get events() {
        return [{
                "method": "valueUpdate",
                "name": "valueUpdate",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }, {
                "method": "removeActiveAnswer",
                "name": "removeActiveAnswer",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "void",
                    "resolved": "void",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "touched",
                "methodName": "onControlChange"
            }, {
                "propName": "choiceId",
                "methodName": "onChoiceIdChange"
            }, {
                "propName": "_choiceId",
                "methodName": "onChange"
            }];
    }
}
