import { QuestionReferenceType } from "@boldpenguin/sdk-types";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
import { findActiveAnswersByGroup } from "@boldpenguin/sdk-redux";
import { isDriverLicenseValidForState } from "@boldpenguin/sdk-utility";
export class BpSdkDriverInputContainer {
    constructor() {
        this.mapStateToProps = (state) => {
            return {
                driverLicenseStateAnswer: findActiveAnswersByGroup(state, {
                    poolId: this.questionPoolId,
                    groupId: this.questionGroupId,
                }).find((answer) => answer.question.reference_type === QuestionReferenceType.DriversLicenseState),
            };
        };
        this.answerId = undefined;
        this.questionId = undefined;
        this.questionGroupId = undefined;
        this.questionPoolId = undefined;
        this.text = undefined;
        this.labelId = undefined;
        this.fieldType = 'text';
        this.placeholder = '';
        this.pattern = undefined;
        this.code = undefined;
        this.required = false;
        this.min = -1;
        this.max = -1;
        this.errors = [];
        this.errorText = undefined;
        this.touched = false;
        this.pristine = true;
        this.isCompleted = false;
        this.isReadOnly = false;
        this.label = undefined;
        this.labelledBy = undefined;
        this.isEnabled = true;
        this.referenceType = undefined;
        this.driverLicenseStateAnswer = undefined;
        this.helpText = undefined;
        this.answeredBySource = undefined;
        this.useExternal = false;
        this.licenseError = undefined;
    }
    resetState() {
        this.licenseError = undefined;
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-driver-input-container');
        customElements.whenDefined('bp-sdk-external-driver-input-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
    }
    handleSubscriptions() {
        this.storeUnsubscribe && this.storeUnsubscribe();
        this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    onSaveAnswer(e) {
        var _a, _b;
        if (this.useExternal) {
            return;
        }
        const state = (_b = (_a = this.driverLicenseStateAnswer) === null || _a === void 0 ? void 0 : _a.question.choices.find((choice) => choice.id === this.driverLicenseStateAnswer.choice_id)) === null || _b === void 0 ? void 0 : _b.alias;
        const detail = e.detail;
        this.licenseError =
            !state || !detail || isDriverLicenseValidForState(detail, state) ? undefined : BpSdkDriverInputContainer.ERROR_MESSAGE;
        if (this.licenseError) {
            e.stopPropagation();
            this.fakeValueUpdate.emit();
        }
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-driver-input-container", { answerId: this.answerId, answeredBySource: this.answeredBySource, code: this.code, driverLicenseStateAnswer: this.driverLicenseStateAnswer, errors: this.errors, errorText: this.errorText, fieldType: this.fieldType, helpText: this.helpText, isCompleted: this.isCompleted, isEnabled: this.isEnabled, isReadOnly: this.isReadOnly, label: this.label, labelId: this.labelId, labelledBy: this.labelledBy, max: this.max, min: this.min, pattern: this.pattern, placeholder: this.placeholder, pristine: this.pristine, questionGroupId: this.questionGroupId, questionId: this.questionId, questionPoolId: this.questionPoolId, referenceType: this.referenceType, required: this.required, text: this.text, touched: this.touched }));
    }
    renderInternal() {
        var _a;
        return (h("bp-sdk-text-input-container", { answerId: this.answerId, answeredBySource: this.answeredBySource, code: this.code, errors: this.errors, errorText: (_a = this.licenseError) !== null && _a !== void 0 ? _a : this.errorText, fieldType: this.fieldType, helpText: this.helpText,
            // TODO: remove this if isCompleted is updated for this validation condition
            isCompleted: !this.licenseError && this.isCompleted, isReadOnly: this.isReadOnly, label: this.label, labelId: this.labelId, labelledBy: this.labelledBy, max: this.max, min: this.min, pattern: this.pattern, placeholder: this.placeholder, pristine: this.pristine, questionId: this.questionId, referenceType: this.referenceType, required: this.required, text: this.text, touched: this.touched }));
    }
    static get is() { return "bp-sdk-driver-input-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-driver-input-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-driver-input-container.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
            },
            "questionGroupId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question group id"
                },
                "attribute": "question-group-id",
                "reflect": false
            },
            "questionPoolId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question pool id"
                },
                "attribute": "question-pool-id",
                "reflect": false
            },
            "text": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Initial text value"
                },
                "attribute": "text",
                "reflect": false
            },
            "labelId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Aria label id's that describes input for screen readers"
                },
                "attribute": "label-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,
                "defaultValue": "'text'"
            },
            "placeholder": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Placeholder to display"
                },
                "attribute": "placeholder",
                "reflect": false,
                "defaultValue": "''"
            },
            "pattern": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string | undefined",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Regular expression pattern input should match"
                },
                "attribute": "pattern",
                "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
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is question required"
                },
                "attribute": "required",
                "reflect": false,
                "defaultValue": "false"
            },
            "min": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "minimum length of input"
                },
                "attribute": "min",
                "reflect": false,
                "defaultValue": "-1"
            },
            "max": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "maximum length of input"
                },
                "attribute": "max",
                "reflect": false,
                "defaultValue": "-1"
            },
            "errors": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "string[]",
                    "resolved": "string[]",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error messages"
                },
                "defaultValue": "[]"
            },
            "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"
            },
            "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"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Label"
                },
                "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
            },
            "isEnabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is enabled"
                },
                "attribute": "is-enabled",
                "reflect": false,
                "defaultValue": "true"
            },
            "referenceType": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Reference type"
                },
                "attribute": "reference-type",
                "reflect": false
            },
            "driverLicenseStateAnswer": {
                "type": "unknown",
                "mutable": true,
                "complexType": {
                    "original": "IAnswer | undefined",
                    "resolved": "IAnswer",
                    "references": {
                        "IAnswer": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IAnswer"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Driver license state answer injected by redux"
                }
            },
            "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 {
            "useExternal": {},
            "licenseError": {}
        };
    }
    static get events() {
        return [{
                "method": "fakeValueUpdate",
                "name": "fakeValueUpdate",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Value change event"
                },
                "complexType": {
                    "original": "void",
                    "resolved": "void",
                    "references": {}
                }
            }];
    }
    static get watchers() {
        return [{
                "propName": "questionGroupId",
                "methodName": "handleSubscriptions"
            }, {
                "propName": "questionPoolId",
                "methodName": "handleSubscriptions"
            }];
    }
    static get listeners() {
        return [{
                "name": "valueUpdate",
                "method": "onSaveAnswer",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
BpSdkDriverInputContainer.ERROR_MESSAGE = `Driver's license is invalid for state`;
