'use strict';

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

const index = require('./index-1141a245.js');
const util = require('./util-eba4e77f.js');
const filter = require('./filter-a24b968a.js');
const i18n = require('./i18n-a565202d.js');
const listFetchState = require('./list-fetch-state-6f8f3bf8.js');
require('./filter-d5040f65.js');
require('./data-model-01c368bc.js');

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

const dvnAppViewListViewCardCss = "dvn-app-view-list-view-card{-ms-flex:1;flex:1;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;overflow:hidden;padding:1rem}dvn-app-view-list-view-card dvn-filter-simple{margin-bottom:1rem}dvn-app-view-list-view-card .list-container{-ms-flex:1;flex:1;overflow-y:auto;overflow-x:hidden}";

const DvnAppViewListViewCard = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.list = listFetchState.ListFetchState.init();
    this.onSimpleFilterChange = ({ filter }) => {
      this.quickFilter = filter;
      this.fetchList(true, true);
    };
  }
  async invalidate(_event) {
    this.fetchList(true, true);
  }
  ;
  /**
   * This method is called by the parent component when the parent is re-rendering its' content.
   * The list loses the scroll position when the parent is re-rendering so we set the list scroll
   * position manually to retain the last position.
   */
  async retainPosition() {
    const lc = this.el.querySelector(`.list-container`);
    if (lc) {
      lc.scrollTop = this.scrollPosition;
    }
  }
  ;
  async componentWillLoad() {
    this.strings = await this.apiSozluk.getContent(this.view.i18nEntryId, i18n.dvnCardListViewI18nDefault);
    this.fetchList();
  }
  fetchList(ignoreError = false, invalidate = false) {
    const { list, quickFilter } = this;
    if (list.isPending || (!invalidate && list.isEndReached()) || (list.error && !ignoreError)) {
      return;
    }
    const { query, table, ui } = this.view;
    // get the default quick filter value if the fields have preset values
    const defaultQuickFilter = ui.quickSearch ? util.generateDefaultFilters(ui.quickSearch, table.fields) : undefined;
    const queryFilter = buildQuery(query, quickFilter || defaultQuickFilter, this.params);
    this.list = list.beforeFetch();
    this.apiData.query(table.name, {
      fields: query.fields,
      filter: queryFilter,
      limit: 25,
      skip: invalidate ? 0 : (list.data ? list.data.length : 0),
      sort: buildSort(query),
    }).then(response => {
      this.list = list.afterFetch(response, !invalidate);
    }, error => {
      this.list = list.afterFetchError(error);
    });
  }
  render() {
    const { list, view } = this;
    const { ui } = view;
    return (index.h(index.Host, null, ui.quickSearch &&
      index.h("dvn-filter-simple", { fields: this.view.table.fields, progressing: list.isPending && !!list.data, config: ui.quickSearch, onFilterChange: (e) => this.onSimpleFilterChange(e.detail) }), list.data &&
      index.h("div", { class: "list-container", onScroll: (e) => {
          const el = e.target;
          this.scrollPosition = el.scrollTop;
          if (el.offsetHeight + el.scrollTop >= el.scrollHeight - 100) {
            this.fetchList();
          }
        } }, index.h("dvn-card-list-view", { config: view.ui, fields: view.table.fields, items: list.data, strings: this.strings }), (list.isPending && !ui.quickSearch) &&
        index.h("div", { class: "d-flex justify-content-center pb-3" }, index.h("dvn-progress", { indefinite: true, tint: "primary" }))), (list.isPending && !list.data) &&
      index.h("div", { class: "d-flex justify-content-center" }, index.h("dvn-progress", { indefinite: true, tint: "primary" })), list.error &&
      index.h("devingen-error-card", { error: list.error, message: "Fetching list failed.", onRetry: () => this.fetchList(true) })));
  }
  get el() { return index.getElement(this); }
};
DvnAppViewListViewCard.style = dvnAppViewListViewCardCss;

exports.dvn_app_view_list_view_card = DvnAppViewListViewCard;
