'use strict';

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

const index = require('./index-1ee80297.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-7befe791.js');
const actionUtil = require('./action-util-1440204f.js');
const actions = require('./actions-d7eabd95.js');
const i18n$1 = require('./i18n-592bf756.js');
require('./filter-d5040f65.js');
require('./data-model-c51de090.js');
require('./i18n-a8cc9ef1.js');
require('./i18n-0bdc9383.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}";

let 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) {
            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;

let DvnAppViewTakvimAvailableTimePickerView = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.invalidation = index.createEvent(this, "invalidation", 7);
    this.notification = index.createEvent(this, "notification", 7);
    this.action = index.createEvent(this, "action", 7);
  }
  async componentWillLoad() {
    this.strings = await this.apiSozluk.getContent(this.view.i18nEntryId, i18n$1.takvimAvailableTimePickerI18nDefault);
  }
  render() {
    const { createdMeeting, membership, user, userSettings, params: { viewedUserMembershipConfig, viewedUserMembership }, strings, view, } = this;
    return (index.h(index.Host, { class: "d-block" }, index.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 actions.InvalidationAction) {
            this.invalidation.emit(eventToEmit);
          }
          if (eventToEmit instanceof actions.NotificationAction) {
            this.notification.emit(eventToEmit);
          }
        });
      }, strings: strings, weekStartDay: userSettings.weekStartDay }), index.h("dvn-dialog", { title: strings.meetingIsScheduled, shown: !!createdMeeting, onDismiss: () => this.createdMeeting = undefined }, index.h("div", { slot: "actions" }, index.h("dvn-button", { view: "flat", onClick: () => this.createdMeeting = undefined }, strings.stayOnPage), index.h("dvn-button", { tint: "primary", view: "flat", onClick: () => this.action.emit(actionUtil.generateActionEmitData({}, this.createdMeeting, this.view.successNavigationAction)) }, strings.goToMeetingPage)))));
  }
};

exports.dvn_app_view_list_view_card = DvnAppViewListViewCard;
exports.dvn_app_view_takvim_available_time_picker_view = DvnAppViewTakvimAvailableTimePickerView;
