'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const index = require('./index-a1121663.js');
require('./data-model-f9df493f.js');
const listFetchState = require('./list-fetch-state-4917b40d.js');

function buildSort(query) {
  if (query && query.sort) {
    return query.sort;
  }
  return undefined;
}
function buildQuery(query, quickFilter) {
  let queryFilter = query ? query.filter : undefined;
  if (quickFilter) {
    if (query && query.filter) {
      queryFilter = {
        operator: 'and',
        filters: [query.filter, quickFilter]
      };
    }
    else {
      queryFilter = quickFilter;
    }
  }
  return queryFilter;
}

const DvnAppViewListViewCard = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.list = listFetchState.ListFetchState.init();
  }
  async invalidate(_event) {
    this.list = listFetchState.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 (index.h(index.Host, null, index.h("div", { class: "mt-3 pt-3" }, list.data &&
      index.h("dvn-card-list-view", { config: view.ui, fields: view.table.fields, items: list.data }), list.error &&
      index.h("devingen-error-card", { error: list.error, message: "Fetching list failed.", onRetry: () => this.fetchList(true) }))));
  }
};

exports.dvn_app_view_list_view_card = DvnAppViewListViewCard;
