'use strict';

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

const index = require('./index-1ee80297.js');
const dataModel = require('./data-model-c51de090.js');
const index$1 = require('./index-b2910095.js');
require('./kimlikApi-1cd43caf.js');
require('./data-transfer-field-0ce05c1a.js');
require('./filter-d5040f65.js');
const instance = require('./instance-8904b0e4.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-ede51458.js');
const env = require('./env-56a21d68.js');
const session = require('./session-612c0d2f.js');
const wrapperUtil = require('./wrapper-util-3bb6f62f.js');
const sozlukEntries = require('./sozluk-entries-34e8486b.js');
require('./BaseField-d019ade6.js');
require('./FieldModelGenerator-2c39c8e9.js');
require('./index-42baafa9.js');
require('./index-a77c3004.js');
require('./date-25b104f6.js');
require('./index-521d07b9.js');
require('./index-9e7c4ca0.js');
require('./index-30008542.js');
require('./attachment-3090e25e.js');
require('./membership-14c44e7c.js');
require('./membership-config-6ba394cc.js');
require('./table-config-be88299b.js');
require('./string-de672b69.js');
require('./User-286a0714.js');
require('./CalendarConfig-fed039db.js');
require('./UserSettings-016122e8.js');
require('./cookie-c4d9848a.js');
require('./_commonjsHelpers-206db00d.js');
require('./index-33e96540.js');
require('./detail-view-config-c107e694.js');
require('./view-config-c409ff28.js');
require('./atama-member-pairs-overview-view-config-1507cd0c.js');
require('./pair-config-7be52db7.js');
require('./pair-config-aa66e586.js');
require('./common-wrapper-util-5385d126.js');
require('./i18n-e6beaec7.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) {
    index.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 } = session.getSessionDetails(this.groupId);
    const { organisationDomain, organisationId, role, userId } = context;
    this.apiSozluk = new index$1.SozlukApi().setLanguage(this.language).setContents(sozlukEntries.sozlukEntries);
    const mentornityApi = instance.createMentornityApiInstance(env.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 mentorintyBridgeApi.MentornityBridgeApi(env.mentornityApiEndpoint, context, headers);
    await instance.createMentornityApiInstance(env.mentornityApiEndpoint, headers)
      .getOrganisationConfig(context.organisationId)
      .then((response) => {
      this.organisationConfig = instance.Config.decode(response);
      this.mentornityBridgeApi.organisationConfig = instance.Config.decode(response);
    });
    const getViewConfigsResponse = await mentornityApi.getViewConfigs(context.organisationId)
      .catch(error => this.error = error);
    if (!getViewConfigsResponse)
      return;
    const viewConfigs = getViewConfigsResponse.results;
    const view = wrapperUtil.getPairOverviewViewConfig(context, this.language, this.organisationConfig, viewConfigs);
    const membership = dataModel.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 (index.h(index.Host, null, index.h("mentornity-root-wrapper", { class: "d-block pt-4", theme: { cornerRadius: '5px' } }, showPairMeetingSummaryWidget &&
      index.h("mentornity-pair-meeting-summary-widget", { apiSozluk: apiSozluk, isPending: isPending, activePairs: activePairs, organisationConfig: organisationConfig, meetingGoalConfig: meetingGoalConfig, requestedPairs: requestedPairs, userType: userType, menteeType: menteeType }))));
  }
};

exports.mentornity_member_home_widgets_wrapper = MentornityMemberHomeWidgetsWrapper;
