'use strict';

require('./index-b7855ed6.js');
const referenceField = require('./reference-field-1239b0c3.js');
const index = require('./index-61f9621a.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-b1eaa56d.js');
const attachment = require('./attachment-a0cdf203.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 language
 * @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(language, config, viewedUserRole, role) {
  return [
    ...getAvailableLegacyProfileFields(language, config, viewedUserRole),
    ...getAvailableMetadataFields(role, viewedUserRole, config),
    new referenceField.ReferenceField('user', 'User', 'firstName', 'imageUrl', 'users', [
      new attachment.AttachmentField('imageUrl', ''),
      new index.TextField('firstName', 'First Name'),
    ]),
  ];
}
/**
 * Returns the list of fields that can be shown to build a view config.
 * @param language
 * @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(language, config, viewedUserRole, role) {
  return [
    ...getAvailableLegacyProfileFields(language, config, viewedUserRole),
    ...getAvailableMetadataFields(role, viewedUserRole, config),
  ];
}
/**
 * Returns the list of card body fields that can be shown inside a detail view (card view, etc.).
 * @param language
 * @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(language, config, viewedUserRole, role, isSummary, limit) {
  let fields;
  if (isSummary) {
    fields = convertFieldsToFieldReferences(getUserDetailFields(language, config, viewedUserRole, role))
      .map(fieldConfig => (Object.assign(Object.assign({}, fieldConfig), { lines: 1, maxItems: 3 })));
  }
  else {
    fields = convertFieldsToFieldReferences(getUserDetailFields(language, 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 language
 * @param config of the program.
 * @param viewedUserType is either 'mentee' or 'mentor'.
 */
function getAvailableLegacyProfileFields(language, config, viewedUserType) {
  const profileFieldVisibility = viewedUserType === 'mentor'
    ? config.profileFieldVisibilities.mentor
    : config.profileFieldVisibilities.mentee;
  return mentorintyBridgeApi.legacyProfileFields(language).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 viewedUserRole
 * @param config
 */
function getAvailableMetadataFields(role, viewedUserRole, config) {
  // show the mentee fields if the user role is mentor. otherwise show mentor fields
  const canUserViewMetadatas = config.metadataVisibility[role] !== 'none';
  return (canUserViewMetadatas
    ? (viewedUserRole === 'mentor' ? config.metadatas.mentor : config.metadatas.mentee) || []
    : []).map(f => mentorintyBridgeApi.convertMentornityFieldToDevingenField(f));
}
/**
 * Filters out the fields that are no longer in the metadata configs.
 *
 * @param role
 * @param config
 * @param fieldConfigs
 */
function filterUnavailableMetadataFields(role, config, fieldConfigs) {
  const metadatas = role === 'mentor' ? config.metadatas.mentee : config.metadatas.mentor;
  const metadataKeys = metadatas.map(m => m.id);
  return fieldConfigs.filter(({ fieldId }) => metadataKeys.includes(fieldId));
}
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.filterUnavailableMetadataFields = filterUnavailableMetadataFields;
exports.getAvailableLegacyProfileFields = getAvailableLegacyProfileFields;
exports.getAvailableMetadataFields = getAvailableMetadataFields;
exports.getMembershipFields = getMembershipFields;
exports.getPairConfigOptionsFromOrganisationConfig = getPairConfigOptionsFromOrganisationConfig;
exports.getUserDetailViewFieldConfigs = getUserDetailViewFieldConfigs;
exports.getViewConfigForViewName = getViewConfigForViewName;
