import { r as registerInstance, h, H as Host } from './index-102043e0.js';
import { D as DataModel } from './data-model-f5160369.js';
import { S as SozlukApi } from './index-1e9f4c53.js';
import './kimlikApi-ac700c81.js';
import './data-transfer-field-918730a3.js';
import './filter-cbaa3d7d.js';
import { c as createMentornityApiInstance, b as Config } from './instance-fe40f49f.js';
import { M as MentornityBridgeApi } from './mentorinty-bridge-api-fc8ad2d8.js';
import { m as mentornityApiEndpoint } from './env-4ec3c807.js';
import { g as getSessionDetails } from './session-4127c2a1.js';
import { g as getPairOverviewViewConfig } from './wrapper-util-444380d8.js';
import { s as sozlukEntries } from './sozluk-entries-e1fd9179.js';
import './BaseField-461b6d0c.js';
import './FieldModelGenerator-ee438a53.js';
import './index-ef734a5e.js';
import './index-336ef773.js';
import './date-ce777316.js';
import './index-0e8b9ea2.js';
import './index-c3eacf05.js';
import './index-a1675f50.js';
import './attachment-32decf79.js';
import './membership-821de3c5.js';
import './membership-config-68ed7772.js';
import './table-config-720f8ea0.js';
import './string-3065a411.js';
import './User-d2ee8b71.js';
import './CalendarConfig-cc1e3a80.js';
import './UserSettings-7b80b841.js';
import './cookie-647f9dc7.js';
import './_commonjsHelpers-27f122dd.js';
import './index-d3bd30bd.js';
import './detail-view-config-e2344d5e.js';
import './view-config-efdeed6d.js';
import './atama-member-pairs-overview-view-config-7a52c297.js';
import './pair-config-c376904c.js';
import './pair-config-01882539.js';
import './common-wrapper-util-f1abefff.js';
import './i18n-2b616f25.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',
};
let MentornityMemberHomeWidgetsWrapper = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.isPending = true;
  }
  componentWillLoad() {
    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;
    }
  }
  async loadData() {
    const { context, headers } = getSessionDetails(this.groupId);
    const { organisationDomain, organisationId, role, userId } = context;
    this.apiSozluk = new SozlukApi().setLanguage(this.language).setContents(sozlukEntries);
    const mentornityApi = createMentornityApiInstance(mentornityApiEndpoint, headers);
    this.meetingGoalConfig = await mentornityApi.getOrganisationMeetingGoalConfig(organisationDomain)
      .catch(error => this.error = error);
    const memberRolePath = role === 'mentor' ? 'mentors' : 'mentees';
    const meetings = await mentornityApi.getMeetings(organisationId, memberRolePath, userId, {
      status: 'active',
    }).catch(error => this.error = error);
    this.mentornityBridgeApi = new MentornityBridgeApi(mentornityApiEndpoint, context, headers);
    await createMentornityApiInstance(mentornityApiEndpoint, headers)
      .getOrganisationConfig(context.organisationId)
      .then((response) => {
      this.organisationConfig = Config.decode(response);
      this.mentornityBridgeApi.organisationConfig = Config.decode(response);
    });
    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 activePairs = [];
    const requestedPairs = [];
    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.results.filter(meeting => meeting.guests.some(guest => guest._id === pair[pairsType][0]._id))
        });
      }
      else if ((pair === null || pair === void 0 ? void 0 : pair.status) === 'requested') {
        requestedPairs.push({
          user: pair[pairsType][0],
          meetings: meetings.results.filter(meeting => meeting.guests.some(guest => guest._id === pair[pairsType][0]._id))
        });
      }
    });
    this.activePairs = activePairs;
    this.requestedPairs = requestedPairs;
    this.organisationConfig = await mentornityApi.getOrganisationConfig(organisationId);
    this.isPending = false;
  }
  render() {
    const { activePairs, apiSozluk, isPending, menteeType, meetingGoalConfig, organisationConfig, requestedPairs, userType, } = this;
    const showPairMeetingSummaryWidget = meetingGoalConfig && meetingGoalConfig.meetingGoals.length > 0;
    if (!showPairMeetingSummaryWidget) {
      return null;
    }
    return (h(Host, null, h("mentornity-root-wrapper", { class: "d-block pt-4", theme: { cornerRadius: '5px' } }, showPairMeetingSummaryWidget &&
      h("mentornity-pair-meeting-summary-widget", { apiSozluk: apiSozluk, isPending: isPending, activePairs: activePairs, organisationConfig: organisationConfig, meetingGoalConfig: meetingGoalConfig, requestedPairs: requestedPairs, userType: userType, menteeType: menteeType }))));
  }
};

export { MentornityMemberHomeWidgetsWrapper as mentornity_member_home_widgets_wrapper };
