import { r as registerInstance, j as createEvent, h, g as getElement } from './index-07845439.js';
import { d as dist$1 } from './index-ea5ab071.js';
import { d as dist } from './index-3cc80977.js';
import './_commonjsHelpers-9bc404fc.js';
import './index-614b6d2f.js';

const sv = {
  Filter: "Filtrera",
  "Remove filter": "Ta bort filter",
  "Apply filter": "Tillämpa filter",
};

function create(language) {
  return dist.create({
    sv,
  }, language);
}

const styleCss = ".sc-payfunc-filter-h{display:block;width:100%;max-width:90em;margin:auto}[hidden].sc-payfunc-filter-h{display:none}.sc-payfunc-filter-h>form.sc-payfunc-filter{border:1px solid rgb(var(--smoothly-medium-color));margin:0.2em 0 0.5em 0;clear:both}.sc-payfunc-filter-h>form.sc-payfunc-filter>payfunc-autocomplete-input.sc-payfunc-filter{width:calc(100% - 80px);display:inline-block}.sc-payfunc-filter-h>form.sc-payfunc-filter>smoothly-input.sc-payfunc-filter{width:calc(100% - 80px);display:inline-block}.sc-payfunc-filter-h>form.sc-payfunc-filter>smoothly-submit.sc-payfunc-filter{display:inline-block}";

const PayfuncFilter = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.filterChanged = createEvent(this, "filterChanged", 7);
    this.textEntered = false;
    this.criteriaString = "";
  }
  async componentWillLoad() {
    this.t = 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 || dist$1.and(dist$1.parse(this.criteriaString)).generalize();
    this.filterChanged.emit(this.value ? dist$1.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("payfunc-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" }))));
  }
  get element() { return getElement(this); }
  static get watchers() { return {
    "criteriaString": ["criteriaStringChanged"]
  }; }
};
function hasValue(element) {
  return element.value == undefined || typeof element.value === "string";
}
PayfuncFilter.style = styleCss;

export { PayfuncFilter as payfunc_filter };
