'use strict';

require('./index-d90390f1.js');
const index = require('./index-74a00556.js');
const index$1 = require('./index-a8f1783c.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-22218ac9.js');
const attachment = require('./attachment-3090e25e.js');

function convertFieldsToFieldReferences(fields) {
  return fields.map(field => ({ fieldId: field.id }));
}
function getViewConfigForViewName(viewConfigs, viewName) {
  return viewConfigs.find(viewConfig => viewConfig.viewName === viewName);
}
/**
 * Returns the complete list of fields that should be put into the membership config of the viewed user.
 * @param config of the program.
 * @param viewedUserRole the role of the user (or user list) that is being viewed.
 * @param role of the current user ('mentee' or 'mentor').
 */
function getMembershipFields(config, viewedUserRole, role) {
  return [
    ...getAvailableLegacyProfileFields(config, viewedUserRole),
    ...getAvailableMetadataFields(role, config),
    new index.ReferenceField('user', 'User', 'firstName', 'imageUrl', 'users', [
      new attachment.AttachmentField('imageUrl', ''),
      new index$1.TextField('firstName', 'First Name'),
    ]),
  ];
}
/**
 * Returns the list of fields that can be shown to build a view config.
 * @param config of the program.
 * @param viewedUserRole the role of the user (or user list) that is being viewed.
 * @param role of the current user ('mentee' or 'mentor').
 */
function getUserDetailFields(config, viewedUserRole, role) {
  return [
    ...getAvailableLegacyProfileFields(config, viewedUserRole),
    ...getAvailableMetadataFields(role, config),
  ];
}
/**
 * Returns the list of card body fields that can be shown inside a detail view (card view, etc.).
 * @param config of the program.
 * @param viewedUserRole the role of the user (or user list) that is being viewed.
 * @param role of the current user ('mentee' or 'mentor').
 * @param isSummary prepares the config to trim long info for summary views (card list).
 * @param limit is used to trim the list to return only a specific number of fields.
 */
function getUserDetailViewFieldConfigs(config, viewedUserRole, role, isSummary, limit) {
  let fields;
  if (isSummary) {
    fields = convertFieldsToFieldReferences(getUserDetailFields(config, viewedUserRole, role))
      .map(fieldConfig => (Object.assign(Object.assign({}, fieldConfig), { lines: 1, maxItems: 3 })));
  }
  else {
    fields = convertFieldsToFieldReferences(getUserDetailFields(config, viewedUserRole, role));
  }
  if (limit) {
    fields = fields.slice(0, limit);
  }
  return fields;
}
/**
 * Filters the legacy profile fields based on the program config. Hides title, bio or skills if not allowed by admins.
 * @param config of the program.
 * @param viewedUserType is either 'mentee' or 'mentor'.
 */
function getAvailableLegacyProfileFields(config, viewedUserType) {
  const profileFieldVisibility = viewedUserType === 'mentor'
    ? config.profileFieldVisibilities.mentor
    : config.profileFieldVisibilities.mentee;
  return mentorintyBridgeApi.legacyProfileFields.filter(field => profileFieldVisibility[field.id]);
}
/**
 * Returns the metadata fields based on the user role and the program config.
 * It may return an empty list if the config doesn't allow the user to view the metadatas of the viewed user.
 * @param role
 * @param config
 */
function getAvailableMetadataFields(role, config) {
  // show the mentee fields if the user role is mentor. otherwise show mentor fields
  const canUserViewMetadatas = config.metadataVisibility[role] !== 'none';
  return (canUserViewMetadatas
    ? (role === 'mentor' ? config.metadatas.mentee : config.metadatas.mentor) || []
    : []).map(f => mentorintyBridgeApi.convertMentornityFieldToDevingenField(f));
}
function getPairConfigOptionsFromOrganisationConfig(config) {
  const { subscriptionConfig } = config;
  return {
    isMentorSelectionOn: subscriptionConfig.mentorVisibilityStatus === 'showAvailableMentors',
    canMentorEndSubscription: subscriptionConfig.mentorsCanEndSubscription,
    pairLimitForMentees: config.subscriptionConfig.menteeSubscriptionLimit,
    selectionLimitForMentees: config.subscriptionConfig.menteeSelectionLimit,
  };
}

exports.convertFieldsToFieldReferences = convertFieldsToFieldReferences;
exports.getAvailableLegacyProfileFields = getAvailableLegacyProfileFields;
exports.getAvailableMetadataFields = getAvailableMetadataFields;
exports.getMembershipFields = getMembershipFields;
exports.getPairConfigOptionsFromOrganisationConfig = getPairConfigOptionsFromOrganisationConfig;
exports.getUserDetailViewFieldConfigs = getUserDetailViewFieldConfigs;
exports.getViewConfigForViewName = getViewConfigForViewName;
