'use strict';

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

const index = require('./index-a1121663.js');
require('./index-16c91445.js');
const dataModel = require('./data-model-f9df493f.js');
require('./BaseField-f50bb8f0.js');
require('./FieldModelGenerator-61c1111e.js');
require('./date-8e8ca89a.js');
require('./index-21b225d5.js');
require('./table-config-d70a1046.js');
const listFetchState = require('./list-fetch-state-4917b40d.js');
const pairConfig = require('./pair-config-a5d7c3eb.js');
const ui = require('./ui-63800053.js');

const DvnAppViewAtamaMemberPairsOverviewView = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.list = listFetchState.ListFetchState.init();
    this.shownTab = 'pairs';
    this.onSizeChange = (size) => {
      this.size = size;
    };
  }
  componentWillLoad() {
    this.fetchMembershipConfigOfMemberToShow();
  }
  componentDidLoad() {
    this.uiHelper = new ui.ResponsiveUIHelper(this.el, this.onSizeChange);
  }
  fetchMembershipConfigOfMemberToShow() {
    var _a;
    const { notifier, view: { table, ui: { i18n } } } = this;
    const { memberToViewRefFieldId } = this.params;
    const memberToShowTableName = (_a = table.fields.find(f => f.type === 'reference' && f.id === memberToViewRefFieldId)) === null || _a === void 0 ? void 0 : _a.otherCollection;
    this.apiUye.getMembershipConfigWithName(memberToShowTableName).then(response => {
      this.membershipConfigToShow = response;
      this.fetchList();
    }, error => notifier.alert(i18n.fetchConfigurationError || 'Getting configuration failed.', error.status));
  }
  fetchList() {
    const { list } = this;
    if (list.isPending) {
      return;
    }
    const { membership, notifier, view: { table, ui: { i18n } } } = this;
    const { userRefFieldId } = this.params;
    this.list = list.beforeFetch();
    this.apiAtama.getPairsOfUser(table.base, table.name, userRefFieldId, membership._id)
      .then(response => this.list = list.afterFetch(response, false), error => {
      this.list = list.afterFetchError(error);
      notifier.alert(i18n.fetchListError || 'Fetching list failed.', error.status);
    });
  }
  onRequestAcceptOrReject() {
    this.fetchList();
  }
  onClickReceivedRequestUser(user) {
    this.action.emit({
      type: 'navigation',
      direction: 'forward',
      resetHistory: true,
      viewId: this.params.viewIdToViewRequestSender,
      params: {
        item: user,
      }
    });
  }
  render() {
    var _a;
    const { list, membershipConfigToShow, shownTab, size, view } = this;
    const { ui: { i18n } } = view;
    const showMobileUI = size < ui.ResponsiveSize.Medium;
    if (!membershipConfigToShow) {
      return (index.h("div", { class: "d-flex justify-content-center pt-3" }, index.h("dvn-progress", { indefinite: true })));
    }
    const { memberToViewRefFieldId, userRefFieldId } = this.params;
    // extract the member (to show) data from the pair data
    const itemsToShow = (list.data || [])
      .map(item => {
      const userData = dataModel.DataModel.decode(item[memberToViewRefFieldId][0]);
      // TODO find another pass the pair id
      userData.pairId = item._id;
      return userData;
    });
    const arePairsFetched = !!list.data;
    const pairConfig$1 = pairConfig.PairConfig.decode(view.table);
    // check if this user needs to see selection UI
    const selectionConfig = pairConfig.getPairConfigSelectionConfigForRole(pairConfig$1, userRefFieldId);
    // check if this user needs to see approve UI (meaning some other users are selecting this user)
    const selectionConfigTargeted = pairConfig.getPairConfigSelectionConfigTargetingRole(pairConfig$1, userRefFieldId);
    // show selection active card if the selection is on and the pair limit is more than the active pair count
    const showSelectionActiveCard = arePairsFetched && (selectionConfig === null || selectionConfig === void 0 ? void 0 : selectionConfig.isActive())
      && (selectionConfig.pairLimit === 0 || selectionConfig.pairLimit > itemsToShow.length);
    const showPairList = shownTab === 'pairs' || !showMobileUI;
    const isReceivedRequestListIncluded = !!selectionConfigTargeted;
    const showReceivedRequestList = !!isReceivedRequestListIncluded && (shownTab === 'pair-requests' || !showMobileUI);
    const showHeaders = !showMobileUI;
    return (index.h(index.Host, { class: "d-block" }, showMobileUI && isReceivedRequestListIncluded &&
      index.h("div", { class: "d-flex justify-content-center pt-3" }, index.h("dvn-content-switcher", { options: [
          {
            label: i18n.pairs || 'Pairs',
            value: 'pairs',
          },
          {
            label: i18n.receivedRequests || 'Received Requests',
            value: 'pair-requests',
          },
        ], onChange: (event) => this.shownTab = event.detail, value: this.shownTab })), showSelectionActiveCard &&
      index.h("div", { class: "pt-3" }, index.h("dvn-card", { elevation: 0, leaned: true, tint: i18n.selectionCardTint || 'primary' }, index.h("dvn-card-header", { slot: "header", tint: i18n.selectionCardTint || 'primary', title: i18n.selectionCardTitle || 'Selection is on!' }), i18n.selectionCardMessage || 'You can view all available users and send them requests.', index.h("dvn-card-footer", { slot: "footer", alignRight: true }, index.h("dvn-button", { tint: i18n.selectionCardActionTint || 'secondary', onClick: () => this.action.emit({
          type: 'navigation',
          direction: 'forward',
          resetHistory: true,
          viewId: this.params.viewIdToNavigateToSelectPair,
        }) }, i18n.selectionCardActionLabel || 'Go to selection page')))), index.h("div", { class: "row" }, showPairList &&
      index.h("div", { class: {
          'col-md-12 pt-3': true,
          'col-lg-8': showReceivedRequestList,
        } }, showReceivedRequestList &&
        index.h("div", { class: "ml-2 mb-2 d-flex align-items-center" }, showHeaders &&
          index.h("small", null, index.h("b", null, ((_a = i18n.pairs) === null || _a === void 0 ? void 0 : _a.toLocaleUpperCase()) || 'PAIRS'), "\u00A0"), (list.isPending && list.data) &&
          index.h("dvn-progress", { indefinite: true, size: 14, strokeWidth: 2, tint: "primary" })), (list.isPending && !list.data) &&
        index.h("div", { class: "d-flex justify-content-center" }, index.h("dvn-progress", { indefinite: true })), list.data &&
        index.h("div", null, itemsToShow.length > 0
          ? index.h("dvn-card-list-view", { config: view.ui, fields: membershipConfigToShow.fields, items: itemsToShow })
          : index.h("dvn-card", { elevation: 0, leaned: true, tint: "warning" }, i18n.noPairsMessage || 'You don\'t have any pairs.'))), showReceivedRequestList &&
      index.h("div", { class: {
          ['col-sm-12 col-md-4 pt-3']: !showMobileUI,
          ['col-sm-12 col-md-12 pt-3']: showMobileUI,
        } }, index.h("atama-received-request-list", { i18n: i18n, apiAtama: this.apiAtama, onActionDone: () => this.onRequestAcceptOrReject(), onViewRequestSender: (user) => this.onClickReceivedRequestUser(user), showHeader: showHeaders, table: this.view.table })))));
  }
  get el() { return index.getElement(this); }
};

exports.dvn_app_view_atama_member_pairs_overview_view = DvnAppViewAtamaMemberPairsOverviewView;
