import { containsTextField, generateFilters, getDefaultFilterValues } from '@dvn/alaca/components/dvn-filter-simple/util';
import { SingleChoiceField } from '@dvn/core/doldurma/src/fields/single-choice';
import { Component, Event, h, Prop, State } from '@stencil/core';
export class DvnFilterSimple {
  constructor() {
    this.filterValues = { text: '' };
    this.leaned = false;
    this.placeholder = 'Quick search';
  }
  componentWillLoad() {
    const { config, fields } = this;
    this.filterFields = config.fields.map(fieldConfig => fieldConfig.field || fields.find(f => f.id === fieldConfig.fieldId));
    this.filterValues = getDefaultFilterValues(config, fields);
  }
  render() {
    const { filterValues } = this;
    return (h("div", { class: "d-flex" },
      containsTextField(this.filterFields) &&
        h("dvn-input", { leaned: true, onChange: (e) => this.onTextChange(e.detail), placeholder: this.placeholder, value: filterValues.text }),
      this.filterFields
        .filter(field => field.type === SingleChoiceField.type)
        .filter(field => !field.dataAdapter)
        .map(field => {
        var _a, _b;
        return (h("dvn-select", { enableClear: true, leaned: true, onChange: (e) => this.onSingleChoiceChange(field.id, e.detail), width: ((_b = (_a = this.config.ui) === null || _a === void 0 ? void 0 : _a[field.id]) === null || _b === void 0 ? void 0 : _b.width) || '100px', style: { marginLeft: 'calc(1.5 * var(--spacing-unit))' }, value: field.value },
          h("dvn-select-option", null, field.title),
          field.options.map(option => (h("dvn-select-option", { value: option.value }, option.label)))));
      }),
      this.filterFields
        .filter(field => field.type === SingleChoiceField.type)
        .filter(field => !!field.dataAdapter)
        .map((field) => {
        var _a, _b;
        return (h("dvn-select", { enableClear: true, leaned: true, dataAdapter: field.dataAdapter, dataProvider: field.dataProvider, onChange: (e) => this.onSingleChoiceChange(field.id, e.detail), width: ((_b = (_a = this.config.ui) === null || _a === void 0 ? void 0 : _a[field.id]) === null || _b === void 0 ? void 0 : _b.width) || '100px', style: { marginLeft: 'calc(1.5 * var(--spacing-unit))' } },
          h("dvn-select-option", null, field.title)));
      })));
  }
  onSingleChoiceChange(fieldId, value) {
    if (value) {
      this.filterValues = Object.assign(Object.assign({}, this.filterValues), { [fieldId]: value });
    }
    else {
      // value is cleared
      const filterValues = Object.assign({}, this.filterValues);
      delete filterValues[fieldId];
      this.filterValues = filterValues;
    }
    this.filterChange.emit({ filter: generateFilters(this.filterFields, this.filterValues) });
  }
  onTextChange(filterText) {
    this.filterValues = Object.assign(Object.assign({}, this.filterValues), { text: filterText });
    clearTimeout(this.quickSearchDelayTimer);
    this.quickSearchDelayTimer = setTimeout(() => {
      this.filterChange.emit({ filter: generateFilters(this.filterFields, this.filterValues) });
    }, 500);
  }
  static get is() { return "dvn-filter-simple"; }
  static get encapsulation() { return "scoped"; }
  static get properties() { return {
    "fields": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "BaseField[]",
        "resolved": "BaseField[]",
        "references": {
          "BaseField": {
            "location": "import",
            "path": "@dvn/core/doldurma/src/BaseField"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "config": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "QuickSearchConfig",
        "resolved": "QuickSearchConfig",
        "references": {
          "QuickSearchConfig": {
            "location": "import",
            "path": "@dvn/core/models/quick-search-config"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "leaned": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "leaned",
      "reflect": false,
      "defaultValue": "false"
    },
    "placeholder": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      },
      "attribute": "placeholder",
      "reflect": false,
      "defaultValue": "'Quick search'"
    }
  }; }
  static get states() { return {
    "filterFields": {},
    "filterValues": {}
  }; }
  static get events() { return [{
      "method": "filterChange",
      "name": "filterChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [],
        "text": ""
      },
      "complexType": {
        "original": "any",
        "resolved": "any",
        "references": {}
      }
    }]; }
}
