'use strict';

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

const index = require('./index-1ee80297.js');
const index$1 = require('./index-b2910095.js');
const i18n = require('./i18n-e6beaec7.js');

const mentornityPairMeetingSummaryWidgetCss = "mentornity-pair-meeting-summary-widget .user-card{margin-bottom:1rem}mentornity-pair-meeting-summary-widget .user-card:nth-last-child(-n+2){margin-bottom:0}mentornity-pair-meeting-summary-widget .header{white-space:nowrap;margin-bottom:calc(3 * var(--spacing-unit))}mentornity-pair-meeting-summary-widget .header dvn-image{float:left}mentornity-pair-meeting-summary-widget .header dvn-linear-progress,mentornity-pair-meeting-summary-widget .header h5,mentornity-pair-meeting-summary-widget .header p,mentornity-pair-meeting-summary-widget .header a{font-size:0.9rem;position:relative;top:var(--spacing-unit);left:calc(2 * var(--spacing-unit))}mentornity-pair-meeting-summary-widget .header p{padding:0;margin:0 0 0 50px;color:var(--color-text-secondary)}mentornity-pair-meeting-summary-widget .header a{margin:0;padding:0;color:#000}mentornity-pair-meeting-summary-widget .clickable:hover{text-decoration:underline;cursor:pointer;width:-webkit-fit-content;width:-moz-fit-content;width:fit-content}";

let MentornityPairsMeetingWidget = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.isPending = true;
  }
  async componentWillLoad() {
    this.strings = await this.apiSozluk.getContent(i18n.MentornityPairsMeetingWidgetI18nKey, i18n.mentornityPairsMeetingWidgetI18nDefault);
  }
  render() {
    const { activePairs, isPending, meetingGoalConfig, menteeType, organisationConfig, requestedPairs, strings, userType, } = this;
    if (isPending) {
      return (index.h(index.Host, null, index.h("b", { class: "pl-3", style: { fontSize: '1.1rem' } }, strings.myPairs), index.h("dvn-card", { elevation: 1 }, index.h("div", { class: "d-flex justify-content-center" }, index.h("dvn-progress", { indefinite: true, tint: "primary" })))));
    }
    // get pairs type
    let pairsType;
    if (userType === 'mentee') {
      pairsType = 'mentor';
    }
    else {
      pairsType = menteeType === 'group' ? 'group' : 'mentee';
    }
    const isMentorSelectionOn = organisationConfig.subscriptionConfig.mentorVisibilityStatus === 'showAvailableMentors';
    const hasPendingRequest = (requestedPairs === null || requestedPairs === void 0 ? void 0 : requestedPairs.length) > 0;
    const hasActivePairs = (activePairs === null || activePairs === void 0 ? void 0 : activePairs.length) > 0;
    const showCardFooter = hasActivePairs || isMentorSelectionOn;
    const showPendingPairRequestAlert = isMentorSelectionOn && hasPendingRequest;
    const showPairList = hasActivePairs;
    const messages = [];
    if (!hasActivePairs) {
      messages.push(strings.notMatchedMessage);
      if (userType === 'mentee') {
        if (isMentorSelectionOn) {
          messages.push(strings.pairsEmptyRequestOpenMessage);
        }
        else {
          messages.push(strings.pairsEmptyRequestOffMessage);
        }
      }
      else {
        if (isMentorSelectionOn) {
          if (hasPendingRequest) {
            messages.push(strings.pairsHasRequestEmptyMessage);
          }
          else {
            messages.push(strings.pairsHasNotRequestEmptyMessage);
          }
        }
        else {
          messages.push(strings.pairsEmptyRequestOffMessage);
        }
      }
    }
    const showMessages = messages.length > 0;
    const goToPairsActionName = userType === 'mentor' ? 'go-to-mentees-page' : 'go-to-mentors-page';
    const goToPairsPage = () => this.action.emit({ name: goToPairsActionName });
    const goToPairSelectionPage = () => this.action.emit({ name: 'go-to-mentor-selection' });
    const goToUserPairPage = (user) => {
      var _a;
      return this.action.emit({
        name: 'go-to-pair-detail-page',
        params: { uid: user._id, subId: (_a = user.subscription) === null || _a === void 0 ? void 0 : _a._id }
      });
    };
    return (index.h(index.Host, null, index.h("b", { class: "pl-3", style: { fontSize: '1.1rem' } }, strings.myPairs), index.h("dvn-card", { elevation: 1 }, showPendingPairRequestAlert &&
      index.h("dvn-alert", { message: index$1.SozlukApi.build(strings.waitingForResponse, { count: requestedPairs.length }) }), showPairList &&
      index.h("div", { class: "row" }, activePairs.map(generatePairView(strings, pairsType, meetingGoalConfig, goToUserPairPage))), showMessages &&
      index.h("div", { class: "col-md-12 text-centered" }, messages.length > 0 && messages.join(' ')), showCardFooter &&
      index.h("dvn-card-footer", { slot: "footer", alignRight: true }, hasActivePairs &&
        index.h("dvn-button", { leaned: true, view: "flat", onClick: () => goToPairsPage() }, strings.seeAll), isMentorSelectionOn &&
        index.h("dvn-button", { leaned: true, view: "flat", onClick: () => {
            if (userType === 'mentee') {
              goToPairSelectionPage();
            }
            else {
              goToPairsPage();
            }
          } }, strings.seeRequests)))));
  }
};
const generatePairView = (strings, pairsType, meetingGoalConfig, goToUserPairPage) => (pair, i) => {
  if (i >= 6) {
    return;
  }
  const { user, meetings } = pair;
  const shortName = pairsType === 'group'
    ? user.title[0]
    : user.firstName[0] + user.lastName[0];
  const hasMeetings = meetings.length > 0;
  return (index.h("div", { class: "col-lg-6 col-md-12 user-card" }, index.h("div", { class: "header" }, index.h("dvn-image", { slot: "media", src: user.imageUrl, text: shortName, leaned: true, shape: "circle", size: 50 }), index.h("b", { class: "clickable pl-2", onClick: () => goToUserPairPage(user) }, pairsType === 'group' ? user.title : user.firstName + ' ' + user.lastName), index.h("dvn-linear-progress", { showPercent: hasMeetings, percent: calculateMeetingsPercent(meetings, hasMeetings, meetingGoalConfig), scale: "small" }), index.h("p", { class: "clickable", onClick: () => goToUserPairPage(user) }, hasMeetings ? `${meetings.length} ${strings.meetings}` : strings.scheduleMeeting))));
};
const calculateMeetingsPercent = (meetings, hasMeetings, meetingGoalConfig) => {
  if (!hasMeetings) {
    return '0';
  }
  const totalMeetings = meetingGoalConfig.meetingGoals.length;
  let completedMeetings = 0;
  const todayDate = new Date();
  meetings.forEach(meeting => {
    const endDate = new Date(meeting.endDate * 1000);
    if (endDate < todayDate) {
      completedMeetings++;
    }
  });
  const result = Math.floor((completedMeetings / totalMeetings) * 100).toString();
  return result;
};
MentornityPairsMeetingWidget.style = mentornityPairMeetingSummaryWidgetCss;

exports.mentornity_pair_meeting_summary_widget = MentornityPairsMeetingWidget;
