'use strict';

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

const index = require('./index-ce58ddc5.js');
const index$1 = require('./index-b2910095.js');
const i18n = require('./i18n-1b762664.js');
const calendar = require('./calendar-12e78fe7.js');
require('./index-1f95aafc.js');
require('./_commonjsHelpers-206db00d.js');
require('./moment-d48a0164.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) => {
  var _a;
  if (i >= 6) {
    return;
  }
  const { user, meetings } = pair;
  const hasMeetings = meetings.length > 0;
  const hasMeetingGoal = meetingGoalConfig && ((_a = meetingGoalConfig.meetingGoals) === null || _a === void 0 ? void 0 : _a.length) > 0;
  const shortName = pairsType === 'group'
    ? user.title[0]
    : user.firstName[0] + user.lastName[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), hasMeetingGoal &&
    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) => {
  var _a;
  if (!hasMeetings) {
    return '0';
  }
  const totalMeetings = (_a = meetingGoalConfig.meetingGoals) === null || _a === void 0 ? void 0 : _a.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;

const mentornityUpcomingMeetingsWidgetCss = "mentornity-upcoming-meetings-widget .meeting-date{font-weight:bold;color:var(--color-text);max-width:6ch;max-height:80px;word-wrap:break-word;border:var(--color-text) 2px solid;border-radius:calc(2 * var(--corner-radius));padding:calc(2 * var(--spacing-unit));text-align:center;cursor:pointer}mentornity-upcoming-meetings-widget .full-name{color:var(--color-text);font-weight:bold}mentornity-upcoming-meetings-widget .meeting-data{font-size:var(--font-size-medium);height:-webkit-fit-content;height:-moz-fit-content;height:fit-content;-ms-flex-align:center;align-items:center}mentornity-upcoming-meetings-widget .break-line{height:1px;color:var(--color-background-secondary)}mentornity-upcoming-meetings-widget .video-icon i::before{margin:0}mentornity-upcoming-meetings-widget .clickable{cursor:pointer}mentornity-upcoming-meetings-widget .custom-address{color:var(--color-text-title)}mentornity-upcoming-meetings-widget .meeting-time{font-size:var(--font-size-medium)}mentornity-upcoming-meetings-widget .meeting-goal-title{margin-bottom:calc(4 * var(--spacing-unit))}mentornity-upcoming-meetings-widget .meeting-goal-title h3{margin-top:0 !important}mentornity-upcoming-meetings-widget .empty-meeting-text{padding:calc(2 * var(--spacing-unit));font-size:var(--font-size-medium);font-weight:bold;font-family:\"Nunito\";color:var(--color-text)}mentornity-upcoming-meetings-widget p{margin:0}";

let MentornityUpcomingmeetingsWidget = class {
  constructor(hostRef) {
    index.registerInstance(this, hostRef);
    this.action = index.createEvent(this, "action", 7);
    this.toggleSeeAllButton = false;
    this.isPending = true;
    this.goToMeetingDetails = (meeting) => this.action.emit({
      name: 'go-to-meeting-page',
      params: { meetingId: meeting._id }
    });
  }
  async componentWillLoad() {
    this.strings = await this.apiSozluk.getContent(i18n.MentornityUpcomingMeetingWidgetI18nKey, i18n.mentornityUpcomingMeetingWidgetI18nDefault);
  }
  render() {
    const { meetings, timeFormat, strings, isPending, meetingGoalConfig, userType } = this;
    const hasMeetings = (meetings === null || meetings === void 0 ? void 0 : meetings.length) > 0;
    if (isPending) {
      return (index.h(index.Host, null, index.h("b", { class: "pl-3", style: { fontSize: '1.1rem' } }, strings.upcomingMeetings), index.h("dvn-card", { elevation: 1 }, index.h("div", { class: "d-flex justify-content-center" }, index.h("dvn-progress", { indefinite: true, tint: "primary" })))));
    }
    return (index.h(index.Host, null, index.h("b", { class: "pl-3", style: { fontSize: '1.1rem' } }, strings.upcomingMeetings), index.h("dvn-card", { elevation: 1 }, meetings &&
      generateUpcomingmeetingView(meetings, timeFormat, meetingGoalConfig, meetings === null || meetings === void 0 ? void 0 : meetings.length, userType, this.toggleSeeAllButton, strings, this.goToMeetingDetails), (meetings === null || meetings === void 0 ? void 0 : meetings.length) > 3 && !this.toggleSeeAllButton &&
      index.h("dvn-card-footer", { class: "mt-3", slot: "footer", alignRight: true }, index.h("dvn-button", { leaned: true, view: "flat", onClick: () => {
          this.toggleSeeAllButton = true;
        } }, strings.seeAll, "(", meetings.length, ")")), !hasMeetings &&
      index.h("div", { class: "col-md-12 empty-meeting-text" }, index.h("p", null, " ", strings.listMeetingsHere, " "), index.h("p", null, " ", strings.emptyMeetings, " ")))));
  }
};
const generateUpcomingmeetingView = (meetings, timeFormat, meetingGoalConfig, meetingsLength, userType, expandMeetings, strings, goToMeetingDetails) => {
  var _a, _b;
  const hasMeetingGoal = meetingGoalConfig && ((_a = meetingGoalConfig.meetingGoals) === null || _a === void 0 ? void 0 : _a.length) > 0;
  if (hasMeetingGoal) {
    // to recognize how many meetings has been rendered, and stop it in 3th index
    let renderedMeeings = 0;
    // goalMeetings help us to put meetings to the own session
    let goalMeetings = {};
    // define each session to an empty array
    (_b = meetingGoalConfig === null || meetingGoalConfig === void 0 ? void 0 : meetingGoalConfig.meetingGoals) === null || _b === void 0 ? void 0 : _b.forEach(goal => {
      goalMeetings[goal.identifier] = [];
    });
    // push each meeting to own session
    for (const meeting of meetings) {
      goalMeetings[meeting.meetingGoalIdentifier].push(meeting);
    }
    // filter nearest meetings
    for (const meeting of meetings) {
      goalMeetings[meeting.meetingGoalIdentifier] = goalMeetings[meeting.meetingGoalIdentifier].sort((a, b) => {
        const aDate = Number(calendar.CalendarUtils.format(new Date(a.startDate * 1000), 'DD'));
        const bDate = Number(calendar.CalendarUtils.format(new Date(b.startDate * 1000), 'DD'));
        return aDate - bDate;
      });
    }
    return index.h("div", null, Object.keys(goalMeetings).map((key, i) => {
      if (goalMeetings[key].length === 0)
        return;
      if (renderedMeeings >= 3)
        return;
      return (index.h("div", null, index.h("div", { class: "meeting-goal-title" }, index.h("h3", null, " ", meetingGoalConfig === null || meetingGoalConfig === void 0 ? void 0 :
        meetingGoalConfig.meetingGoals[i].meetingTitle, " ")), goalMeetings[key].map(meeting => {
        if (!expandMeetings) {
          // render only first 3 meetings
          if (renderedMeeings >= 3)
            return;
          renderedMeeings++;
        }
        return meetingDetails(meeting, userType, timeFormat, meetingsLength, strings, i, goToMeetingDetails);
      })));
    }));
  }
  else {
    const generatedMeetings = generateMeetings(meetings, expandMeetings);
    return (index.h("div", null, generatedMeetings.map((meeting, i) => meetingDetails(meeting, userType, timeFormat, meetingsLength, strings, i, goToMeetingDetails))));
  }
};
const meetingDetails = (meeting, userType, timeFormat, meetingsLength, strings, dataIndex, goToMeetingDetails) => {
  var _a, _b, _c;
  const meetingDate = calendar.CalendarUtils.format(new Date(meeting.startDate * 1000), 'MMM DD');
  const startDate = calendar.CalendarUtils.format(new Date(meeting.startDate * 1000), `ddd HH:mm ${timeFormat === '12-hour' ? 'A' : ''}`);
  const endDate = calendar.CalendarUtils.format(new Date(meeting.endDate * 1000), `HH:mm ${timeFormat === '12-hour' ? 'A' : ''}`);
  let fullName = '';
  if (userType === 'mentor') {
    if (((_a = meeting.guests) === null || _a === void 0 ? void 0 : _a.length) > 2) {
      for (let i = 1; i < ((_b = meeting.guests) === null || _b === void 0 ? void 0 : _b.length); i++) {
        fullName += meeting.guests[i].title
          ? meeting.guests[i].title
          : meeting.guests[i].firstName + ' ' + meeting.guests[i].lastName;
        fullName = ((_c = meeting.guests) === null || _c === void 0 ? void 0 : _c.length) - 1 !== i
          ? fullName.concat(', ')
          : fullName;
      }
      fullName = truncate(fullName, 40);
    }
    else {
      fullName = meeting.guests[1].title
        ? meeting.guests[1].title
        : meeting.guests[1].firstName + ' ' + meeting.guests[1].lastName;
    }
  }
  else {
    fullName = meeting.guests[0].title
      ? meeting.guests[0].title
      : meeting.guests[0].firstName + ' ' + meeting.guests[0].lastName;
  }
  const meetingTime = startDate + ' - ' + endDate;
  const displayBreakLine = (meetingsLength - 1) > dataIndex;
  const meetingLocation = `${meeting.location.type.charAt(0).toUpperCase() + meeting.location.type.slice(1)} ${strings.meet}`;
  return (index.h("div", null, index.h("div", { class: "d-flex justify-content-between align-items-center" }, index.h("div", { class: "d-flex meeting-data" }, index.h("p", { class: "meeting-date", onClick: () => goToMeetingDetails(meeting) }, " ", meetingDate, " "), index.h("div", { class: "flex-column ml-3" }, index.h("div", { class: "full-name" }, index.h("p", null, " ", fullName, " ")), index.h("div", { class: "meeting-time" }, index.h("p", null, " ", meetingTime, " ")), index.h("div", { class: "d-flex align-items-center text-primary" }, meeting.location.type === 'custom' ?
    index.h("p", { class: "custom-address" }, " ", truncate(meeting.location.address, 30), " ")
    :
      index.h("div", { class: "d-flex align-items-center" }, index.h("dvn-icon", { "icon-name": "uil uil-video", scale: "large", size: 20, class: "video-icon" }), index.h("a", { class: "ml-2", target: '_blank', href: meeting.location.address }, " ", meetingLocation, " "))))), index.h("dvn-icon", { onClick: () => goToMeetingDetails(meeting), class: "clickable", "icon-name": "uil uil-angle-right-b", size: 35 })), displayBreakLine &&
    index.h("hr", { class: "break-line" })));
};
const generateMeetings = (meetings, toggleSeeAllButton) => {
  if (!meetings)
    return [];
  // display only first 3 items
  if (!toggleSeeAllButton) {
    return meetings.slice(0, 3);
  }
  // display all item
  return meetings;
};
const truncate = (text, limit) => {
  if (text.length > limit) {
    return text.substring(0, limit) + '...';
  }
  return text;
};
MentornityUpcomingmeetingsWidget.style = mentornityUpcomingMeetingsWidgetCss;

exports.mentornity_pair_meeting_summary_widget = MentornityPairsMeetingWidget;
exports.mentornity_upcoming_meetings_widget = MentornityUpcomingmeetingsWidget;
