import { isAutocompleteEnabled } from "@boldpenguin/sdk-redux";
import { QuestionReferenceType } from "@boldpenguin/sdk-types";
import { getAutocomplete } from "@boldpenguin/sdk-utility";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
import { VehicleVinService } from "../../services/vehicle-vin.service";
export class BpSdkTextInputContainer {
    constructor() {
        this.handleSubscriptions = () => {
            this.storeUnsubscribe && this.storeUnsubscribe();
            this.storeUnsubscribe = store.mapStateToProps(this, this.mapStateToProps);
        };
        this.mapStateToProps = (state) => {
            return {
                isAutocompleteEnabled: isAutocompleteEnabled(state),
            };
        };
        this.answerId = undefined;
        this.questionId = 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.isAutocompleteEnabled = undefined;
        this.helpText = undefined;
        this.answeredBySource = undefined;
        this.autoComplete = undefined;
        this.useExternal = false;
        this._vinErrors = [];
    }
    resetState() {
        this._vinErrors = [];
    }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-text-input-container');
        customElements.whenDefined('bp-sdk-external-text-input-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType: this.fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
        this.onValueUpdate({
            detail: this.text,
        });
    }
    onValueUpdate(evt) {
        if (this.referenceType === QuestionReferenceType.CommercialAutoVIN) {
            if (VehicleVinService.isValidVin(evt.detail)) {
                this._vinErrors = [];
                this.errorText = undefined;
            }
            else {
                this.touched = true;
                this.pristine = false;
                this._vinErrors = [VehicleVinService.getValidationErrorMessage(evt.detail)];
                this.errorText = VehicleVinService.getValidationErrorMessage(evt.detail);
            }
        }
    }
    onReferenceTypeChange(referenceType) {
        this.updateAutocomplete({
            referenceType,
            code: this.code,
            fieldType: this.fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
    }
    onCodeChange(code) {
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code,
            fieldType: this.fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
    }
    onFieldTypeChange(fieldType) {
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
    }
    onIsAutocompleteEnabledChange(isAutocompleteEnabled) {
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType: this.fieldType,
            isAutocompleteEnabled,
        });
    }
    updateAutocomplete({ code, fieldType, referenceType, isAutocompleteEnabled, }) {
        this.autoComplete = isAutocompleteEnabled ? getAutocomplete({ referenceType, code, fieldType }) : undefined;
    }
    disconnectedCallback() {
        this.storeUnsubscribe && this.storeUnsubscribe();
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-text-input-container", { answerId: this.answerId, code: this.code, errorText: this.errorText, errors: [...this._vinErrors, ...this.errors], fieldType: this.fieldType, helpText: this.helpText, answeredBySource: this.answeredBySource, isAutocompleteEnabled: this.isAutocompleteEnabled, isCompleted: this.isCompleted, isEnabled: this.isEnabled, isReadOnly: this.isReadOnly, labelId: this.labelId, label: this.label, 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 }));
    }
    renderInternal() {
        return [
            h("bp-sdk-text-input", { answerId: this.answerId, autoComplete: this.autoComplete, code: this.code, errorText: this.errorText, errors: [...this._vinErrors, ...this.errors], fieldType: this.fieldType, helpText: this.helpText, answeredBySource: this.answeredBySource, isCompleted: this.isCompleted, isEnabled: this.isEnabled, isReadOnly: this.isReadOnly, labelId: this.labelId, label: this.label, labelledBy: this.labelledBy, max: this.max, min: this.min, pattern: this.pattern, placeholder: this.placeholder, pristine: this.pristine, questionId: this.questionId, required: this.required, text: this.text, touched: this.touched }),
        ];
    }
    static get is() { return "bp-sdk-text-input-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["./bp-sdk-text-input-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-text-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
            },
            "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_value",
                "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_value",
                "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
            },
            "isAutocompleteEnabled": {
                "type": "boolean",
                "mutable": true,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is autocomplete enabled"
                },
                "attribute": "is-autocomplete-enabled",
                "reflect": 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 {
            "autoComplete": {},
            "useExternal": {},
            "_vinErrors": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "referenceType",
                "methodName": "onReferenceTypeChange"
            }, {
                "propName": "code",
                "methodName": "onCodeChange"
            }, {
                "propName": "fieldType",
                "methodName": "onFieldTypeChange"
            }, {
                "propName": "isAutocompleteEnabled",
                "methodName": "onIsAutocompleteEnabledChange"
            }];
    }
    static get listeners() {
        return [{
                "name": "valueUpdate",
                "method": "onValueUpdate",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
