import { isAutocompleteEnabled } from "@boldpenguin/sdk-redux";
import { QuestionFieldType } from "@boldpenguin/sdk-types";
import { getAutocomplete } from "@boldpenguin/sdk-utility";
import { h } from "@stencil/core";
import store from "../../services/redux-store.service";
export class BpSdkMaskedInputContainer {
    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.labelId = undefined;
        this.errors = [];
        this.code = undefined;
        this.errorText = undefined;
        this.isCompleted = undefined;
        this.isReadOnly = false;
        this.min = undefined;
        this.minDate = undefined;
        this.max = undefined;
        this.maxDate = undefined;
        this.fieldType = undefined;
        this.labelledBy = undefined;
        this.pattern = undefined;
        this.required = false;
        this.referenceType = undefined;
        this.value = undefined;
        this.placeholder = '';
        this.touched = false;
        this.pristine = true;
        this.questionId = undefined;
        this.label = undefined;
        this.isAutocompleteEnabled = undefined;
        this.helpText = undefined;
        this.answeredBySource = undefined;
        this.canHide = false;
        this.useComplexMaskLogic = false;
        this.mask = undefined;
        this.options = undefined;
        this.autoComplete = undefined;
        this.useExternal = false;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
        this.handleSubscriptions();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-masked-input-container');
        customElements.whenDefined('bp-sdk-external-masked-input-container').then(() => {
            this.useExternal = true;
        });
        this.handleSubscriptions();
        this.onFieldTypeChange(this.fieldType);
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType: this.fieldType,
            isAutocompleteEnabled: this.isAutocompleteEnabled,
        });
    }
    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,
        });
    }
    onIsAutocompleteEnabledChange(isAutocompleteEnabled) {
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType: this.fieldType,
            isAutocompleteEnabled,
        });
    }
    onFieldTypeChange(fieldType) {
        let mask;
        let useComplexMaskLogic = false;
        let canHide = false;
        let options;
        switch (fieldType) {
            case QuestionFieldType.Phone:
                mask = '(999) 999-9999';
                options = {
                    placeholder: '#',
                };
                break;
            case QuestionFieldType.Percentage:
                mask = '999%';
                options = {
                    placeholder: ' ',
                };
                break;
            case QuestionFieldType.Currency:
                mask = 'integer';
                options = {
                    // unlike percentage, currency placeholder must be empty
                    // otherwise, inputmask will show "$integer" for placeholder
                    placeholder: '',
                    prefix: '$',
                };
                break;
            case QuestionFieldType.Date:
                mask = 'datetime';
                options = {
                    placeholder: '#',
                    inputFormat: 'mm/dd/yyyy',
                };
                useComplexMaskLogic = true;
                canHide = true;
                break;
            case QuestionFieldType.SocialSecurityNumber:
                mask = '999-99-9999';
                options = {
                    placeholder: '#',
                };
                useComplexMaskLogic = true;
                canHide = true;
                break;
            case QuestionFieldType.FederalEmployeeIdentification:
                mask = '99-9999999';
                options = {
                    placeholder: '#',
                };
                useComplexMaskLogic = true;
                canHide = true;
                break;
        }
        this.mask = mask;
        this.options = options;
        this.canHide = canHide;
        this.useComplexMaskLogic = useComplexMaskLogic;
        this.updateAutocomplete({
            referenceType: this.referenceType,
            code: this.code,
            fieldType,
            isAutocompleteEnabled: this.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-masked-input-container", { answerId: this.answerId, labelId: this.labelId, code: this.code, errorText: this.errorText, errors: this.errors, fieldType: this.fieldType, helpText: this.helpText, answeredBySource: this.answeredBySource, isCompleted: this.isCompleted, isReadOnly: this.isReadOnly, label: this.label, labelledBy: this.labelledBy, mask: this.mask, maxDate: this.maxDate, max: this.max, minDate: this.minDate, min: this.min, options: this.options, pattern: this.pattern, placeholder: this.placeholder, pristine: this.pristine, questionId: this.questionId, referenceType: this.referenceType, required: this.required, shouldHideOnBlur: this.canHide, touched: this.touched, useComplexMaskLogic: this.useComplexMaskLogic, value: this.value, isAutocompleteEnabled: this.isAutocompleteEnabled }));
    }
    renderInternal() {
        return (h("bp-sdk-masked-input", { answerId: this.answerId, autoComplete: this.autoComplete, canHide: this.canHide, code: this.code, errors: this.errors, errorText: this.errorText, fieldType: this.fieldType, helpText: this.helpText, answeredBySource: this.answeredBySource, isCompleted: this.isCompleted, isReadOnly: this.isReadOnly, label: this.label, labelId: this.labelId, labelledBy: this.labelledBy, mask: this.mask, max: this.max, maxDate: this.maxDate, min: this.min, minDate: this.minDate, options: this.options, pattern: this.pattern, placeholder: this.placeholder, pristine: this.pristine, questionId: this.questionId, required: this.required, touched: this.touched, useComplexMaskLogic: this.useComplexMaskLogic, value: this.value }));
    }
    static get is() { return "bp-sdk-masked-input-container"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-masked-input-container.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-masked-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
            },
            "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
            },
            "errors": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "any[]",
                    "resolved": "any[]",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error messages"
                },
                "defaultValue": "[]"
            },
            "code": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "question code"
                },
                "attribute": "code",
                "reflect": false
            },
            "errorText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error message"
                },
                "attribute": "error_text",
                "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
            },
            "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"
            },
            "min": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "minimum value"
                },
                "attribute": "min_value",
                "reflect": false
            },
            "minDate": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "minimum date value"
                },
                "attribute": "min_date",
                "reflect": false
            },
            "max": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "maximum value"
                },
                "attribute": "max_value",
                "reflect": false
            },
            "maxDate": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "maximum date value"
                },
                "attribute": "max_date",
                "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
            },
            "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
            },
            "pattern": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Regex pattern to match"
                },
                "attribute": "pattern",
                "reflect": false
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Determines if input is required"
                },
                "attribute": "required",
                "reflect": false,
                "defaultValue": "false"
            },
            "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
            },
            "value": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Current value"
                },
                "attribute": "value",
                "reflect": false
            },
            "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": "''"
            },
            "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"
            },
            "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
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Label to use"
                },
                "attribute": "label",
                "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 {
            "canHide": {},
            "useComplexMaskLogic": {},
            "mask": {},
            "options": {},
            "autoComplete": {},
            "useExternal": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "referenceType",
                "methodName": "onReferenceTypeChange"
            }, {
                "propName": "code",
                "methodName": "onCodeChange"
            }, {
                "propName": "isAutocompleteEnabled",
                "methodName": "onIsAutocompleteEnabledChange"
            }, {
                "propName": "fieldType",
                "methodName": "onFieldTypeChange"
            }];
    }
}
