import { h } from "@stencil/core";
import { buildDescribedByString, listToObject } from "@boldpenguin/sdk-utility";
import { QuestionFieldType } from "@boldpenguin/sdk-types";
export class BpSdkTextInput {
    constructor() {
        this.onChange = () => {
            const input = this.el.querySelector('.bp-sdk-text__input');
            this.valueUpdate.emit(input.value);
        };
        this.answerId = undefined;
        this.autoComplete = 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.helpText = undefined;
        this.answeredBySource = undefined;
        this._labelledBy = undefined;
        this._pattern = '';
        this._fieldType = 'text';
        this.useExternal = false;
    }
    updatePattern(fieldType, pattern) {
        if (fieldType === QuestionFieldType.Number && !pattern) {
            pattern = '^\\d*';
        }
        else if (!pattern) {
            pattern = undefined;
        }
        this._pattern = pattern;
    }
    onAriaLabelledByChange(labelId) {
        this.updateLabelledBy(labelId, this.labelledBy);
    }
    onLabelledByChange(labelledBy) {
        this.updateLabelledBy(this.labelId, labelledBy);
    }
    updateLabelledBy(aria, ids) {
        const prefix = ids ? `${ids} ` : '';
        this._labelledBy = `${prefix}${aria !== null && aria !== void 0 ? aria : ''}` || undefined;
    }
    onFieldTypeChange(fieldType) {
        let type;
        if (fieldType === QuestionFieldType.Number) {
            type = 'tel';
        }
        else {
            type = fieldType !== null && fieldType !== void 0 ? fieldType : 'text';
        }
        this._fieldType = type;
        this.updatePattern(fieldType, this.pattern);
    }
    onPatternChange(pattern) {
        this.updatePattern(this.fieldType, pattern);
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
    }
    handleExternalComponents() {
        this.useExternal = !!customElements.get('bp-sdk-external-text-input');
        customElements.whenDefined('bp-sdk-external-text-input').then(() => {
            this.useExternal = true;
        });
    }
    componentWillLoad() {
        this.handleExternalComponents();
        this.onFieldTypeChange(this.fieldType);
        this.onPatternChange(this.pattern);
        this.onAriaLabelledByChange(this.labelId);
        this.onLabelledByChange(this.labelledBy);
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderTextInput() {
        var _a, _b;
        return (h("input", { "aria-describedby": buildDescribedByString(listToObject(this.errors)) || undefined, "aria-label": this.placeholder && !this.label ? this.placeholder : null, "aria-labelledby": this._labelledBy, autocomplete: this.autoComplete, class: "bp-sdk-text__input", disabled: !this.isEnabled, id: `bp-sdk-text-${this.labelId}__input-${this.answerId}`, maxLength: (_a = this.max) !== null && _a !== void 0 ? _a : -1, minLength: (_b = this.min) !== null && _b !== void 0 ? _b : -1, name: this.code, onChange: this.onChange, onInput: this.onChange, onPaste: this.onChange, pattern: this._pattern, placeholder: this.placeholder || '', required: this.required, type: this._fieldType, value: this.text }));
    }
    renderNumberInput() {
        var _a, _b;
        return (h("input", { "aria-describedby": buildDescribedByString(listToObject(this.errors)) || undefined, "aria-label": this.placeholder && !this.label ? this.placeholder : null, "aria-labelledby": this._labelledBy, autocomplete: this.autoComplete, class: "bp-sdk-text__input", disabled: !this.isEnabled, id: `bp-sdk-text-${this.labelId}__input-${this.answerId}`, max: (_a = this.max) !== null && _a !== void 0 ? _a : Number.MAX_VALUE, min: (_b = this.min) !== null && _b !== void 0 ? _b : Number.MIN_VALUE, name: this.code, onChange: this.onChange, onInput: this.onChange, onPaste: this.onChange, pattern: this._pattern, placeholder: this.placeholder || '', required: this.required, type: this._fieldType, value: this.text }));
    }
    renderInputElement() {
        return this.fieldType === QuestionFieldType.Number ? this.renderNumberInput() : this.renderTextInput();
    }
    renderExternal() {
        return (h("bp-sdk-external-text-input", { answerId: this.answerId, autoComplete: this.autoComplete, code: this.code, errorText: this.errorText, errors: 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, readonly: this.isReadOnly, required: this.required, text: this.text, touched: this.touched }));
    }
    renderLabel() {
        return (this.label && (h("label", { id: this.labelId, htmlFor: `bp-sdk-text__${this.labelId}__input}-${this.answerId}`, class: "bp-sdk-label bp-sdk-text__label", innerHTML: this.label })));
    }
    renderErrors() {
        var _a;
        return (this.touched &&
            !this.pristine &&
            ((this.errorText && !this.isCompleted) || !!((_a = this.errors) === null || _a === void 0 ? void 0 : _a.length)) && (h("bp-sdk-errors-container", { questionId: this.questionId, fieldType: this.fieldType, pattern: this.pattern, min: this.min, max: this.max, errors: this.errors, errorText: this.errorText })));
    }
    renderInternal() {
        const element = this.isReadOnly ? this.renderReadOnlyElement(this.text) : this.renderInputElement();
        return (h("span", { class: "bp-sdk-text__container" }, this.renderLabel(), element, this.renderErrors()));
    }
    renderReadOnlyElement(value) {
        return h("span", { class: "read-only-value" }, value);
    }
    static get is() { return "bp-sdk-text-input"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-text-input.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-text-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
            },
            "autoComplete": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Autocomplete attribute value for input"
                },
                "attribute": "auto-complete",
                "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"
            },
            "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 {
            "_labelledBy": {},
            "_pattern": {},
            "_fieldType": {},
            "useExternal": {}
        };
    }
    static get events() {
        return [{
                "method": "valueUpdate",
                "name": "valueUpdate",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Value change event"
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "labelId",
                "methodName": "onAriaLabelledByChange"
            }, {
                "propName": "labelledBy",
                "methodName": "onLabelledByChange"
            }, {
                "propName": "fieldType",
                "methodName": "onFieldTypeChange"
            }, {
                "propName": "pattern",
                "methodName": "onPatternChange"
            }];
    }
}
