import { h } from "@stencil/core";
import { getDateStringFromDate } from "@boldpenguin/sdk-utility";
export class BpSdkDateInput {
    constructor() {
        this.answerId = undefined;
        this.questionId = undefined;
        this.fieldType = undefined;
        this.text = undefined;
        this.minDate = undefined;
        this.maxDate = undefined;
        this.code = undefined;
        this.value = undefined;
        this.placeholder = 'Pick a date';
        this.labelId = undefined;
        this.pattern = undefined;
        this.touched = false;
        this.pristine = true;
        this.isCompleted = false;
        this.isReadOnly = false;
        this.errors = [];
        this.errorText = undefined;
        this.isSensitive = false;
        this.label = undefined;
        this.labelledBy = undefined;
        this.required = undefined;
        this.helpText = undefined;
        this.answeredBySource = undefined;
        this._labelledBy = undefined;
        this._value = undefined;
        this.useExternal = false;
    }
    resetState() {
        this._value = undefined;
    }
    connectedCallback() {
        this.resetState();
    }
    componentWillLoad() {
        this.useExternal = !!customElements.get('bp-sdk-external-date-input');
        customElements.whenDefined('bp-sdk-external-date-input').then(() => {
            this.useExternal = true;
        });
        this.onValueChange(this.value);
        this.onAriaLabelledByChange(this.labelId);
        this.onLabelledByChange(this.labelledBy);
    }
    componentDidLoad() {
        const el = this.getDatePicker();
        if (el === null || el === void 0 ? void 0 : el._setTheme) {
            el._setTheme('calendar');
        }
    }
    getDatePicker() {
        return this.el.querySelector('.bp-sdk-date__picker');
    }
    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;
    }
    onValueChange(newValue) {
        if (newValue != null) {
            this._value = getDateStringFromDate(new Date(newValue));
        }
    }
    onChange(event) {
        if (this.useExternal) {
            return;
        }
        const value = new Date(event.target.value);
        try {
            const date = getDateStringFromDate(value);
            this._value = date;
            if (this.value != this._value) {
                this.valueUpdate.emit(value.toISOString());
            }
        }
        catch (ex) {
            // don't use bad iso8601 dates
            // TODO: error logging
        }
    }
    render() {
        return [this.useExternal && this.renderExternal(), !this.useExternal && this.renderInternal()];
    }
    renderExternal() {
        return (h("bp-sdk-external-date-input", { answerId: this.answerId, code: this.code, errors: this.errors, errorText: this.errorText, fieldType: this.fieldType, helpText: this.helpText, answeredBySource: this.answeredBySource, isCompleted: this.isCompleted, isReadOnly: this.isReadOnly, isSensitive: this.isSensitive, labelId: this.labelId, label: this.label, labelledBy: this.labelledBy, maxDate: this.maxDate, minDate: this.minDate, pattern: this.pattern, placeholder: this.placeholder, pristine: this.pristine, questionId: this.questionId, required: this.required, text: this.text, touched: this.touched, value: this.value }));
    }
    renderInternal() {
        var _a;
        const input = this.isReadOnly ? this.renderReadOnlyElement(this.value) : this.renderInput();
        return (h("div", { class: "bp-sdk-date__container" }, h("label", { id: this.labelId, htmlFor: `bp-sdk-date__${this.labelId}__input`, class: "bp-sdk-label bp-sdk-date__label", innerHTML: this.label }), input, this.touched && !this.pristine && ((this.errorText && !this.isCompleted) || !!((_a = this.errors) === null || _a === void 0 ? void 0 : _a.length)) && (h("bp-sdk-errors-container", { answerId: this.answerId, questionId: this.questionId, fieldType: this.fieldType, minDate: this.minDate, maxDate: this.maxDate, pattern: this.pattern, errors: this.errors, errorText: this.errorText }))));
    }
    renderInput() {
        return (h("input", { id: `bp-sdk-date__${this.labelId}__input`, class: "bp-sdk-date__input", "aria-labelledby": this._labelledBy, type: "date", placeholder: this.placeholder, size: 10, min: this.minDate, max: this.maxDate, value: this._value }));
    }
    renderReadOnlyElement(value) {
        return (h("span", { class: "read-only-value" }, value));
    }
    static get is() { return "bp-sdk-date-input"; }
    static get originalStyleUrls() {
        return {
            "$": ["bp-sdk-date-input.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["bp-sdk-date-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
            },
            "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
            },
            "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
            },
            "text": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Question text"
                },
                "attribute": "text",
                "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
            },
            "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
            },
            "code": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "question code"
                },
                "attribute": "code",
                "reflect": false
            },
            "value": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "question 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": "'Pick a date'"
            },
            "labelId": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Label id to use"
                },
                "attribute": "label-id",
                "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
            },
            "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"
            },
            "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
            },
            "isSensitive": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is sensitive"
                },
                "attribute": "is-sensitive",
                "reflect": false,
                "defaultValue": "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
            },
            "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
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Is required"
                },
                "attribute": "required",
                "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 {
            "_labelledBy": {},
            "_value": {},
            "useExternal": {}
        };
    }
    static get events() {
        return [{
                "method": "valueUpdate",
                "name": "valueUpdate",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "Value has updated"
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }];
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "labelId",
                "methodName": "onAriaLabelledByChange"
            }, {
                "propName": "labelledBy",
                "methodName": "onLabelledByChange"
            }, {
                "propName": "value",
                "methodName": "onValueChange"
            }];
    }
    static get listeners() {
        return [{
                "name": "change",
                "method": "onChange",
                "target": undefined,
                "capture": false,
                "passive": false
            }];
    }
}
