import { r as registerInstance, h, H as Host } from './index-b7735e0d.js';
import { g as getDataModelField } from './data-model-f7ccf826.js';
import { I as ItemFetchState } from './item-fetch-state-610938e5.js';
import { L as ListFetchState } from './list-fetch-state-d8c2f8ef.js';
import { C as CalendarUtils } from './calendar-67e174ec.js';
import { g as getUserFullName } from './User-1ef5a482.js';
import { g as getTintForStatus, a as getLabelForStatus } from './pair-request-ee2b9bbc.js';
import './index-3958847a.js';
import './_commonjsHelpers-27f122dd.js';
import './moment-6f6ab70c.js';

let AtamaReceivedRequestList = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.list = ListFetchState.init();
    this.actionStatus = 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 (h(Host, null, (showHeader || showProgress) &&
      h("div", { class: "ml-2 mb-2 d-flex align-items-center" }, showHeader &&
        h("small", null, h("b", null, ((_a = i18n.receivedRequests) === null || _a === void 0 ? void 0 : _a.toLocaleUpperCase()) || 'RECEIVED REQUESTS'), "\u00A0"), showProgress &&
        h("dvn-progress", { indefinite: true, size: 14, strokeWidth: 2, tint: "primary" })), list.isPending && !list.data &&
      h("div", { class: "d-flex justify-content-center" }, h("dvn-progress", { indefinite: true })), (list.data && list.data.length) === 0 &&
      h("dvn-card", { elevation: 0, leaned: true, tint: "neutral" }, i18n.noReceivedRequests || 'There are no pending requests.'), list.data && list.data.map((request) => (h("dvn-card", { key: request._id, elevation: 0, leaned: true, bodyLeaned: true, style: { margin: '0 0 1rem 0' } }, h("div", { class: "d-flex justify-content-between align-items-center pr-2" }, h("div", { style: { cursor: 'pointer' } }, h("dvn-info-summary", { title: getUserFullName(getDataModelField(request, 'sentBy')), subTitle: request.requestedAt
        ? CalendarUtils.format(new Date(request.requestedAt * 1000), i18n.sentOnDateFormat || 'DD MMM YYYY, HH:mm') : undefined, onClick: () => this.onViewRequestSender(getDataModelField(request, 'sentBy')) }, h("dvn-image", { slot: "media", src: getDataModelField(request, 'sentBy').imageUrl, shape: "circle" }))), request.status !== 'requested' &&
      h("dvn-button", { leaned: true, scale: "small", shape: "rounded", tagMode: true, tint: getTintForStatus(request), view: "ghost" }, getLabelForStatus(i18n.statusLabels, request))), request.status === 'requested' &&
      h("dvn-card-footer", { slot: "footer", alignRight: true }, h("dvn-button", { disabled: actionStatus.isPending, leaned: true, scale: "small", tint: "secondary", onClick: () => this.acceptRequest(request) }, i18n.accept || 'Accept'), h("dvn-button", { disabled: actionStatus.isPending, scale: "small", tint: "danger", onClick: () => this.rejectRequest(request) }, i18n.reject || 'Reject')))))));
  }
};

export { AtamaReceivedRequestList as atama_received_request_list };
