import { EasePicker } from "../../utils/easepicker";
import { Host, h } from "@stencil/core";
import { cls, getId } from "../../utils/helpers";
import { DateTime } from "../../utils/datetime";
export class XDatepicker {
    constructor() {
        /**
         * Element ID for internal reference
         */
        this.inputId = getId('datepicker');
        /**
         * Element input picker instance
         */
        this.picker = null;
        this.label = undefined;
        this.name = undefined;
        this.info = undefined;
        this.format = 'DD-MM-YYYY';
        this.loading = undefined;
        this.cleanable = undefined;
        this.ranged = undefined;
        this.rangedDelimiter = " > ";
        this.value = '';
        this.minDate = undefined;
        this.maxDate = undefined;
        this.minRangedDays = undefined;
        this.maxRangedDays = undefined;
        this.disableDates = undefined;
        this.isFocused = undefined;
        this.isLoading = undefined;
        this.isDisabled = undefined;
    }
    /**
     * On value attribute change
     *
     * @param value
     */
    onValueChange(value) {
        if (this.ranged) {
            const delimiter = value.includes(this.rangedDelimiter) ? this.rangedDelimiter : ',';
            const [start, end] = value.split(delimiter);
            this.picker.setDateRange(start, end);
        }
        else {
            this.picker.setDate(value);
        }
        this.internals.setFormValue(value);
        this.el.dispatchEvent(new InputEvent('input'));
    }
    /**
     * This is called when the 'disabled' attribute of the element or or an ancestor <fieldset> is updated
     *
     * @param disabled
     */
    formDisabledCallback(disabled) {
        this.isDisabled = disabled;
    }
    /**
     * This is called when the parent/owner form is reset
     */
    formResetCallback() {
        this.internals.setFormValue('');
    }
    /**
     * This is called on component did load
     */
    componentDidLoad() {
        this.picker = new EasePicker(this.generateOptions());
        this.picker.on('select', ({ detail }) => this.onSelect(detail));
    }
    /**
     * Clears the input value
     */
    clearInput() {
        this.value = '';
    }
    /**
     * Generates options for the picker
     */
    generateOptions() {
        const disableDates = this.generateLockedDays();
        const ranged = this.minRangedDays != null ||
            this.maxRangedDays != null;
        const locked = disableDates.length > 0 ||
            (this.minDate && this.minDate != "") ||
            (this.maxDate && this.maxDate != "");
        const options = {
            element: this.el.querySelector("input"),
            zIndex: 99999,
            date: this.value,
            format: this.format,
            shadowDom: false,
            delimiter: this.rangedDelimiter,
            ranged: this.ranged || ranged,
            locked: locked || ranged,
            minDate: null,
            maxDate: null,
            minDays: null,
            maxDays: null,
            filter: null,
        };
        if (this.ranged) {
            options.minDays = this.minRangedDays;
            options.maxDays = this.maxRangedDays;
        }
        if (locked) {
            options.minDate = this.minDate;
            options.maxDate = this.maxDate;
            options.filter = (date, picked) => {
                if (picked.length === 1 && date instanceof DateTime) {
                    const incl = date.isBefore(picked[0]) ? '[)' : '(]';
                    return !picked[0].isSame(date, 'day') && date.inArray(disableDates, incl);
                }
                if (date instanceof DateTime) {
                    return date.inArray(disableDates, '[)');
                }
            };
        }
        return options;
    }
    /**
     * Generates locked days
     */
    generateLockedDays() {
        let lockedDays = [];
        if (typeof this.disableDates === 'string') {
            lockedDays = this.disableDates.split(',')
                .map((date) => date.includes(this.rangedDelimiter) ? date.split(this.rangedDelimiter) : date);
        }
        else {
            lockedDays = this.disableDates || [];
        }
        return lockedDays.map((date) => {
            if (Array.isArray(date)) {
                return [new DateTime(date[0], this.format), new DateTime(date[1], this.format)];
            }
            else {
                return new DateTime(date, this.format);
            }
        });
    }
    /**
     * This called on date select
     */
    onSelect(selected) {
        if (this.ranged) {
            this.value = [new DateTime(selected.start).format(this.format), new DateTime(selected.end).format(this.format)].join(',');
        }
        else {
            this.value = new DateTime(selected.date).format(this.format);
        }
        this.internals.setFormValue(this.value);
    }
    /**
     * This called on input field is unfocused
     */
    onBlurInput() {
        this.blurTimeout = setTimeout(() => {
            this.isFocused = false;
        }, 50);
    }
    /**
     * This called on input field is focused
     */
    onFocusInput() {
        clearTimeout(this.blurTimeout);
        this.isFocused = true;
    }
    /**
     * Render component
     */
    render() {
        return h(Host, { key: '6debff82cda8d9da7e963ef98d3de785027b643e', "data-for": this.name, tabindex: "0", onFocus: () => this.onFocusInput(), onBlur: () => this.onBlurInput() }, h("div", { key: 'ec350d36c99f6aa75852da3c1ca1214258215c91', class: cls({
                "input-container": true,
                "focus": this.isFocused,
                "loading": this.loading || this.isLoading,
                "disabled": this.isDisabled,
            }) }, h("div", { key: 'c490673af26806daa13eb2412d64ad0a8825a130', class: "indicator" }, h("slot", { key: '38e7e6e7560903c439a5097b0f10d5e1364f32cc', name: "indicator" })), h("div", { key: 'ec78aaeac4b01467d58ef12d1a147229cc98f881', class: "separator" }), h("div", { key: 'cc0f8a04bbb4d1ba1c74215e8d66988616f393b8', class: "wrap" }, this.label && h("label", { key: '3d8e7fbe72f89b90e8e6846a9f85be53e5aab70b', htmlFor: this.inputId }, this.label), h("input", { key: '5c471813e48aa51fe8292402ec9d01b033deffdb', id: this.inputId, type: "tel", placeholder: this.ranged ? this.format + this.rangedDelimiter + this.format : this.format, disabled: this.isDisabled || this.isLoading, onFocus: () => this.onFocusInput(), onBlur: () => this.onBlurInput() })), this.loading && h("div", { key: 'd53f00ec74f927ff75ae5a45f01e16c320739234', class: "loading-indicator" }), h("div", { key: 'bc9e265bb48d728d1bdb3b8b213b9700ed67f836', class: "buttons" }, this.cleanable && h("x-button", { key: '8a07000d4c1a3f609b4e432c5168e03c88b65425', icon: "backclear", onClick: () => this.clearInput() }), h("slot", { key: '6008e7e1985646e08e756e39308593a05eabaf24', name: "buttons" })), this.info && h("div", { key: '464f452a17ed0a2a6bd7add7892337e364b11a5b', class: "info" }, h("x-tooltip", { key: '64bc11e655038546415437bd1ec2b89cce0efe8c', content: this.info }, h("x-icon", { key: '5964c704557b8c80a03f5495f97edfc5fce775b1', name: "info" }))), h("slot", { key: 'df2a52c82441ada62aefd66ae43e373a25ee6be7' })));
    }
    static get is() { return "x-datepicker"; }
    static get formAssociated() { return true; }
    static get originalStyleUrls() {
        return {
            "$": ["./style.scss"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["style.css"]
        };
    }
    static get properties() {
        return {
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input label text"
                },
                "attribute": "label",
                "reflect": false
            },
            "name": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input name for forms keys"
                },
                "attribute": "name",
                "reflect": false
            },
            "info": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input information block"
                },
                "attribute": "info",
                "reflect": false
            },
            "format": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element date format"
                },
                "attribute": "format",
                "reflect": false,
                "defaultValue": "'DD-MM-YYYY'"
            },
            "loading": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element show loading and block for edit"
                },
                "attribute": "loading",
                "reflect": false
            },
            "cleanable": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input clear button"
                },
                "attribute": "cleanable",
                "reflect": false
            },
            "ranged": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input date ranged"
                },
                "attribute": "ranged",
                "reflect": false
            },
            "rangedDelimiter": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input ranged deliter"
                },
                "attribute": "ranged-delimiter",
                "reflect": false,
                "defaultValue": "\" > \""
            },
            "value": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string | null",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "Element input value"
                },
                "attribute": "value",
                "reflect": false,
                "defaultValue": "''"
            },
            "minDate": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input min ranged date"
                },
                "attribute": "min-date",
                "reflect": false
            },
            "maxDate": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input max ranged date"
                },
                "attribute": "max-date",
                "reflect": false
            },
            "minRangedDays": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input min ranged days"
                },
                "attribute": "min-ranged-days",
                "reflect": false
            },
            "maxRangedDays": {
                "type": "number",
                "mutable": false,
                "complexType": {
                    "original": "number",
                    "resolved": "number",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input max ranged days"
                },
                "attribute": "max-ranged-days",
                "reflect": false
            },
            "disableDates": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "[] | string | null",
                    "resolved": "[] | string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Element input disable dates"
                },
                "attribute": "disable-dates",
                "reflect": false
            }
        };
    }
    static get states() {
        return {
            "isFocused": {},
            "isLoading": {},
            "isDisabled": {}
        };
    }
    static get elementRef() { return "el"; }
    static get watchers() {
        return [{
                "propName": "value",
                "methodName": "onValueChange"
            }];
    }
    static get attachInternalsMemberName() { return "internals"; }
}
