import { h } from "@stencil/core";
import Tunnel from '../../../locale/i18n.provider';
export class DateFilterPanel {
    onChange({ detail, target }) {
        const input = target;
        this[input.name] = detail.innerValue;
    }
    async reset() {
        this.element.querySelector('form').reset();
        this.max = undefined;
        this.min = undefined;
    }
    onSubmit(e) {
        e.preventDefault();
        const valid = e.target.checkValidity();
        if (!valid)
            return;
        const filter = { field: this.field };
        let query;
        let type;
        if (this.min && this.max) {
            type = 'contains';
            query = [new Date(this.min), new Date(this.max)];
        }
        else if (this.min) {
            type = 'starts-with';
            query = new Date(this.min);
        }
        else {
            type = 'ends-with';
            query = new Date(this.max);
        }
        if (query) {
            filter.type = type;
            filter.query = query;
            this.anjFilterChange.emit(filter);
        }
    }
    render() {
        return (h("form", { onSubmit: (e) => this.onSubmit(e), novalidate: true },
            this.types.includes('starts-with') || this.types.includes('contains') ?
                h("anj-form-group", null,
                    h("anj-label", null, this.translator('Start Date')),
                    h("anj-input", { name: "min", max: this.max },
                        h("anj-date-picker-toggle", null),
                        h("anj-date-picker", null))) : undefined,
            this.types.includes('ends-with') || this.types.includes('contains') ?
                h("anj-form-group", null,
                    h("anj-label", null, this.translator('End Date')),
                    h("anj-input", { name: "max", min: this.min },
                        h("anj-date-picker-toggle", null),
                        h("anj-date-picker", null))) : undefined,
            h("anj-button-group", { align: "right" },
                h("anj-button", { context: "text", type: "submit" }, this.translator('Apply')))));
    }
    static get is() { return "anj-date-filter-panel"; }
    static get encapsulation() { return "scoped"; }
    static get originalStyleUrls() { return {
        "$": ["date-filter-panel.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["date-filter-panel.css"]
    }; }
    static get properties() { return {
        "field": {
            "type": "string",
            "mutable": false,
            "complexType": {
                "original": "string",
                "resolved": "string",
                "references": {}
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "The key we're working with"
            },
            "attribute": "field",
            "reflect": false
        },
        "filter": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "ColumnFilter",
                "resolved": "ColumnFilter",
                "references": {
                    "ColumnFilter": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Current active filter if it exists"
            }
        },
        "translator": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "IntlFormatFn",
                "resolved": "(value: any, variables?: any) => string",
                "references": {
                    "IntlFormatFn": {
                        "location": "import",
                        "path": "../../../locale/i18n.provider"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [{
                        "text": undefined,
                        "name": "internal"
                    }],
                "text": ""
            }
        },
        "types": {
            "type": "unknown",
            "mutable": false,
            "complexType": {
                "original": "FilterType[]",
                "resolved": "FilterType[]",
                "references": {
                    "FilterType": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            },
            "required": false,
            "optional": false,
            "docs": {
                "tags": [],
                "text": "Types of filters supported"
            }
        }
    }; }
    static get states() { return {
        "max": {},
        "min": {}
    }; }
    static get events() { return [{
            "method": "anjFilterChange",
            "name": "anjFilterChange",
            "bubbles": true,
            "cancelable": true,
            "composed": true,
            "docs": {
                "tags": [],
                "text": "Emitted when the user wants to apply a new filter"
            },
            "complexType": {
                "original": "ColumnFilter",
                "resolved": "ColumnFilter",
                "references": {
                    "ColumnFilter": {
                        "location": "import",
                        "path": "../../table.types"
                    }
                }
            }
        }]; }
    static get methods() { return {
        "reset": {
            "complexType": {
                "signature": "() => Promise<void>",
                "parameters": [],
                "references": {
                    "Promise": {
                        "location": "global"
                    }
                },
                "return": "Promise<void>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        }
    }; }
    static get elementRef() { return "element"; }
    static get listeners() { return [{
            "name": "anjChange",
            "method": "onChange",
            "target": undefined,
            "capture": false,
            "passive": false
        }]; }
}
Tunnel.injectProps(DateFilterPanel, ['translator']);
