import { Membership } from '@dvn/core/models/membership';
import { MembershipConfig } from '@dvn/core/models/membership-config';
import { User } from '@dvn/kimlik/api/models';
import { UserSettings } from '@dvn/kimlik/api/models/UserSettings';
import { CookieStorage } from '@dvn/kimlik/storage/cookie';
import { getRouteParams } from './route';
import { createMentornityApiInstance } from "../../../mentornity-js-client";
import { mentornityApiEndpoint } from "../constants/env";
import { MentornityBridgeApi } from "../api/mentorinty-bridge-api";
import { SozlukApi } from "@dvn/sozluk/api";
import { sozlukEntries } from "../view-wrappers/sozluk-entries";
import CalendarUtils from "@dvn/core/util/calendar";
import { sozlukCalendarEntryEn, sozlukCalendarEntryPtBr, sozlukCalendarEntryTr } from "../view-wrappers/sozluk-calendar-entries";
export function getSessionDetails(groupId) {
  const { domain } = getRouteParams(location.pathname);
  const jwt = new CookieStorage().get('mt_at');
  const sessionToken = new CookieStorage().get('mt_st');
  const context = {
    organisationDomain: domain,
    organisationId: new CookieStorage().get('mt_oid'),
    userId: new CookieStorage().get('mt_uid'),
    role: new CookieStorage().get('mt_role'),
  };
  const client = new CookieStorage().get('Client');
  const headers = {
    Client: client || 'd2ViLWNsaWVu',
  };
  if (jwt) {
    headers['Authorization'] = `Bearer ${jwt}`;
    headers['Session-Token'] = sessionToken;
  }
  else {
    headers['Session-Token'] = `NTdiMzY3NjBlNjNiY2UxZWViMDAwMDA0`;
  }
  context.groupId = groupId;
  return { context, headers };
}
export function getUserSettings(timeFormat, dateFormat, timeZone, language, isZoomAuthenticated, isGoogleAuthenticated, isMicrosoftBusinessAuthenticated, weekStartDay = 'monday') {
  const authPlatforms = [];
  if (isZoomAuthenticated) {
    authPlatforms.push({
      id: 'zoom',
      name: 'Zoom',
      scope: []
    });
  }
  if (isGoogleAuthenticated) {
    authPlatforms.push({
      id: 'google',
      name: 'Google Meet',
      scope: []
    });
  }
  if (isMicrosoftBusinessAuthenticated) {
    authPlatforms.push({
      id: 'microsoftbusiness',
      name: 'Microsoft Teams Meet',
      scope: []
    });
  }
  return UserSettings.decode({ timeFormat, dateFormat, timeZone, language, authPlatforms, weekStartDay });
}
export function generateMembershipForRole(role) {
  return Membership.decode({
    config: generateMembershipConfigForRole(role),
  });
}
export function generateMembershipConfigIdForRole(role) {
  return `${role}s`;
}
export function generateMembershipConfigForRole(role) {
  return MembershipConfig.decode({
    _id: generateMembershipConfigIdForRole(role),
    roleName: role
  });
}
export function getUser(id) {
  return User.decode({ _id: id });
}
export async function generateMentornitySessionContext(contextParams, options) {
  const { organisationId, 
  // organisationDomain,
  accessToken, sessionToken, 
  // role,
  userId, 
  // groupId,
  dateFormat, language, isZoomAuthenticated, isGoogleAuthenticated, isMicrosoftBusinessAuthenticated, timeFormat, timeZone, weekStartDay, } = contextParams;
  const apiHeaders = generateApiHeaders(accessToken, sessionToken);
  const api = createMentornityApiInstance(mentornityApiEndpoint, apiHeaders);
  const mentornityContext = {
    apiSozluk: new SozlukApi().setLanguage(language).setContents(sozlukEntries),
    mentornityBridgeApi: new MentornityBridgeApi(mentornityApiEndpoint, contextParams, apiHeaders),
    userSettings: getUserSettings(timeFormat, dateFormat, timeZone, language, isZoomAuthenticated, isGoogleAuthenticated, isMicrosoftBusinessAuthenticated, weekStartDay),
    user: User.decode({ _id: userId }),
  };
  if (options === null || options === void 0 ? void 0 : options.fetchConfig) {
    mentornityContext.organisationConfig = await api.getOrganisationConfig(organisationId);
    mentornityContext.mentornityBridgeApi.organisationConfig = mentornityContext.organisationConfig;
  }
  if (options === null || options === void 0 ? void 0 : options.fetchMentorship) {
    mentornityContext.mentorship = await api.getMentorshipOfUser(organisationId, userId);
    mentornityContext.mentornityBridgeApi.mentorship = mentornityContext.mentorship;
  }
  // set user's locale and time zone
  CalendarUtils.setTimeFormat(timeFormat);
  CalendarUtils.setDateFormat(dateFormat);
  CalendarUtils.setTimeZone(timeZone);
  switch (language) {
    case 'pt-br':
      CalendarUtils.setLocale(language, sozlukCalendarEntryPtBr, weekStartDay);
      break;
    case 'tr':
      CalendarUtils.setLocale(language, sozlukCalendarEntryTr, weekStartDay);
      break;
    default:
      CalendarUtils.setLocale(language, sozlukCalendarEntryEn, weekStartDay);
      break;
  }
  return mentornityContext;
}
function generateApiHeaders(accessToken, sessionToken) {
  const client = new CookieStorage().get('Client');
  const headers = {
    Client: client || 'd2ViLWNsaWVu',
    'Session-Token': 'NTdiMzY3NjBlNjNiY2UxZWViMDAwMDA0'
  };
  if (accessToken) {
    headers['Authorization'] = `Bearer ${accessToken}`;
  }
  if (sessionToken) {
    headers['Session-Token'] = sessionToken;
  }
  return headers;
}
