import { r as registerInstance, c as createEvent, h, H as Host } from './index-b7735e0d.js';
import { S as SozlukApi } from './index-1e9f4c53.js';
import { M as MentornityPairsMeetingWidgetI18nKey, m as mentornityPairsMeetingWidgetI18nDefault, a as MentornityUpcomingMeetingWidgetI18nKey, b as mentornityUpcomingMeetingWidgetI18nDefault } from './i18n-e90027e3.js';
import { C as CalendarUtils } from './calendar-67e174ec.js';
import './index-3958847a.js';
import './_commonjsHelpers-27f122dd.js';
import './moment-6f6ab70c.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) {
    registerInstance(this, hostRef);
    this.action = createEvent(this, "action", 7);
    this.isPending = true;
  }
  async componentWillLoad() {
    this.strings = await this.apiSozluk.getContent(MentornityPairsMeetingWidgetI18nKey, mentornityPairsMeetingWidgetI18nDefault);
  }
  render() {
    const { activePairs, isPending, meetingGoalConfig, menteeType, organisationConfig, requestedPairs, strings, userType, } = this;
    if (isPending) {
      return (h(Host, null, h("b", { class: "pl-3", style: { fontSize: '1.1rem' } }, strings.myPairs), h("dvn-card", { elevation: 1 }, h("div", { class: "d-flex justify-content-center" }, 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 (h(Host, null, h("b", { class: "pl-3", style: { fontSize: '1.1rem' } }, strings.myPairs), h("dvn-card", { elevation: 1 }, showPendingPairRequestAlert &&
      h("dvn-alert", { message: SozlukApi.build(strings.waitingForResponse, { count: requestedPairs.length }) }), showPairList &&
      h("div", { class: "row" }, activePairs.map(generatePairView(strings, pairsType, meetingGoalConfig, goToUserPairPage))), showMessages &&
      h("div", { class: "col-md-12 text-centered" }, messages.length > 0 && messages.join(' ')), showCardFooter &&
      h("dvn-card-footer", { slot: "footer", alignRight: true }, hasActivePairs &&
        h("dvn-button", { leaned: true, view: "flat", onClick: () => goToPairsPage() }, strings.seeAll), isMentorSelectionOn &&
        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 (h("div", { class: "col-lg-6 col-md-12 user-card" }, h("div", { class: "header" }, h("dvn-image", { slot: "media", src: user.imageUrl, text: shortName, leaned: true, shape: "circle", size: 50 }), h("b", { class: "clickable pl-2", onClick: () => goToUserPairPage(user) }, pairsType === 'group' ? user.title : user.firstName + ' ' + user.lastName), hasMeetingGoal &&
    h("dvn-linear-progress", { showPercent: hasMeetings, percent: calculateMeetingsPercent(meetings, hasMeetings, meetingGoalConfig), scale: "small" }), 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) {
    registerInstance(this, hostRef);
    this.action = 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(MentornityUpcomingMeetingWidgetI18nKey, 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 (h(Host, null, h("b", { class: "pl-3", style: { fontSize: '1.1rem' } }, strings.upcomingMeetings), h("dvn-card", { elevation: 1 }, h("div", { class: "d-flex justify-content-center" }, h("dvn-progress", { indefinite: true, tint: "primary" })))));
    }
    return (h(Host, null, h("b", { class: "pl-3", style: { fontSize: '1.1rem' } }, strings.upcomingMeetings), 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 &&
      h("dvn-card-footer", { class: "mt-3", slot: "footer", alignRight: true }, h("dvn-button", { leaned: true, view: "flat", onClick: () => {
          this.toggleSeeAllButton = true;
        } }, strings.seeAll, "(", meetings.length, ")")), !hasMeetings &&
      h("div", { class: "col-md-12 empty-meeting-text" }, h("p", null, " ", strings.listMeetingsHere, " "), 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(CalendarUtils.format(new Date(a.startDate * 1000), 'DD'));
        const bDate = Number(CalendarUtils.format(new Date(b.startDate * 1000), 'DD'));
        return aDate - bDate;
      });
    }
    return h("div", null, Object.keys(goalMeetings).map((key, i) => {
      if (goalMeetings[key].length === 0)
        return;
      if (renderedMeeings >= 3)
        return;
      return (h("div", null, h("div", { class: "meeting-goal-title" }, 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 (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 = CalendarUtils.format(new Date(meeting.startDate * 1000), 'MMM DD');
  const startDate = CalendarUtils.format(new Date(meeting.startDate * 1000), `ddd HH:mm ${timeFormat === '12-hour' ? 'A' : ''}`);
  const endDate = 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 (h("div", null, h("div", { class: "d-flex justify-content-between align-items-center" }, h("div", { class: "d-flex meeting-data" }, h("p", { class: "meeting-date", onClick: () => goToMeetingDetails(meeting) }, " ", meetingDate, " "), h("div", { class: "flex-column ml-3" }, h("div", { class: "full-name" }, h("p", null, " ", fullName, " ")), h("div", { class: "meeting-time" }, h("p", null, " ", meetingTime, " ")), h("div", { class: "d-flex align-items-center text-primary" }, meeting.location.type === 'custom' ?
    h("p", { class: "custom-address" }, " ", truncate(meeting.location.address, 30), " ")
    :
      h("div", { class: "d-flex align-items-center" }, h("dvn-icon", { "icon-name": "uil uil-video", scale: "large", size: 20, class: "video-icon" }), h("a", { class: "ml-2", target: '_blank', href: meeting.location.address }, " ", meetingLocation, " "))))), h("dvn-icon", { onClick: () => goToMeetingDetails(meeting), class: "clickable", "icon-name": "uil uil-angle-right-b", size: 35 })), displayBreakLine &&
    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;

export { MentornityPairsMeetingWidget as mentornity_pair_meeting_summary_widget, MentornityUpcomingmeetingsWidget as mentornity_upcoming_meetings_widget };
