'use strict';

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

const index = require('./index-7cdf1df1.js');
const dataModel = require('./data-model-f9df493f.js');
const User = require('./User-ed078a17.js');
require('./_commonjsHelpers-206db00d.js');
require('./moment-d48a0164.js');
const calendar = require('./calendar-9f82f772.js');
const listFetchState = require('./list-fetch-state-4917b40d.js');
const itemFetchState = require('./item-fetch-state-18978ab4.js');
const pairRequest = require('./pair-request-21d16db8.js');

const AtamaReceivedRequestList = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.list = listFetchState.ListFetchState.init();
    this.actionStatus = itemFetchState.ItemFetchState.init();
    this.showHeader = true;
    this.acceptRequest = (request) => this.performAction('accept', request);
    this.rejectRequest = (request) => this.performAction('reject', request);
  }
  fetchList(ignoreError = false) {
    const { list, table } = this;
    if (list.isPending || (list.error && !ignoreError)) {
      return;
    }
    this.list = list.beforeFetch();
    this.apiAtama.getReceivedRequests(table.base, table.name)
      .then(response => {
      this.list = list.afterFetch(response, false);
    }, error => {
      this.list = list.afterFetchError(error);
    });
  }
  performAction(actionName, request) {
    const { actionStatus, table } = this;
    if (actionStatus.isPending) {
      return;
    }
    this.actionStatus = actionStatus.beforeFetch();
    const onSuccess = response => {
      this.actionStatus = actionStatus.afterFetch(response);
      this.onActionDone();
      this.fetchList();
    };
    const onFail = error => {
      this.actionStatus = actionStatus.afterFetchError(error);
    };
    if (actionName === 'accept') {
      this.apiAtama.acceptPairRequest(table.base, table.name, request._id).then(onSuccess, onFail);
    }
    else {
      this.apiAtama.rejectPairRequest(table.base, table.name, request._id).then(onSuccess, onFail);
    }
  }
  componentWillLoad() {
    this.fetchList();
  }
  render() {
    var _a;
    const { i18n, actionStatus, list, showHeader } = this;
    const showProgress = (list.isPending && list.data);
    return (index.h(index.Host, null, (showHeader || showProgress) &&
      index.h("div", { class: "ml-2 mb-2 d-flex align-items-center" }, showHeader &&
        index.h("small", null, index.h("b", null, ((_a = i18n.receivedRequests) === null || _a === void 0 ? void 0 : _a.toLocaleUpperCase()) || 'RECEIVED REQUESTS'), "\u00A0"), showProgress &&
        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 && list.data.length) === 0 &&
      index.h("dvn-card", { elevation: 0, leaned: true, tint: "neutral" }, i18n.noReceivedRequests || 'There are no pending requests.'), list.data && list.data.map((request) => (index.h("dvn-card", { key: request._id, elevation: 0, leaned: true, bodyLeaned: true, style: { margin: '0 0 1rem 0' } }, index.h("div", { class: "d-flex justify-content-between align-items-center pr-2" }, index.h("div", { style: { cursor: 'pointer' } }, index.h("dvn-info-summary", { title: User.getUserFullName(dataModel.getDataModelField(request, 'sentBy')), subTitle: request.requestedAt
        ? calendar.CalendarUtils.format(new Date(request.requestedAt * 1000), i18n.sentOnDateFormat || 'DD MMM YYYY, HH:mm') : undefined, onClick: () => this.onViewRequestSender(dataModel.getDataModelField(request, 'sentBy')) }, index.h("dvn-image", { slot: "media", src: dataModel.getDataModelField(request, 'sentBy').imageUrl, shape: "circle" }))), request.status !== 'requested' &&
      index.h("dvn-button", { leaned: true, scale: "small", shape: "rounded", tagMode: true, tint: pairRequest.getTintForStatus(request), view: "ghost" }, pairRequest.getLabelForStatus(i18n.statusLabels, request))), request.status === 'requested' &&
      index.h("dvn-card-footer", { slot: "footer", alignRight: true }, index.h("dvn-button", { disabled: actionStatus.isPending, leaned: true, scale: "small", tint: "secondary", onClick: () => this.acceptRequest(request) }, i18n.accept || 'Accept'), index.h("dvn-button", { disabled: actionStatus.isPending, scale: "small", tint: "danger", onClick: () => this.rejectRequest(request) }, i18n.reject || 'Reject')))))));
  }
};

exports.atama_received_request_list = AtamaReceivedRequestList;
