'use strict';

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

const index = require('./index-7cdf1df1.js');
require('./index-c2a077ae.js');
const dataModel = require('./data-model-f9df493f.js');
require('./BaseField-f50bb8f0.js');
require('./FieldModelGenerator-5a321a60.js');
require('./date-8e8ca89a.js');
require('./index-e79eb186.js');
require('./table-config-b20b4da1.js');
const string = require('./string-9e6ebf63.js');
const User = require('./User-ed078a17.js');
const pairConfig = require('./pair-config-3ff31ab1.js');
const ui = require('./ui-63800053.js');
const itemFetchState = require('./item-fetch-state-18978ab4.js');

function buildSummaryCardViewConfig(view) {
  var _a;
  return Object.assign(Object.assign({}, (_a = view.ui.main) === null || _a === void 0 ? void 0 : _a.infoSummaryCardViewConfig), { footer: {
      actions: [
        {
          ui: {
            label: view.ui.i18n.seeMore || 'See More',
            name: 'open-member-detail-view',
            view: 'flat'
          }
        }
      ]
    } });
}
const DvnAppViewAtamaMemberPairDetailView = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.item = itemFetchState.ItemFetchState.init();
    this.showInfoDetailDialog = false;
    this.showEndPairConfirmationDialog = false;
    this.onSizeChange = (size) => {
      this.size = size;
    };
  }
  onAction(event) {
    const action = event.detail;
    if (action.name === 'open-member-detail-view') {
      event.preventDefault();
      this.showInfoDetailDialog = true;
    }
    if (action.name === 'create-meeting-with-config-only') {
      event.preventDefault();
      if (!this.params.showCreateMeetingButton) {
        // open availabilities page if the current user cannot create meeting
        // TODO also check if mentor can create availabilities, otherwise forwarding user there won't make sense
        this.action.emit({
          name: 'view-availabilities',
          data: {
            item: this.item.data,
          }
        });
        return;
      }
      // the children who fires this event doesn't have the item detail
      // catch the event, add the item details and fire the same event
      this.action.emit({
        name: 'create-meeting',
        data: {
          meetingConfig: event.detail.data.meetingConfig,
          item: this.item.data,
        }
      });
    }
  }
  fetchMembershipConfigOfMemberToShow() {
    const { notifier, view: { ui: { i18n }, table } } = this;
    const { memberToViewRefFieldId } = this.params;
    const pairConfig$1 = table;
    this.apiUye.getMembershipConfigWithName(pairConfig.getPairConfigMemberCollection(pairConfig$1, memberToViewRefFieldId)).then(response => {
      this.membershipConfigToShow = response;
      this.fetchItem();
    }, error => notifier.alert(i18n.fetchConfigurationError || 'Getting configuration failed.', error.status));
  }
  fetchItem(ignoreError = false) {
    const { membershipConfigToShow, item, view: { ui: { i18n } } } = this;
    if (item.isPending || (item.error && !ignoreError)) {
      return;
    }
    const { notifier, view: { table } } = this;
    const pairConfig$1 = table;
    const { memberToViewRefFieldId, memberToViewRefFieldValue } = this.params;
    this.item = item.beforeFetch();
    this.apiData.get(pairConfig.getPairConfigMemberCollection(pairConfig$1, memberToViewRefFieldId), memberToViewRefFieldValue, {
      fields: membershipConfigToShow.fields,
    }).then(response => this.item = item.afterFetch(response), error => {
      this.item = item.afterFetchError(error);
      notifier.alert(i18n.fetchItemError || 'Getting item failed.', error.status);
    });
  }
  completePair() {
    this.apiAtama.endPair('', '', this.params.pairId).then(() => {
      this.action.emit({
        type: 'navigation',
        direction: 'forward',
        resetHistory: true,
        viewId: this.params.viewIdToNavigateOnEndPair,
      });
    });
  }
  componentDidLoad() {
    this.fetchMembershipConfigOfMemberToShow();
    this.uiHelper = new ui.ResponsiveUIHelper(this.el, this.onSizeChange);
  }
  render() {
    var _a, _b;
    const { membershipConfigToShow, item, size, view: { ui: { i18n } } } = this;
    const showMobileUI = size < ui.ResponsiveSize.Medium;
    if (!item.data || !membershipConfigToShow) {
      return (index.h(index.Host, { class: "d-flex justify-content-center pt-3 mt-3" }, index.h("dvn-progress", { indefinite: true })));
    }
    const { view } = this;
    const pairConfig = view.table;
    const membership = item.data;
    // TODO add this to config because we need this feature for groups as well
    const userToView = dataModel.getDataModelField(membership, 'user');
    const infoSummaryCardViewConfig = (_a = view.ui.main) === null || _a === void 0 ? void 0 : _a.infoSummaryCardViewConfig;
    const canCompletePairProcess = (_b = pairConfig.actionPermissions[this.params.userRefFieldId]) === null || _b === void 0 ? void 0 : _b.completePair;
    return (index.h(index.Host, { class: "d-block mt-3 pt-3" }, index.h("div", { class: "row" }, index.h("div", { class: {
        'col-sm-12 col-md-9': !showMobileUI,
        'col-sm-12': showMobileUI,
      } }, index.h("dvn-card", { elevation: 0, leaned: true }, index.h("div", { class: "d-flex align-items-center" }, index.h("dvn-image", { src: userToView.imageUrl, shape: "circle", size: 48, text: User.getUserInitials(userToView) }), index.h("div", { class: "d-flex flex-column ml-2" }, index.h("b", { class: "mb-1" }, User.getUserFullName(userToView)), index.h("small", null, string.capitalize(i18n.roleName || membershipConfigToShow.roleName)))), (showMobileUI && (this.params.showSendMessageButton || this.params.showAvailabilitiesButton)) &&
      index.h("dvn-card-footer", { slot: "footer", alignRight: true }, this.params.showSendMessageButton &&
        index.h("dvn-icon-button", { iconName: "uil uil-envelope", onClick: () => this.action.emit({
            name: 'send-message',
            data: {
              item: item.data,
            }
          }), view: "outline" }), this.params.showAvailabilitiesButton &&
        index.h("dvn-icon-button", { iconName: "uil uil-calendar-alt", onClick: () => this.action.emit({
            name: 'view-availabilities',
            data: {
              item: item.data,
            }
          }), view: "outline" }))), infoSummaryCardViewConfig &&
      index.h("div", { class: "mt-3" }, index.h("div", { class: "ml-2 mb-2" }, index.h("small", null, index.h("b", null, i18n.info || 'INFO'))), index.h("dvn-card-view", { config: buildSummaryCardViewConfig(view), fields: membershipConfigToShow.fields, item: membership, leaned: true }), index.h("dvn-dialog", { shown: this.showInfoDetailDialog, onDismiss: () => this.showInfoDetailDialog = false, raw: true }, index.h("dvn-card-view", { config: this.view.ui.main.infoDetailCardViewConfig, fields: membershipConfigToShow.fields, item: membership }))), pairConfig.structure &&
      index.h("div", { class: "mt-3" }, index.h("div", { class: "ml-2 mb-2" }, index.h("small", null, index.h("b", null, i18n.meetings || 'MEETINGS'))), index.h("atama-structured-pair-overview", { i18n: i18n, apiAtama: this.apiAtama, pairConfig: pairConfig, userRefFieldId: this.params.userRefFieldId, showCreateMeetingButton: this.params.showAvailabilitiesButton || this.params.showCreateMeetingButton, size: size }))), !showMobileUI &&
      index.h("div", { class: "col-sm-12 col-md-3" }, index.h("div", null, this.params.showAvailabilitiesButton &&
        index.h("dvn-button", { leaned: true, onClick: () => this.action.emit({
            name: 'send-message',
            data: {
              item: item.data,
            }
          }), tint: "primary", view: "flat" }, i18n.sendMessage || 'Send Message'), this.params.showAvailabilitiesButton &&
        index.h("dvn-button", { leaned: true, onClick: () => this.action.emit({
            name: 'view-availabilities',
            data: {
              item: item.data,
            }
          }), tint: "primary", view: "flat" }, i18n.createMeeting || 'Schedule Session')), canCompletePairProcess &&
        index.h("div", null, index.h("dvn-button", { leaned: true, onClick: () => {
            this.showEndPairConfirmationDialog = true;
          }, tint: "primary", view: "flat" }, i18n.endPair || 'Finish This Process'), index.h("dvn-dialog", { title: i18n.endPairConfirmationTitle || 'Are you sure to end the process?', shown: this.showEndPairConfirmationDialog, onDismiss: () => this.showEndPairConfirmationDialog = false }, i18n.endPairConfirmationDescription || 'This action removes the pair from your account. You will not be able to schedule meetings with this person.', index.h("div", { slot: "actions" }, index.h("dvn-button", { tint: "primary", view: "flat", onClick: () => this.showEndPairConfirmationDialog = false }, i18n.cancel || 'Cancel'), index.h("dvn-button", { tint: "danger", view: "flat", onClick: () => this.completePair() }, i18n.endPairConfirmationFinishPair || 'Finish the Process'))))))));
  }
  get el() { return index.getElement(this); }
};

exports.dvn_app_view_atama_member_pair_detail_view = DvnAppViewAtamaMemberPairDetailView;
