import { Host, h } from "@stencil/core";
import { Errors } from "../../services/errors";
import { bracketToDot } from "../../utils/helpers";
import { Lang } from "../../services/lang";
export class XInputError {
    constructor() {
        this.errorsFor = undefined;
        this.errors = [];
    }
    /**
     * Get the event object and parse to validation messages
     *
     * @param ev
     * @returns
     */
    parseValidationEvent(ev) {
        if ((ev === null || ev === void 0 ? void 0 : ev.detail) === null) {
            return [];
        }
        const input = ev.detail;
        const validity = input.validity;
        const errors = [];
        if (validity.valid) {
            return errors;
        }
        if (validity.badInput) {
            errors.push(Lang.get('fields.validation.bad-input'));
        }
        if (validity.customError) {
            errors.push(input.validationMessage);
        }
        if (validity.patternMismatch) {
            errors.push(Lang.get('fields.validation.pattern-mismatch'));
        }
        if (validity.rangeOverflow) {
            errors.push(Lang.get('fields.validation.range-overflow'));
        }
        if (validity.rangeUnderflow) {
            errors.push(Lang.get('fields.validation.range-underflow', { min: input.getAttribute('min') }));
        }
        if (validity.stepMismatch) {
            errors.push(Lang.get('fields.validation.step-mismatch', { max: input.getAttribute('max') }));
        }
        if (validity.tooLong) {
            errors.push(Lang.get('fields.validation.too-long', { max: input.getAttribute('maxlength') }));
        }
        if (validity.tooShort) {
            errors.push(Lang.get('fields.validation.too-short', { min: input.getAttribute('minlength') }));
        }
        if (validity.typeMismatch) {
            errors.push(Lang.get('fields.validation.type-mismatch'));
        }
        if (validity.valueMissing) {
            errors.push(Lang.get('fields.validation.value-missing'));
        }
        return errors;
    }
    /**
     * This is called on component did load
     */
    componentDidLoad() {
        if (this.errorsFor.length == 0) {
            return;
        }
        const form = this.internals.form;
        const inputs = form.querySelectorAll(`[name="${this.errorsFor}"], [data-for="${this.errorsFor}"]`);
        inputs.length > 0 && inputs.forEach((input) => {
            input.addEventListener('validated', (ev) => {
                this.errors = this.parseValidationEvent(ev);
            });
            document.addEventListener('errorsAdded', () => {
                this.errors = Errors.get(bracketToDot(this.errorsFor));
            });
        });
    }
    /**
     * Render component
     */
    render() {
        return (h(Host, { key: '480857a7c4ea4e1772e6b207ae1f523d4f53a6db' }, h("slot", { key: 'c3f6335da9b10315ab82cfb56d2a971fe3063066' }), this.errors.map((error, i) => (h("p", { key: `${bracketToDot(this.errorsFor)}-${i}` }, error)))));
    }
    static get is() { return "x-input-error"; }
    static get formAssociated() { return true; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "errorsFor": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Input errors for mapping to element"
                },
                "attribute": "errors-for",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "errors": {}
        };
    }
    static get elementRef() { return "el"; }
    static get attachInternalsMemberName() { return "internals"; }
}
