import { h } from "@stencil/core";
import { NUMBER_FIELDS, AnswerValidatorType } from "@boldpenguin/sdk-types";
export class BpSdkErrors {
    constructor() {
        this.answerId = undefined;
        this.questionId = undefined;
        this.min = undefined;
        this.minDate = undefined;
        this.max = undefined;
        this.maxDate = undefined;
        this.fieldType = undefined;
        this.pattern = undefined;
        this.errors = [];
        this.errorText = undefined;
        this.errorMessages = undefined;
        this.useExternal = false;
    }
    transformError(error) {
        switch (error) {
            case AnswerValidatorType.MaximumValue:
            case AnswerValidatorType.MinimumValue:
                if (NUMBER_FIELDS.includes(this.fieldType)) {
                    return this.errorMessages.value(this.min, this.max);
                }
                else {
                    return this.errorMessages.length(this.min, this.max);
                }
            case AnswerValidatorType.MinimumDate:
            case AnswerValidatorType.MaximumDate:
                return this.errorMessages.date(this.minDate, this.maxDate);
            case AnswerValidatorType.Required:
                return this.errorMessages.required();
            case AnswerValidatorType.Pattern:
                return this.errorMessages.pattern();
        }
        return error;
    }
    resetState() { }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-errors');
        customElements.whenDefined('bp-sdk-external-errors').then(() => {
            this.useExternal = true;
        });
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-errors", { answerId: this.answerId, errorMessages: this.errorMessages, errors: this.errors, errorText: this.errorText, fieldType: this.fieldType, maxDate: this.maxDate, max: this.max, minDate: this.minDate, min: this.min, pattern: this.pattern, questionId: this.questionId }));
    }
    renderInternal() {
        return (h("div", { class: "bp-sdk-errors__container" }, this.errorText && (h("span", { "aria-live": "polite", id: `bp-sdk-error-${this.answerId}-text`, class: `bp-sdk-errors-text bp-sdk-errors-text-${this.answerId}` }, this.errorText)), !this.errorText && (h("ul", { class: "bp-sdk-errors__list" }, this.errors.map((error) => (h("li", { "aria-live": "polite", id: `bp-sdk-errors__${this.answerId}-${error}`, class: `bp-sdk-errors__list-item bp-sdk-errors__list-item-${this.answerId}` }, this.transformError(error))))))));
    }
    static get is() { return "bp-sdk-errors"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-errors.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-errors.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
            },
            "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"
                },
                "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"
                },
                "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
            },
            "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
            },
            "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
            },
            "errorMessages": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "IErrorMessages",
                    "resolved": "IErrorMessages",
                    "references": {
                        "IErrorMessages": {
                            "location": "import",
                            "path": "@boldpenguin/sdk-types",
                            "id": "../sdk-types/dist/types.d.ts::IErrorMessages"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Error messages function map"
                }
            }
        };
    }
    static get states() {
        return {
            "useExternal": {}
        };
    }
}
