'use strict';

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

const index$1 = require('./index-1141a245.js');
const calendar = require('./calendar-e4b7baeb.js');
const User = require('./User-dbfbd45e.js');
const index$2 = require('./index-a2e99c85.js');
require('./apiBase-6c66ff1b.js');
require('./data-transfer-field-e8417d5f.js');
require('./filter-d5040f65.js');
const session = require('./session-f588baa3.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-b1eaa56d.js');
const sozlukEntries = require('./sozluk-entries-3b9686e7.js');
const models = require('./models-8b357ee7.js');
const compositeViewLinearConfig = require('./composite-view-linear-config-0dcc4b3c.js');
require('./index-b7855ed6.js');
const index = require('./index-61f9621a.js');
const actions = require('./actions-d7eabd95.js');
const appConfig = require('./app-config-d402bbe9.js');
const detailViewConfig = require('./detail-view-config-7bcb0034.js');
const tableConfig = require('./table-config-969d3038.js');
const viewConfig = require('./view-config-52c16db0.js');
const takvimAvailableTimePickerViewConfig = require('./takvim-available-time-picker-view-config-6bf1a5d8.js');
const commonWrapperUtil = require('./common-wrapper-util-95591ac2.js');
const takvimMeetingListViewConfig = require('./takvim-meeting-list-view-config-3d1568c6.js');
const attachment = require('./attachment-a0cdf203.js');
require('./index-b776fc14.js');
require('./_commonjsHelpers-206db00d.js');
require('./moment-7a3433f2.js');
require('./data-model-01c368bc.js');
require('./BaseField-381327a9.js');
require('./membership-e87e02ed.js');
require('./string-de672b69.js');
require('./UserSettings-2a2399b1.js');
require('./cookie-e64bc800.js');
require('./kimlik-api-663f4884.js');
require('./index-33e96540.js');
require('./FieldModelGenerator-d61f01d4.js');
require('./reference-field-1239b0c3.js');
require('./index-2eea69a0.js');
require('./FieldGenerator-279a646b.js');
require('./date-a24d7714.js');
require('./index-0783d14f.js');
require('./index-9d3d3d15.js');
require('./index-9fd84b93.js');
require('./CalendarConfig-998f742e.js');
require('./i18n-0bcf144d.js');
require('./i18n-77d45414.js');

function getAppConfig(mentornityCookieContext, organisationConfig, language, userRole, viewedUserRole) {
  const showMessageButton = viewedUserRole === 'mentor'
    ? [
      session.InfoVisibility.allMentors,
      session.InfoVisibility.everyone,
    ].includes(organisationConfig.conversationPermissions.mentee)
    : [
      session.InfoVisibility.allMentees,
      session.InfoVisibility.everyone,
    ].includes(organisationConfig.conversationPermissions.mentor);
  const actions$1 = [];
  if (userRole === 'mentee' && viewedUserRole === 'mentor') {
    // show follow buttons when mentee is viewing mentor detail
    actions$1.push({
      ui: {
        componentTag: 'mentornity-follow-button',
        name: 'toggle-follow',
      },
      data: {
        language,
        mentornityCookieContext,
      }
    });
  }
  if (showMessageButton) {
    actions$1.push({
      ui: {
        label: mentorintyBridgeApi.getEmbeddedTranslation(language, 'sendMessage'),
        name: 'send-message',
        view: 'flat'
      }
    });
  }
  return appConfig.AppConfig.decode({
    views: [
      detailViewConfig.DetailViewConfig.decode({
        _id: 'user-detail-page-summary-card-view',
        type: viewConfig.AppViewType.DetailViewCard,
        table: tableConfig.TableConfig.decode({
          fields: [
            new index.TextField('user.name', ''),
            new index.TextField('user.initials', ''),
            new attachment.AttachmentField('user.imageUrl', ''),
          ],
          roleName: viewedUserRole,
          name: `${viewedUserRole}-detail-view`,
        }),
        ui: {
          cardConfig: {
            header: {
              title: {
                fieldId: 'user.name',
                position: models.TitlePosition.RightOfImage,
              },
              image: {
                fieldId: 'user.imageUrl',
                placeholderFieldId: 'user.initials',
                position: models.ImagePosition.BottomLeft,
              },
              cover: {
                // placeholder: 'https://thumbs.dreamstime.com/z/books-wooden-desk-table-abstract-background-education-copy-space-back-to-school-184388515.jpg',
                size: 88,
              },
            },
            footer: {
              actions: actions$1,
            }
          }
        },
      }),
      getDetailViewUIConfig(mentornityCookieContext, language, organisationConfig, viewedUserRole),
      takvimAvailableTimePickerViewConfig.TakvimAvailableTimePickerViewConfig.decode({
        _id: 'user-detail-page-available-time-picker-view',
        i18nEntryId: 'user-detail-page-available-time-picker-view',
        type: viewConfig.AppViewType.TakvimAvailableTimePickerView,
        table: mentorintyBridgeApi.generateCalendarConfig(organisationConfig),
        events: {
          success: [
            new actions.InvalidationAction('user-detail-page-available-time-picker-view', 'user-detail-page-meeting-list-view'),
          ]
        },
        successNavigationAction: {
          type: 'navigation',
          direction: 'forward',
          name: 'open-meeting-detail-after-creation',
          params: {
            conversionConfig: {
              'item._id': 'meetingId',
            }
          }
        },
      }),
      takvimMeetingListViewConfig.TakvimMeetingListViewConfig.decode({
        _id: 'user-detail-page-meeting-list-view',
        i18nEntryId: 'user-detail-page-meeting-list-view',
        type: viewConfig.AppViewType.TakvimMeetingListViewTable,
        table: mentorintyBridgeApi.generateCalendarConfig(organisationConfig),
      }),
    ]
  });
}
function getDetailViewUIConfig(mentornityCookieContext, language, config, viewedUserRole) {
  if (!config) {
    return null;
  }
  const fields = commonWrapperUtil.getMembershipFields(language, config, viewedUserRole, mentornityCookieContext.role);
  const summaryCardFields = commonWrapperUtil.getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, true, 3);
  const detailCardFields = commonWrapperUtil.getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, false, 0);
  return detailViewConfig.DetailViewConfig.decode({
    _id: 'user-detail-page-info-view',
    i18nEntryId: 'user-detail-page-info-view',
    type: viewConfig.AppViewType.DetailViewCard,
    table: tableConfig.TableConfig.decode({
      fields,
      roleName: viewedUserRole,
      name: `${viewedUserRole}-detail-view`,
    }),
    ui: {
      summaryCardConfig: {
        body: {
          fields: summaryCardFields,
        }
      },
      cardConfig: {
        body: {
          fields: detailCardFields,
        }
      },
    },
  });
}
function getCompositeViewUIConfig(organisationConfig, language, userId, groupId, userRole, viewedUserRole) {
  const views = [
    {
      viewId: 'user-detail-page-summary-card-view',
    },
    {
      title: mentorintyBridgeApi.getEmbeddedTranslation(language, 'info').toLocaleUpperCase(),
      viewId: 'user-detail-page-info-view',
    }
  ];
  const mentorViewingMenteeProfileInOneToOneProgram = userRole === 'mentor' && viewedUserRole === 'mentee'
    && organisationConfig.menteeType !== 'group';
  // no need to check if it's one-to-one because mentees can view their group's meetings in mentor detail page
  const menteeViewingMentorProfile = userRole === 'mentee' && viewedUserRole === 'mentor';
  if (mentorViewingMenteeProfileInOneToOneProgram || menteeViewingMentorProfile) {
    views.push({
      title: mentorintyBridgeApi.getEmbeddedTranslation(language, 'getMeeting').toLocaleUpperCase(),
      viewId: 'user-detail-page-available-time-picker-view',
      params: {
        viewedUserMembershipConfig: session.generateMembershipConfigForRole(viewedUserRole),
        conversionConfig: {
          // viewedUserMembership will be provided by
          // the dvn-app-view-composite-view-linear with the name 'item'
          // we're setting the conversion config to convert the 'item' into 'viewedUserMembership'
          item: 'viewedUserMembership',
        }
      },
    }, {
      title: mentorintyBridgeApi.getEmbeddedTranslation(language, 'meetings').toLocaleUpperCase(),
      viewId: 'user-detail-page-meeting-list-view',
      params: {
        guest1Id: groupId || userId,
        conversionConfig: {
          // viewedUserMembership will be provided by
          // the dvn-app-view-composite-view-linear with the name 'item'
          // we're setting the conversion config to convert the 'item.user._id' into 'guest2Id'
          'item.user._id': 'guest2Id',
        }
      },
    });
  }
  return compositeViewLinearConfig.CompositeViewLinearConfig.decode({
    _id: 'mentornity-user-detail-page',
    table: {
      name: viewedUserRole === 'mentee' ? 'mentees' : 'mentors',
    },
    type: viewConfig.AppViewType.CompositeViewLinear,
    params: undefined,
    ui: {
      layout: 'vertical',
      views,
    }
  });
}

const mentornityUserDetailWrapperCss = "mentornity-user-detail-wrapper{display:block}";

const MentornityUserDetailWrapper = class {
  constructor(hostRef) {
    index$1.registerInstance(this, hostRef);
    this.weekStartDay = 'monday';
  }
  componentWillLoad() {
    const { isZoomAuthenticated, isGoogleAuthenticated, isMicrosoftBusinessAuthenticated, language, timeFormat, dateFormat, timeZone, weekStartDay, groupId, viewedUserRole, } = this;
    // set user's locale and time zone
    calendar.CalendarUtils.setTimeFormat(timeFormat);
    calendar.CalendarUtils.setDateFormat(dateFormat);
    calendar.CalendarUtils.setTimeZone(timeZone);
    switch (language) {
      case 'pt-br':
        calendar.CalendarUtils.setLocale(language, sozlukEntries.sozlukCalendarEntryPtBr, weekStartDay);
        break;
      case 'tr':
        calendar.CalendarUtils.setLocale(language, sozlukEntries.sozlukCalendarEntryTr, weekStartDay);
        break;
      default:
        calendar.CalendarUtils.setLocale(language, sozlukEntries.sozlukCalendarEntryEn, weekStartDay);
        break;
    }
    this.apiSozluk = new index$2.SozlukApi().setLanguage(language).setContents(sozlukEntries.sozlukEntries);
    const { context, headers } = session.getSessionDetails(this.groupId);
    const { userId, role } = context;
    this.user = session.getUser(userId);
    this.userSettings = session.getUserSettings(timeFormat, dateFormat, timeZone, language, isZoomAuthenticated, isGoogleAuthenticated, isMicrosoftBusinessAuthenticated);
    this.mentornityBridgeApi = new mentorintyBridgeApi.MentornityBridgeApi(session.mentornityApiEndpoint, context, headers);
    const mentornityApi = session.createMentornityApiInstance(session.mentornityApiEndpoint, headers);
    mentornityApi.get('users', context.userId)
      .then(user => this.user = User.User.decode(user));
    mentornityApi
      .getOrganisationConfig(context.organisationId)
      .then((config) => {
      this.organisationConfig = session.Config.decode(config);
      this.mentornityBridgeApi.organisationConfig = session.Config.decode(config);
      if (context.role === 'mentee' && this.organisationConfig.menteeType === 'group') {
        this.membership = session.generateMembershipForRole('group');
      }
      else {
        this.membership = session.generateMembershipForRole(context.role);
      }
      this.guestToPassToAvailableTimePickerAsCurrentUser = this.user;
      if (context.role === 'mentee' && config.menteeType === 'group') {
        // fetch the group of the mentee and set as the guest instead of the mentee
        if (context.groupId) {
          mentornityApi.getGroup(context.groupId).then(group => {
            if (group) {
              this.guestToPassToAvailableTimePickerAsCurrentUser = Object.assign(Object.assign({}, group), { name: group.title, initials: group.title[0] });
            }
          });
        }
        else {
          this.guestToPassToAvailableTimePickerAsCurrentUser = undefined;
        }
      }
      this.appConfig = getAppConfig(context, this.organisationConfig, language, role, viewedUserRole);
      this.compositeViewUIConfig = getCompositeViewUIConfig(this.organisationConfig, language, userId, groupId, role, viewedUserRole);
    });
  }
  onAction(event) {
    const action = event.detail;
    switch (action.name) {
      case 'send-message':
        // called when user clicks the "Send Message" button from the detail page
        window.dispatchEvent(new CustomEvent('dvn.mentornity-user-detail-wrapper.send-message', {
          detail: {
            user: action.params.item,
          }
        }));
        break;
      case 'open-meeting-detail-after-creation':
      case 'open-meeting-detail-from-meeting-list':
        window.dispatchEvent(new CustomEvent('dvn.mentornity-user-detail-wrapper.open-meeting-detail', {
          detail: {
            meetingId: action.params.meetingId,
          }
        }));
        break;
    }
  }
  render() {
    const { compositeViewUIConfig, appConfig, membership, mentornityBridgeApi, organisationConfig, userSettings, viewedUserId, user, } = this;
    if (!organisationConfig || !user) {
      return (index$1.h("div", { class: "d-flex justify-content-center pt-3" }, index$1.h("dvn-progress", { indefinite: true })));
    }
    return (index$1.h(index$1.Host, null, index$1.h("mentornity-root-wrapper", null, index$1.h("dvn-app-view-composite-view-linear", { apiSozluk: this.apiSozluk, apiData: mentornityBridgeApi, apiTakvim: mentornityBridgeApi, appConfig: appConfig, params: {
        itemId: viewedUserId,
      }, user: this.guestToPassToAvailableTimePickerAsCurrentUser, userSettings: userSettings, membership: membership, view: compositeViewUIConfig }))));
  }
};
MentornityUserDetailWrapper.style = mentornityUserDetailWrapperCss;

exports.mentornity_user_detail_wrapper = MentornityUserDetailWrapper;
