import { Host, h, } from "@stencil/core";
import { FormControlErrorText } from "../../ui-components";
import { input, inputDisabled, inputFocused, inputInvalid, label } from "../../styles/parts";
export class FormControlDate {
    constructor() {
        this.isFocused = false;
        this.maxDate = this.currentDate;
        this.handleFormControlInput = (event) => {
            const dateValue = event.target.value;
            this.inputHandler(this.name, dateValue);
            this.formControlInput.emit({ name: this.name, value: dateValue });
        };
    }
    handleDefaultValueChange(newValue) {
        this.updateInput(newValue);
    }
    get currentDate() {
        if (!this.showTime) {
            return new Date().toISOString().split('T')[0];
        }
        else {
            return new Date().toISOString().slice(0, 16);
        }
    }
    componentDidLoad() {
        this.updateInput(this.defaultValue);
    }
    updateInput(value) {
        this.dateInput.value = value;
    }
    get part() {
        let part = input;
        if (this.errorText) {
            part = inputInvalid;
        }
        if (this.disabled) {
            part = inputDisabled;
        }
        if (this.isFocused) {
            part = inputFocused;
        }
        return part;
    }
    render() {
        return (h(Host, { key: 'a7bf2b14b5b1d29ea4ae370c440d9fbeac550ca4' }, h("div", { key: 'bdb2162f367f5fdec34fae2d8732d084958246da', class: "form-group d-flex flex-column" }, h("div", { key: 'eb9483d77d8f1d1effd3278b83e4540b6017ee4c', class: "d-flex gap-2" }, h("label", { key: '2947a82052e88c4bb44d9d32e409d7a39baf157f', part: label, class: "form-label", htmlFor: this.name }, this.label), h("form-control-tooltip", { key: 'edfbc6ddb02cb5d70abd0867f8da8bab860ba598', helpText: this.helpText })), h("input", { key: '2a2d8d464e330ae2ba56f76dadede59c180cdcb6', type: this.showTime ? 'datetime-local' : 'date', ref: el => (this.dateInput = el), id: this.name, name: this.name, onFocus: () => this.isFocused = true, onBlur: () => {
                this.isFocused = false;
                this.formControlBlur.emit();
            }, onInput: this.handleFormControlInput, part: this.part, class: this.errorText ? 'form-control is-invalid' : 'form-control', disabled: this.disabled, max: this.maxDate }), h(FormControlErrorText, { key: '8837370dd05097b6d43489855ac87eff8270ae25', errorText: this.errorText, name: this.name }))));
    }
    static get is() { return "form-control-date"; }
    static get properties() {
        return {
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "name"
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "label"
            },
            "inputHandler": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "(name: string, value: string) => void",
                    "resolved": "(name: string, value: string) => void",
                    "references": {}
                },
                "required": true,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false
            },
            "defaultValue": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "default-value"
            },
            "helpText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "help-text"
            },
            "errorText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "error-text"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "disabled"
            },
            "showTime": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "show-time"
            },
            "maxDate": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "max-date",
                "defaultValue": "this.currentDate"
            }
        };
    }
    static get states() {
        return {
            "isFocused": {}
        };
    }
    static get events() {
        return [{
                "method": "formControlInput",
                "name": "formControlInput",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }, {
                "method": "formControlBlur",
                "name": "formControlBlur",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "complexType": {
                    "original": "any",
                    "resolved": "any",
                    "references": {}
                }
            }];
    }
    static get watchers() {
        return [{
                "propName": "defaultValue",
                "methodName": "handleDefaultValueChange"
            }];
    }
}
