import { r as registerInstance, h, H as Host, g as getElement, c as createEvent } from './index-55faf181.js';
import { g as generateDefaultFilters } from './util-c68b7c9b.js';
import { r as replaceParamsInFilter, c as cloneFilter } from './filter-8542e288.js';
import { L as ListFetchState } from './list-fetch-state-623a5ba4.js';
import { g as generateActionEmitData } from './action-util-c91834e1.js';
import { I as InvalidationAction, N as NotificationAction } from './actions-b9a0f808.js';
import './filter-cbaa3d7d.js';
import './data-model-f5160369.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 replaceParamsInFilter(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) {
    registerInstance(this, hostRef);
    this.list = 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;
    }
  }
  ;
  componentWillLoad() {
    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 ? 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 (h(Host, null, ui.quickSearch &&
      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 &&
      h("div", { class: "list-container", onScroll: (e) => {
          const el = e.target;
          this.scrollPosition = el.scrollTop;
          if (el.offsetHeight + el.scrollTop >= el.scrollHeight) {
            this.fetchList();
          }
        } }, h("dvn-card-list-view", { config: view.ui, fields: view.table.fields, items: list.data }), (list.isPending && !ui.quickSearch) &&
        h("div", { class: "d-flex justify-content-center pb-3" }, h("dvn-progress", { indefinite: true, tint: "primary" }))), (list.isPending && !list.data) &&
      h("div", { class: "d-flex justify-content-center" }, h("dvn-progress", { indefinite: true, tint: "primary" })), list.error &&
      h("devingen-error-card", { error: list.error, message: "Fetching list failed.", onRetry: () => this.fetchList(true) })));
  }
  get el() { return getElement(this); }
};
DvnAppViewListViewCard.style = dvnAppViewListViewCardCss;

const DvnAppViewTakvimAvailableTimePickerView = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.invalidation = createEvent(this, "invalidation", 7);
    this.notification = createEvent(this, "notification", 7);
    this.action = createEvent(this, "action", 7);
  }
  render() {
    const { createdMeeting, membership, user, userSettings, params: { viewedUserMembershipConfig, viewedUserMembership }, view, } = this;
    return (h(Host, { class: "d-block" }, h("takvim-available-time-picker", { apiTakvim: this.apiTakvim, userId: user._id, membershipConfigId: membership.config._id, userSettings: userSettings, viewedUserId: viewedUserMembership.user._id, viewedUserMembershipConfigId: viewedUserMembershipConfig._id, calendarConfigId: view.table._id, onCreateMeeting: (e) => {
        var _a, _b;
        if (view.successNavigationAction) {
          // show go to meeting page action confirmation dialog
          this.createdMeeting = e.detail;
        }
        (_b = (_a = view.events) === null || _a === void 0 ? void 0 : _a.success) === null || _b === void 0 ? void 0 : _b.forEach(eventToEmit => {
          if (eventToEmit instanceof InvalidationAction) {
            this.invalidation.emit(eventToEmit);
          }
          if (eventToEmit instanceof NotificationAction) {
            this.notification.emit(eventToEmit);
          }
        });
      } }), h("dvn-dialog", { title: "Meeting is scheduled.", shown: !!createdMeeting, onDismiss: () => this.createdMeeting = undefined }, h("div", { slot: "actions" }, h("dvn-button", { view: "flat", onClick: () => this.createdMeeting = undefined }, "Stay on page"), h("dvn-button", { tint: "primary", view: "flat", onClick: () => this.action.emit(generateActionEmitData({}, this.createdMeeting, this.view.successNavigationAction)) }, "Go to meeting page")))));
  }
};

export { DvnAppViewListViewCard as dvn_app_view_list_view_card, DvnAppViewTakvimAvailableTimePickerView as dvn_app_view_takvim_available_time_picker_view };
