import { Component, Element, Event, h, Prop, State, Watch } from "@stencil/core";
import * as selectively from "selectively";
import * as translation from "./translation";
export class PayfuncFilter {
  constructor() {
    this.textEntered = false;
    this.criteriaString = "";
  }
  async componentWillLoad() {
    this.t = translation.create(this.element);
  }
  checkTextEntered() {
    const input = document.getElementById("filter");
    if (hasValue(input))
      this.textEntered = input.value != "";
  }
  onFilter(e) {
    this.criteriaString = e.detail.filter;
    this.applyFilter();
    this.checkTextEntered();
  }
  criteriaStringChanged() {
    const input = document.getElementById("filter");
    if (hasValue(input))
      input.value = this.criteriaString;
    this.textEntered = true;
    this.applyFilter();
  }
  applyFilter(criteria) {
    this.filter = criteria || selectively.and(selectively.parse(this.criteriaString)).generalize();
    this.filterChanged.emit(selectively.filter(this.filter, this.value));
  }
  reset() {
    this.criteriaString = "";
    this.applyFilter();
    const input = document.getElementById("filter");
    if (hasValue(input))
      input.value = "";
    this.textEntered = false;
  }
  render() {
    return (h("form", null,
      h("smoothly-input", { type: "text", id: "filter", name: "filter", onKeyDown: this.checkTextEntered.bind(this) }, this.t("Filter")),
      h("smoothly-submit", { fill: "clear", prevent: true, onSubmit: (e) => this.onFilter(e), style: { visibility: this.textEntered ? "visible" : "hidden" } },
        h("smoothly-icon", { toolTip: this.t("Apply filter"), name: "search", size: "small" })),
      h("smoothly-submit", { fill: "clear", prevent: true, onSubmit: () => this.reset() },
        h("smoothly-icon", { toolTip: this.t("Remove filter"), size: "small", name: "backspace" }))));
  }
  static get is() { return "payfunc-filter"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "criteriaString": {
      "type": "string",
      "mutable": true,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "criteria-string",
      "reflect": false,
      "defaultValue": "\"\""
    },
    "filter": {
      "type": "any",
      "mutable": true,
      "complexType": {
        "original": "Criteria",
        "resolved": "Helper[] | Rule | number | string | undefined | { [property: string]: string | number | Rule | any | Helper[]; }",
        "references": {
          "Criteria": {
            "location": "import",
            "path": "selectively"
          }
        }
      },
      "required": false,
      "optional": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "filter",
      "reflect": false
    },
    "value": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "any[]",
        "resolved": "any[]",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "t": {},
    "textEntered": {}
  }; }
  static get events() { return [{
      "method": "filterChanged",
      "name": "filterChanged",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
  static get elementRef() { return "element"; }
  static get watchers() { return [{
      "propName": "criteriaString",
      "methodName": "criteriaStringChanged"
    }]; }
}
function hasValue(element) {
  return element.value == undefined || typeof element.value === "string";
}
