import { h } from "@stencil/core";
import { hasValue } from '../../../../utils/utils';
import Tunnel from '../../../locale/i18n.provider';
export class NumberFilterPanel {
    onChange({ detail, target }) {
        const el = target;
        this[el.name] = detail.checked ? detail.checked : detail.value;
    }
    async reset() {
        this.element.querySelector('form').reset();
        this.max = undefined;
        this.min = undefined;
    }
    onSubmit(e) {
        e.preventDefault();
        const filter = { field: this.field };
        const valid = e.target.checkValidity();
        if (!valid)
            return;
        let query;
        let type;
        if (hasValue(this.min) && hasValue(this.max)) {
            type = 'contains';
            query = [parseInt(this.min, 10), parseInt(this.max, 10)];
        }
        else if (hasValue(this.min)) {
            type = 'starts-with';
            query = parseInt(this.min, 10);
        }
        else {
            type = 'ends-with';
            query = parseInt(this.max, 10);
        }
        if (hasValue(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('Min')),
                    h("anj-input", { name: "min", type: "number", max: this.max, value: this.min })) : undefined,
            this.types.includes('ends-with') || this.types.includes('contains') ?
                h("anj-form-group", null,
                    h("anj-label", null, this.translator('Max')),
                    h("anj-input", { name: "max", type: "number", min: this.min, value: this.max })) : undefined,
            h("anj-button-group", { align: "right" },
                h("anj-button", { context: "text", type: "submit" }, this.translator('Apply')))));
    }
    static get is() { return "anj-number-filter-panel"; }
    static get encapsulation() { return "scoped"; }
    static get originalStyleUrls() { return {
        "$": ["number-filter-panel.scss"]
    }; }
    static get styleUrls() { return {
        "$": ["number-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(NumberFilterPanel, ['translator']);
