import { buildQuery, buildSort } from '@dvn/core/components/views/dvn-app-view-list-view-card/util';
import { ListFetchState } from '@dvn/core/models/list-fetch-state';
import { Component, h, Host, Method, Prop, State } from '@stencil/core';
export class DvnAppViewListViewCard {
  constructor() {
    this.list = ListFetchState.init();
  }
  async invalidate(_event) {
    this.list = ListFetchState.init();
  }
  ;
  fetchList(ignoreError = false) {
    const { list, quickFilter } = this;
    if (list.isPending || list.isEndReached() || (list.error && !ignoreError)) {
      return;
    }
    const { query, table } = this.view;
    this.list = list.beforeFetch();
    const queryFilter = buildQuery(query, quickFilter);
    this.apiData.query(table.name, {
      fields: query.fields,
      filter: queryFilter,
      limit: 25,
      skip: list.data ? list.data.length : 0,
      sort: buildSort(query),
    }).then(response => {
      this.list = list.afterFetch(response);
    }, error => {
      this.list = list.afterFetchError(error);
    });
  }
  componentWillLoad() {
    this.fetchList();
  }
  render() {
    const { list, view } = this;
    return (h(Host, null,
      h("div", { class: "mt-3 pt-3" },
        list.data &&
          h("dvn-card-list-view", { config: view.ui, fields: view.table.fields, items: list.data }),
        list.error &&
          h("devingen-error-card", { error: list.error, message: "Fetching list failed.", onRetry: () => this.fetchList(true) }))));
  }
  static get is() { return "dvn-app-view-list-view-card"; }
  static get properties() { return {
    "apiData": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "DataApi",
        "resolved": "DataApi",
        "references": {
          "DataApi": {
            "location": "import",
            "path": "@dvn/core/api"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    },
    "view": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "CardListViewConfig",
        "resolved": "CardListViewConfig",
        "references": {
          "CardListViewConfig": {
            "location": "import",
            "path": "@dvn/core/models/card-list-view-config"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [],
        "text": ""
      }
    }
  }; }
  static get states() { return {
    "list": {},
    "quickFilter": {},
    "quickFilterText": {}
  }; }
  static get methods() { return {
    "invalidate": {
      "complexType": {
        "signature": "(_event: CustomEvent<InvalidationAction>) => Promise<void>",
        "parameters": [{
            "tags": [],
            "text": ""
          }],
        "references": {
          "Promise": {
            "location": "global"
          },
          "CustomEvent": {
            "location": "global"
          },
          "InvalidationAction": {
            "location": "import",
            "path": "@dvn/core/models/actions"
          }
        },
        "return": "Promise<void>"
      },
      "docs": {
        "text": "",
        "tags": []
      }
    }
  }; }
}
