import { Component, Element, Event, h, Prop, State, Watch } from "@stencil/core";
import * as selectively from "selectively";
import * as translation from "./translation";
export class IntergiroFilter {
  constructor() {
    this.textEntered = false;
    this.criteriaString = "";
  }
  async componentWillLoad() {
    this.t = translation.create(this.element);
  }
  checkTextEntered() {
    const input = this.inputElement;
    if (hasValue(input))
      this.textEntered = input.value != "";
  }
  onFilter(e) {
    this.criteriaString = e.detail.filter;
    this.applyFilter();
    this.checkTextEntered();
  }
  criteriaStringChanged() {
    const input = this.inputElement;
    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(this.value ? selectively.filter(this.filter, this.value) : this.filter);
  }
  reset() {
    this.criteriaString = "";
    this.applyFilter();
    const input = this.inputElement;
    if (hasValue(input))
      input.value = "";
    this.textEntered = false;
  }
  render() {
    return (h("form", null,
      h("ig-autocomplete-input", { template: this.template, ref: (el) => (this.inputElement = el), 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 "ig-filter"; }
  static get encapsulation() { return "scoped"; }
  static get originalStyleUrls() { return {
    "$": ["style.css"]
  }; }
  static get styleUrls() { return {
    "$": ["style.css"]
  }; }
  static get properties() { return {
    "template": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "SelectivelyType",
        "resolved": "Base",
        "references": {
          "SelectivelyType": {
            "location": "import",
            "path": "../model"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "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]: Helper | 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[] | undefined",
        "references": {}
      },
      "required": false,
      "optional": true,
      "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";
}
