import { r as registerInstance, c as createEvent, h, H as Host, g as getElement } from './index-b7735e0d.js';
import { R as ResponsiveUIHelper, a as ResponsiveSize } from './ui-c7f31f6f.js';
import { g as getDataModelField } from './data-model-f7ccf826.js';
import { I as ItemFetchState } from './item-fetch-state-610938e5.js';
import { g as getPairConfigMemberCollection } from './pair-config-cf56a965.js';
import { c as capitalize } from './string-3065a411.js';
import { a as getUserInitials, g as getUserFullName } from './User-1ef5a482.js';
import { t as takvimAvailableTimePickerI18nDefault } from './i18n-c00c9a61.js';
import './table-config-0c06fb1f.js';
import './FieldModelGenerator-020f4a04.js';
import './reference-field-0e7cd534.js';
import './BaseField-461b6d0c.js';
import './index-7b119e61.js';
import './index-403101cb.js';
import './date-ce777316.js';
import './index-1d3efe49.js';
import './index-eb14fd2d.js';
import './index-80ec23ef.js';
import './index-4c39b6a6.js';
import './i18n-27a6b633.js';
import './i18n-f1ddd0c3.js';

function buildSummaryCardViewConfig(view) {
  var _a, _b;
  return Object.assign(Object.assign({}, (_a = view.ui.main) === null || _a === void 0 ? void 0 : _a.infoSummaryCardViewConfig), { footer: {
      actions: [
        {
          ui: {
            label: ((_b = view.ui.i18n) === null || _b === void 0 ? void 0 : _b.seeMore) || 'See More',
            name: 'open-member-detail-view',
            view: 'flat'
          }
        }
      ]
    } });
}
let DvnAppViewAtamaMemberPairDetailView = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.action = createEvent(this, "action", 7);
    this.item = 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();
      // 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 = table;
    this.apiUye.getMembershipConfigWithName(getPairConfigMemberCollection(pairConfig, memberToViewRefFieldId)).then(response => {
      this.membershipConfigToShow = response;
      this.fetchItem();
    }, error => notifier.alert((i18n === null || i18n === void 0 ? void 0 : 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 = table;
    const { memberToViewRefFieldId, memberToViewRefFieldValue } = this.params;
    this.item = item.beforeFetch();
    this.apiData.get(getPairConfigMemberCollection(pairConfig, memberToViewRefFieldId), memberToViewRefFieldValue, {
      fields: membershipConfigToShow.fields,
    }).then(response => this.item = item.afterFetch(response), error => {
      this.item = item.afterFetchError(error);
      notifier.alert((i18n === null || i18n === void 0 ? void 0 : 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,
      });
    });
  }
  async componentWillLoad() {
    this.strings = await this.apiSozluk.getContent(this.view.i18nEntryId, takvimAvailableTimePickerI18nDefault);
  }
  componentDidLoad() {
    this.fetchMembershipConfigOfMemberToShow();
    this.uiHelper = new ResponsiveUIHelper(this.el, this.onSizeChange);
  }
  render() {
    var _a, _b, _c, _d, _e;
    const { membershipConfigToShow, item, size, strings, userSettings, view: { ui: { i18n } } } = this;
    const showMobileUI = size < ResponsiveSize.Medium;
    if (!item.data || !membershipConfigToShow) {
      return (h(Host, { class: "d-flex justify-content-center pt-3 mt-3" }, h("dvn-progress", { indefinite: true })));
    }
    const { view } = this;
    const pairConfig = view.table;
    const calendarConfigId = (_a = pairConfig.calendarConfig) === null || _a === void 0 ? void 0 : _a._id;
    const membership = item.data;
    // TODO add this to config because we need this feature for groups as well
    const userToView = getDataModelField(membership, 'user');
    const infoSummaryCardViewConfig = (_b = view.ui.main) === null || _b === void 0 ? void 0 : _b.infoSummaryCardViewConfig;
    const canCompletePairProcess = (_c = pairConfig.actionPermissions[this.params.userRefFieldId]) === null || _c === void 0 ? void 0 : _c.completePair;
    return (h(Host, { class: "d-block mt-3 pt-3" }, h("div", { class: "row" }, h("div", { class: {
        'col-sm-12 col-md-9': !showMobileUI,
        'col-sm-12': showMobileUI,
      } }, h("dvn-card", { elevation: 0, leaned: true }, h("div", { class: "d-flex align-items-center" }, h("dvn-image", { src: userToView.imageUrl, shape: "circle", size: 48, text: getUserInitials(userToView) }), h("div", { class: "d-flex flex-column ml-2" }, h("b", { class: "mb-1" }, getUserFullName(userToView)), h("small", null, capitalize((i18n === null || i18n === void 0 ? void 0 : i18n.roleName) || membershipConfigToShow.roleName)))), (showMobileUI && this.params.showSendMessageButton) &&
      h("dvn-card-footer", { slot: "footer", alignRight: true }, this.params.showSendMessageButton &&
        h("dvn-icon-button", { iconName: "uil uil-envelope", onClick: () => this.action.emit({
            name: 'send-message',
            data: {
              item: item.data,
            }
          }), view: "outline" }))), infoSummaryCardViewConfig &&
      h("div", { class: "mt-3" }, h("div", { class: "ml-2 mb-2" }, h("small", null, h("b", null, (i18n === null || i18n === void 0 ? void 0 : i18n.info) || 'INFO'))), h("dvn-card-view", { config: buildSummaryCardViewConfig(view), fields: membershipConfigToShow.fields, item: membership, leaned: true }), h("dvn-dialog", { shown: this.showInfoDetailDialog, onDismiss: () => this.showInfoDetailDialog = false, raw: true }, h("dvn-card-view", { config: this.view.ui.main.infoDetailCardViewConfig, fields: membershipConfigToShow.fields, item: membership }))), calendarConfigId &&
      h("div", { class: "mt-3" }, h("div", { class: "ml-2 mb-2" }, h("small", null, h("b", null, (i18n === null || i18n === void 0 ? void 0 : i18n.getMeeting) || 'GET MEETING'))), h("takvim-available-time-picker", { apiTakvim: this.apiTakvim, userId: this.user._id, membershipConfigId: this.membership.config._id, userSettings: this.userSettings, viewedUserId: this.item.data.user._id, viewedUserMembershipConfigId: membershipConfigToShow._id, calendarConfigId: calendarConfigId, meetingTypes: (_d = pairConfig.structure) === null || _d === void 0 ? void 0 : _d.meetingConfigs.map(mc => ({
          id: mc.id,
          name: mc.name,
          maxCount: mc.meetingCount,
        })), onCreateMeeting: () => {
          var _a, _b, _c, _d;
          (_b = (_a = this.el.querySelector(`takvim-meeting-list`)) === null || _a === void 0 ? void 0 : _a.invalidate) === null || _b === void 0 ? void 0 : _b.call(_a);
          (_d = (_c = this.el.querySelector(`atama-structured-pair-overview`)) === null || _c === void 0 ? void 0 : _c.invalidate) === null || _d === void 0 ? void 0 : _d.call(_c);
        }, strings: strings, weekStartDay: userSettings.weekStartDay })), pairConfig.structure
      ? h("div", { class: "mt-3" }, h("div", { class: "ml-2 mb-2" }, h("small", null, h("b", null, (i18n === null || i18n === void 0 ? void 0 : i18n.meetings) || 'MEETINGS'))), h("atama-structured-pair-overview", { i18n: i18n, apiAtama: this.apiAtama, apiTakvim: this.apiTakvim, pairConfig: pairConfig, userRefFieldId: this.params.userRefFieldId, size: size, sessionUserId: this.user._id, userId: this.item.data.user._id }))
      : h("div", { class: "mt-3" }, h("div", { class: "ml-2 mb-2" }, h("small", null, h("b", null, (i18n === null || i18n === void 0 ? void 0 : i18n.meetings) || 'MEETINGS'))), h("takvim-meeting-list", { apiTakvim: this.apiTakvim, calendarConfigId: (_e = pairConfig.calendarConfig) === null || _e === void 0 ? void 0 : _e._id, guestIDs: [this.item.data.user._id, this.user._id], userSettings: this.userSettings }))), !showMobileUI &&
      h("div", { class: "col-sm-12 col-md-3" }, h("div", null, this.params.showSendMessageButton &&
        h("dvn-button", { leaned: true, onClick: () => this.action.emit({
            name: 'send-message',
            data: {
              item: item.data,
            }
          }), tint: "primary", view: "flat" }, (i18n === null || i18n === void 0 ? void 0 : i18n.sendMessage) || 'Send Message')), canCompletePairProcess &&
        h("div", null, h("dvn-button", { leaned: true, onClick: () => {
            this.showEndPairConfirmationDialog = true;
          }, tint: "primary", view: "flat" }, (i18n === null || i18n === void 0 ? void 0 : i18n.endPair) || 'Finish This Process'), h("dvn-dialog", { title: (i18n === null || i18n === void 0 ? void 0 : i18n.endPairConfirmationTitle) || 'Are you sure to end the process?', shown: this.showEndPairConfirmationDialog, onDismiss: () => this.showEndPairConfirmationDialog = false }, (i18n === null || i18n === void 0 ? void 0 : i18n.endPairConfirmationDescription) || 'This action removes the pair from your account. You will not be able to schedule meetings with this person.', h("div", { slot: "actions" }, h("dvn-button", { tint: "primary", view: "flat", onClick: () => this.showEndPairConfirmationDialog = false }, (i18n === null || i18n === void 0 ? void 0 : i18n.cancel) || 'Cancel'), h("dvn-button", { tint: "danger", view: "flat", onClick: () => this.completePair() }, (i18n === null || i18n === void 0 ? void 0 : i18n.endPairConfirmationFinishPair) || 'Finish the Process'))))))));
  }
  get el() { return getElement(this); }
};

export { DvnAppViewAtamaMemberPairDetailView as dvn_app_view_atama_member_pair_detail_view };
