import { AtamaMemberPairDetailViewConfig } from '../../../../atama/src/views/atama-member-pair-detail-view-config';
import { spaces } from '../../../../mentornity-console/src/configs/spaces';
import { pairConfig } from '../../constants/pair-config';
import { getRouteParams } from '../../util/route';
import { convertFieldsToFieldReferences, getFieldsToShow, getPairConfigOptionsFromOrganisationConfig } from '../common-wrapper-util';
import { en, tr } from './i18n';
export function getPairDetailParameters(mentornityCookieContext, config) {
  const { role } = mentornityCookieContext;
  const viewingGroups = role === 'mentor' && config.menteeType === 'group';
  // if user is mentor, then the view config should say that the 'subscribed' is viewing the 'subscriber'
  // if user is mentee, then it should say otherwise
  const userRefFieldIdValue = role === 'mentor' ? 'subscribed' : 'subscriber';
  const memberToViewRefFieldIdValue = role === 'mentor' ? 'subscriber' : 'subscribed';
  const routeParams = getRouteParams(location.pathname);
  const showAvailabilitiesButton = role === 'mentee' && config.meetingSettings.mentorsCanCreateAvailability;
  const showCreateMeetingButton = (role === 'mentee' && config.meetingSettings.menteesCanCreateMeeting) ||
    (role === 'mentor' && config.meetingSettings.mentorsCanCreateMeeting);
  // don't show the message button if mentor is viewing group
  const showSendMessageButton = !viewingGroups;
  return {
    memberToViewRefFieldId: memberToViewRefFieldIdValue,
    userRefFieldId: userRefFieldIdValue,
    memberToViewRefFieldValue: new URLSearchParams(window.location.search).get('uid'),
    pairId: routeParams.id,
    viewIdToNavigateOnEndPair: 'pair-overview-page',
    showAvailabilitiesButton,
    showCreateMeetingButton,
    showSendMessageButton,
  };
}
export function getPairDetailViewConfig(mentornityCookieContext, language, config, goalConfig) {
  if (!config) {
    return null;
  }
  const i18n = language === 'tr' ? tr : en;
  const { role } = mentornityCookieContext;
  const viewingGroups = role === 'mentor' && config.menteeType === 'group';
  // if user is mentor, then the view config should say that the 'subscribed' is viewing the 'subscriber'
  // if user is mentee, then it should say otherwise
  const userRefFieldIdValue = role === 'mentor' ? 'subscribed' : 'subscriber';
  const memberToViewRefFieldIdValue = role === 'mentor' ? 'subscriber' : 'subscribed';
  const profileFieldVisibility = role === 'mentor' ? config.profileFieldVisibilities.mentee : config.profileFieldVisibilities.mentor;
  const defaultUserFields = ['title', 'bio', 'skills'].filter(id => profileFieldVisibility[id]).map(id => ({ fieldId: id }));
  const defaultGroupFields = ['description', 'members'].map(id => ({ fieldId: id, showTitle: true }));
  const fields = viewingGroups ? [] : getFieldsToShow(role, config);
  const infoDetailCardFields = (viewingGroups ? defaultGroupFields : [...defaultUserFields, ...convertFieldsToFieldReferences(fields)])
    .filter(field => !['fullName', 'imageUrl'].includes(field.fieldId));
  const infoSummaryCardFields = infoDetailCardFields
    .slice(0, 3)
    .map(fieldConfig => (Object.assign(Object.assign({}, fieldConfig), { lines: 1 }))); // to trim long texts
  return AtamaMemberPairDetailViewConfig.decode({
    _id: 'pair-detail-page',
    ui: {
      i18n: Object.assign(Object.assign({}, i18n.detailView), { roleName: getLocalisedRoleName(config, role, language, viewingGroups) }),
      main: {
        infoSummaryCardViewConfig: {
          header: {},
          body: {
            fields: infoSummaryCardFields,
          },
          footer: {}
        },
        infoDetailCardViewConfig: {
          header: {},
          body: {
            fields: infoDetailCardFields,
          },
          footer: {}
        },
      },
    },
    space: spaces['space-1'],
    title: 'Pair Details',
    type: 'atama-member-pair-detail-view',
    table: pairConfig(config, goalConfig, getPairConfigOptionsFromOrganisationConfig(config)),
    params: {
      userRefFieldId: {
        required: true,
        value: userRefFieldIdValue
      },
      memberToViewRefFieldId: {
        required: true,
        value: memberToViewRefFieldIdValue,
      },
      memberToViewRefFieldValue: {
        required: true,
      },
      pairId: {
        required: true,
      },
      viewIdToNavigateOnEndPair: {
        required: true,
        value: 'pair-overview-page',
      },
    },
  });
}
const roleNameMaps = {
  en: {
    group: {
      company: 'Company',
      group: 'Group',
      startup: 'Startup',
    },
    mentee: {
      attendee: 'Attendee',
      candidate: 'Candidate',
      coachee: 'Coachee',
      consultee: 'Consultee',
      entrepreneur: 'Entrepreneur',
      member: 'Member',
      mentee: 'Mentee',
      student: 'Student',
    },
    mentor: {
      alumnus: 'Alumnus',
      angel: 'Angel',
      authority: 'Company Rep.',
      coach: 'Coach',
      consultant: 'Consultant',
      expert: 'Expert',
      host: 'Host',
      mentor: 'Mentor',
      partner: 'Partner',
    },
  },
  tr: {
    group: {
      company: 'Şirket',
      group: 'Grup',
      startup: 'Girişim',
    },
    mentee: {
      attendee: 'Katılımcı',
      candidate: 'Aday',
      coachee: 'Danışan',
      consultee: 'Danışan',
      entrepreneur: 'Girişimci',
      member: 'Üye',
      mentee: 'Menti',
      student: 'Öğrenci',
    },
    mentor: {
      alumnus: 'Mezun',
      angel: 'Melek',
      authority: 'Yetkili',
      coach: 'Koç',
      consultant: 'Danışman',
      expert: 'Uzman',
      host: 'Düzenleyici',
      mentor: 'Mentor',
      partner: 'Partner',
    },
  }
};
const getLocalisedRoleName = (config, accountRole, language, viewingGroups) => {
  const otherRole = accountRole === 'mentor' ? (viewingGroups ? 'group' : 'mentee') : 'mentor';
  const roleNameConstant = accountRole === 'mentor'
    ? (viewingGroups ? config.termReplacements.group : config.termReplacements.mentee)
    : config.termReplacements.mentor;
  return roleNameMaps[language][otherRole][roleNameConstant];
};
