import { r as registerInstance, h, H as Host } from './index-ecdfb761.js';
import { D as DataModel } from './data-model-88af49fc.js';
import { C as CalendarUtils } from './calendar-470c802a.js';
import { S as SozlukApi } from './index-3bf9b308.js';
import './apiBase-6015f41f.js';
import './data-transfer-field-2f305919.js';
import './filter-cbaa3d7d.js';
import { g as getSessionDetails, m as mentornityApiEndpoint, c as createMentornityApiInstance, e as Config } from './session-2a491341.js';
import { M as MentornityBridgeApi } from './mentorinty-bridge-api-5ca2d652.js';
import { g as getPairOverviewViewConfig } from './wrapper-util-9b6b4574.js';
import { a as sozlukCalendarEntryEn, b as sozlukCalendarEntryTr, c as sozlukCalendarEntryPtBr, s as sozlukEntries } from './sozluk-entries-1e8f5234.js';
import './index-a3ca021c.js';
import './_commonjsHelpers-27f122dd.js';
import './moment-65eca6b3.js';
import './BaseField-0b01dc2c.js';
import './membership-b6639471.js';
import './table-config-d73a72ab.js';
import './FieldModelGenerator-378c8613.js';
import './reference-field-a5b414d1.js';
import './index-61dcd269.js';
import './FieldGenerator-3ae403b2.js';
import './date-1bc489bd.js';
import './index-4e451db4.js';
import './index-23baeeef.js';
import './index-09efdc01.js';
import './index-150b6a59.js';
import './string-3065a411.js';
import './User-72c4643a.js';
import './UserSettings-7e4170a7.js';
import './cookie-5d9f5819.js';
import './kimlik-api-fddeac81.js';
import './index-d3bd30bd.js';
import './index-e66e8eca.js';
import './attachment-961dd228.js';
import './CalendarConfig-412d1b0f.js';
import './detail-view-config-23af2b0d.js';
import './view-config-1ed8c59f.js';
import './atama-member-pairs-overview-view-config-ae8ec43d.js';
import './pair-config-a31d66bf.js';
import './pair-config-13b5a7c1.js';
import './common-wrapper-util-e4afa19b.js';
import './i18n-09a07b97.js';
import './i18n-0e757ca5.js';

/**
 * Mentornity web-client has the same event name mapping to consume the events with the same name.
 */
const ViewEvents = {
  openProfilePage: 'dvn.mentornity-member-home-widgets-wrapper.open-profile-page',
  openMeetingDetail: 'dvn.mentornity-member-home-widgets-wrapper.open-meeting-page',
  openMenteesPage: 'dvn.mentornity-member-home-widgets-wrapper.open-mentees-page',
  openMentorsPage: 'dvn.mentornity-member-home-widgets-wrapper.open-mentors-page',
  openUserPairPage: 'dvn.mentornity-member-home-widgets-wrapper.open-user-pair-page',
  openMentorSelectionPage: 'dvn.mentornity-member-home-widgets-wrapper.open-mentor-selection-page',
};
const MentornityMemberHomeWidgetsWrapper = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.isPairsDataPending = true;
    this.isMeetingPending = true;
    this.isAnnouncementsPending = true;
    this.isOrganisationDataPending = true;
  }
  componentWillLoad() {
    const { timeFormat, timeZone, language } = this;
    switch (language) {
      case 'pt-br':
        CalendarUtils.setLocale(language, sozlukCalendarEntryPtBr);
        break;
      case 'tr':
        CalendarUtils.setLocale(language, sozlukCalendarEntryTr);
        break;
      default:
        CalendarUtils.setLocale(language, sozlukCalendarEntryEn);
        break;
    }
    // set user's locale and time zone
    CalendarUtils.setTimeFormat(timeFormat);
    CalendarUtils.setTimeZone(timeZone);
    this.loadData();
  }
  onAction(event) {
    const action = event.detail;
    switch (action.name) {
      case 'go-to-mentees-page':
        // dispatch event for Mentornity web-client to handle the action
        window.dispatchEvent(new CustomEvent(ViewEvents.openMenteesPage));
        break;
      case 'go-to-mentors-page':
        // dispatch event for Mentornity web-client to handle the action
        window.dispatchEvent(new CustomEvent(ViewEvents.openMentorsPage));
        break;
      case 'go-to-pair-detail-page':
        // dispatch event for Mentornity web-client to handle the action
        window.dispatchEvent(new CustomEvent(ViewEvents.openUserPairPage, {
          detail: {
            userId: action.params.uid,
            subId: action.params.subId,
          }
        }));
        break;
      case 'go-to-mentor-selection':
        // dispatch event for Mentornity web-client to handle the action
        window.dispatchEvent(new CustomEvent(ViewEvents.openMentorSelectionPage));
        break;
      case 'go-to-meeting-page':
        // dispatch event for Mentornity web-client to handle the action
        window.dispatchEvent(new CustomEvent(ViewEvents.openMeetingDetail, {
          detail: {
            meetingId: action.params.meetingId,
          }
        }));
        break;
    }
  }
  async fetchPairs(meetings, mentornityApi, context) {
    const getViewConfigsResponse = await mentornityApi.getViewConfigs(context.organisationId)
      .catch(error => this.error = error);
    if (!getViewConfigsResponse)
      return;
    const viewConfigs = getViewConfigsResponse.results;
    const view = getPairOverviewViewConfig(context, this.language, this.organisationConfig, viewConfigs);
    const membership = DataModel.decode({});
    const userRefFieldId = context.role === 'mentor' ? 'subscribed' : 'subscriber';
    const getPairsResponse = await this.mentornityBridgeApi.getPairsOfUser(view.table.base, view.table.name, userRefFieldId, membership._id)
      .catch(error => this.error = error);
    const requestedList = await this.mentornityBridgeApi.getReceivedRequests(view.table.base, view.table.name)
      .catch(error => this.error = error);
    const activePairs = [];
    const requestedPairs = requestedList === null || requestedList === void 0 ? void 0 : requestedList.results;
    const pairsType = context.role === 'mentor' ? 'subscriber' : 'subscribed';
    getPairsResponse.results.forEach(pair => {
      if ((pair === null || pair === void 0 ? void 0 : pair.status) === 'active') {
        activePairs.push({
          user: pair[pairsType][0],
          meetings: meetings.filter(meeting => meeting.guests.some(guest => (meeting.status === 'active' && guest._id === pair[pairsType][0]._id)))
        });
      }
    });
    this.activePairs = activePairs;
    this.requestedPairs = requestedPairs;
    this.isPairsDataPending = false;
  }
  async fetchPairsMeetings(context, mentornityApi) {
    var _a;
    const { organisationDomain, organisationId, userId, groupId } = context;
    const hasMeetingGoal = this.meetingGoalConfig && ((_a = this.meetingGoalConfig.meetingGoals) === null || _a === void 0 ? void 0 : _a.length) > 0;
    let meetings;
    if (hasMeetingGoal) {
      meetings = await mentornityApi.getMeetingsInMeetingGoal(organisationDomain, { status: 'active' })
        .catch(error => this.error = error);
      meetings = meetings === null || meetings === void 0 ? void 0 : meetings.results;
    }
    else {
      if (this.userType === 'mentee' && this.menteeType === 'group') {
        meetings = await mentornityApi.getGroupsMeetingsChronologically(groupId, 0)
          .catch(error => this.error = error);
      }
      else {
        meetings = await mentornityApi.getUsersMeetingsChronologically(userId, organisationId, 0)
          .catch(error => this.error = error);
      }
    }
    return meetings;
  }
  async fetchPastMeeings(context) {
    let promise;
    if (context.groupId) {
      promise = this.api.getGroupsMeetingsChronologically(context.groupId, 0);
    }
    else {
      promise = this.api.getUsersMeetingsChronologically(this.context.userId, this.context.organisationId, 0);
    }
    let meetings = await promise.catch(error => this.error = error);
    const pastMeetings = [];
    let unfilledFormCount = 0;
    const today = new Date();
    (meetings || []).forEach((meeting) => {
      const startDate = new Date(meeting.startDate * 1000);
      if (startDate < today && meeting.status !== 'canceled') {
        if (!(meeting === null || meeting === void 0 ? void 0 : meeting.feedback))
          unfilledFormCount++;
        pastMeetings.unshift(meeting);
      }
    });
    return {
      meetings: pastMeetings,
      unfilledFormCount
    };
  }
  async fetchUserMeetings(context, mentornityApi) {
    var _a;
    let promise;
    if (context.groupId) {
      promise = mentornityApi.getGroupsMeetingsChronologically(context.groupId, 0);
    }
    else {
      promise = mentornityApi.getUsersMeetingsChronologically(context.userId, context.organisationId, 0);
    }
    let meetings = await promise.catch(error => this.error = error);
    const hasMeetingGoal = this.meetingGoalConfig && ((_a = this.meetingGoalConfig.meetingGoals) === null || _a === void 0 ? void 0 : _a.length) > 0;
    const upcomingMeetings = [];
    const pastMeetings = [];
    let unfilledFormCount = 0;
    let unfilledSessionFormCount = 0;
    const today = new Date();
    (meetings || []).forEach((meeting) => {
      var _a, _b, _c, _d, _e, _f, _g;
      const startDate = new Date(meeting.startDate * 1000);
      if (startDate >= today && meeting.status !== 'canceled') {
        upcomingMeetings.unshift(meeting);
      }
      if (startDate < today && meeting.status !== 'canceled') {
        if (!(meeting === null || meeting === void 0 ? void 0 : meeting.feedback))
          unfilledFormCount++;
        // calculate unfilled session forms
        if (hasMeetingGoal) {
          let sessionHasQuestions = false;
          const ownSession = hasMeetingGoal && ((_a = this.meetingGoalConfig) === null || _a === void 0 ? void 0 : _a.meetingGoals.find(session => session.identifier === meeting.meetingGoalIdentifier));
          const session = ownSession === null || ownSession === void 0 ? void 0 : ownSession.specifications[this.userType];
          sessionHasQuestions = ((_c = (_b = session === null || session === void 0 ? void 0 : session.postMeetingForm) === null || _b === void 0 ? void 0 : _b.fields) === null || _c === void 0 ? void 0 : _c.length) > 0 || ((_e = (_d = session === null || session === void 0 ? void 0 : session.preMeetingForm) === null || _d === void 0 ? void 0 : _d.fields) === null || _e === void 0 ? void 0 : _e.length) > 0;
          const hasFeedback = !!((_f = meeting === null || meeting === void 0 ? void 0 : meeting.feedback) === null || _f === void 0 ? void 0 : _f._id) || ((_g = meeting === null || meeting === void 0 ? void 0 : meeting.feedback) === null || _g === void 0 ? void 0 : _g.length) > 0;
          if (sessionHasQuestions && !hasFeedback)
            unfilledSessionFormCount++;
        }
        pastMeetings.unshift(meeting);
      }
    });
    this.upcomingMeetings = upcomingMeetings;
    this.pastMeetings = pastMeetings;
    this.unfilledSessionFormCount = unfilledSessionFormCount;
    this.unfilledFormCount = unfilledFormCount;
    this.isMeetingPending = false;
  }
  async fetchAnnouncements(mentornityApi) {
    const { organisationId, role } = this.context;
    // Fetch announcements from api
    this.announcements = await mentornityApi.getAnnouncements(organisationId, role);
    this.isAnnouncementsPending = false;
  }
  async fetchOrganisation(mentornityApi) {
    this.organisation = await mentornityApi.getOrganisation(this.organisationConfig.organisation._id);
    this.isOrganisationDataPending = false;
  }
  async loadData() {
    const { context, headers } = getSessionDetails(this.groupId);
    const { organisationId } = context;
    const showPairsWidget = this.mentorMenteeSubscription === 'restricted';
    // Setup configuration
    this.apiSozluk = new SozlukApi().setLanguage(this.language).setContents(sozlukEntries);
    this.mentornityBridgeApi = new MentornityBridgeApi(mentornityApiEndpoint, context, headers);
    const mentornityApi = createMentornityApiInstance(mentornityApiEndpoint, headers);
    mentornityApi.getOrganisationConfig(organisationId)
      .then((response) => {
      this.organisationConfig = Config.decode(response);
      this.mentornityBridgeApi.organisationConfig = Config.decode(response);
    });
    this.api = mentornityApi;
    this.context = context;
    this.sessionData = await mentornityApi.getSessionData(context.organisationDomain);
    this.meetingGoalConfig = await mentornityApi.getOrganisationMeetingGoalConfig(context.organisationDomain)
      .catch(error => this.error = error);
    // Fetch data from api
    const meetings = await this.fetchPairsMeetings(context, mentornityApi);
    if (showPairsWidget) {
      this.fetchPairs(meetings, mentornityApi, context);
    }
    this.fetchUserMeetings(context, mentornityApi);
    this.fetchOrganisation(mentornityApi);
    this.fetchAnnouncements(mentornityApi);
  }
  render() {
    const { activePairs, apiSozluk, isPairsDataPending, menteeType, meetingGoalConfig, organisationConfig, requestedPairs, userType, mentorMenteeSubscription, upcomingMeetings, pastMeetings, unfilledFormCount, unfilledSessionFormCount, isMeetingPending, sessionData, groupId, context, api, announcements, isAnnouncementsPending, isOrganisationDataPending, organisation } = this;
    const showPairsWidget = mentorMenteeSubscription === 'restricted';
    const feedbackConfig = (organisationConfig && organisationConfig.feedback) && (organisationConfig === null || organisationConfig === void 0 ? void 0 : organisationConfig.feedback[userType]);
    return (h(Host, null, h("mentornity-root-wrapper", { class: "d-block pt-4", theme: { cornerRadius: '15px' } }, h("div", { class: "row" }, h("div", { class: "col-md-8 order-2 order-md-1" }, showPairsWidget &&
      h("mentornity-pair-meeting-summary-widget", { apiSozluk: apiSozluk, isPending: isPairsDataPending, activePairs: activePairs, organisationConfig: organisationConfig, meetingGoalConfig: meetingGoalConfig, requestedPairs: requestedPairs, userType: userType, menteeType: menteeType }), h("mentornity-upcoming-meetings-widget", { meetings: upcomingMeetings, meetingGoalConfig: this.meetingGoalConfig, account: sessionData === null || sessionData === void 0 ? void 0 : sessionData.account, groupId: groupId, isPending: isMeetingPending, timeFormat: this.timeFormat, apiSozluk: apiSozluk }), h("mentornity-past-meetings-widget", { meetings: pastMeetings, meetingGoalConfig: this.meetingGoalConfig, fetchPastMeetings: this.fetchPastMeeings, context: context, userType: userType, unfilledFormCount: unfilledFormCount, unfilledSessionFormCount: unfilledSessionFormCount, account: sessionData === null || sessionData === void 0 ? void 0 : sessionData.account, groupId: groupId, isPending: isMeetingPending, timeFormat: this.timeFormat, apiSozluk: apiSozluk, feedbackConfig: feedbackConfig, api: api })), h("div", { class: "col-md-4 order-1 order-md-2" }, h("mentornity-announcements-widget", { announcements: announcements, isPending: isAnnouncementsPending, apiSozluk: apiSozluk }), h("mentornity-program-info-widget", { organisation: organisation, organisationConfig: organisationConfig, isPending: isOrganisationDataPending, apiSozluk: apiSozluk }))))));
  }
};

export { MentornityMemberHomeWidgetsWrapper as mentornity_member_home_widgets_wrapper };
