'use strict';

const index = require('./index-61f9621a.js');
const cardListViewConfig = require('./card-list-view-config-ba20191c.js');
const detailViewConfig = require('./detail-view-config-7bcb0034.js');
const tableConfig = require('./table-config-969d3038.js');
const takvimAvailableTimePickerViewConfig = require('./takvim-available-time-picker-view-config-6bf1a5d8.js');
const mentorintyBridgeApi = require('./mentorinty-bridge-api-b1eaa56d.js');
const commonWrapperUtil = require('./common-wrapper-util-95591ac2.js');
const models = require('./models-8b357ee7.js');

function getUserCardListViewConfig(mentornityCookieContext, language, config, viewedUserRole, dependencies) {
  if (!config) {
    return null;
  }
  const userRole = mentornityCookieContext.role;
  // TODO
  // get the table fields from the view config if exists. otherwise generate them from the config
  // const viewConfig = getViewConfigForViewName(viewConfigs, MentornityViewConfigViewName.MenteeCardList);
  const membershipFields = commonWrapperUtil.getMembershipFields(language, config, viewedUserRole, userRole);
  const fieldConfigReferences = commonWrapperUtil.getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, true, 4);
  // add the big button for schedule meeting and view details actions
  const cardBodyFields = [...fieldConfigReferences];
  let cardFooter = {};
  const isMenteeViewingMentor = userRole === 'mentee' && viewedUserRole === 'mentor';
  const isMenteeEligibleToGetMeeting = config.menteeType === 'individual' || mentornityCookieContext.groupId;
  if (isMenteeViewingMentor && isMenteeEligibleToGetMeeting) {
    cardFooter = Object.assign({ customViewTag: 'mentornity-schedule-meeting-button-on-card', language }, dependencies);
  }
  // simple search ui config
  const quickSearchUIConfig = [];
  const quickSearchFieldConfigReferences = [
    { field: new index.TextField('search', mentorintyBridgeApi.getEmbeddedTranslation(language, 'search')) },
    ...commonWrapperUtil.convertFieldsToFieldReferences([
      // remove title and bio because they'll be searched via the 'search' field above
      ...commonWrapperUtil.getAvailableLegacyProfileFields(language, config, viewedUserRole).filter(f => !['title', 'bio'].includes(f.id)),
      ...commonWrapperUtil.getAvailableMetadataFields(mentornityCookieContext.role, viewedUserRole, config),
    ]),
  ];
  quickSearchFieldConfigReferences.forEach(fieldConfigReference => {
    var _a;
    const fieldId = fieldConfigReference.fieldId || ((_a = fieldConfigReference.field) === null || _a === void 0 ? void 0 : _a.id);
    if (fieldId) {
      quickSearchUIConfig.push({ fieldId, width: '200px' });
    }
  });
  const subActions = [];
  if (viewedUserRole === 'mentor' && userRole !== 'anonymous') {
    subActions.push({
      ui: {
        componentTag: 'mentornity-follow-button',
      },
      data: {
        language,
        mentornityCookieContext,
        isOnUserList: true
      }
    });
  }
  if (userRole !== 'anonymous') {
    subActions.push({
      ui: {
        label: mentorintyBridgeApi.getEmbeddedTranslation(language, 'sendMessage'),
        name: 'send-message',
        tint: 'primary',
        scale: 'small',
        view: 'outline'
      }
    });
  }
  subActions.push(...[
    {
      ui: {
        label: mentorintyBridgeApi.getEmbeddedTranslation(language, 'showMore'),
        name: 'view-details',
        tint: 'primary',
        scale: 'small',
        view: 'outline'
      }
    }
  ]);
  return cardListViewConfig.CardListViewConfig.decode({
    _id: `${viewedUserRole}-card-list`,
    i18nEntryId: `mentornity-${viewedUserRole}-card-list`,
    ui: {
      quickSearch: {
        fields: quickSearchFieldConfigReferences,
        fieldUIConfigs: quickSearchUIConfig,
      },
      main: {
        cardViewConfig: {
          header: {
            cover: {
              size: 120,
            },
            image: {
              fieldId: 'user.imageUrl',
              placeholderFieldId: 'user.initials',
              size: 96,
              position: models.ImagePosition.TopLeft,
            },
            title: {
              fieldId: 'user.name',
              position: models.TitlePosition.RightOfImage,
            },
            subtitleActions: subActions
          },
          body: {
            fields: cardBodyFields,
          },
          footer: cardFooter
        },
      },
    },
    table: {
      fields: membershipFields,
      roleName: viewedUserRole,
      name: `${viewedUserRole}-card-list`,
    },
    query: {
      fields: cardBodyFields,
    },
  });
}
function getAvailableTimePickerViewConfig(config) {
  if (!config) {
    return undefined;
  }
  return takvimAvailableTimePickerViewConfig.TakvimAvailableTimePickerViewConfig.decode({
    _id: 'user-card-list-page-available-time-picker-view',
    i18nEntryId: 'user-card-list-page-available-time-picker-view',
    table: mentorintyBridgeApi.generateCalendarConfig(config),
    successNavigationAction: {
      type: 'navigation',
      direction: 'forward',
      name: 'open-meeting-detail-after-creation',
      params: {
        conversionConfig: {
          'item._id': 'meetingId',
        }
      }
    },
  });
}
function getUserDetailDialogViewConfig(mentornityCookieContext, language, config, viewedUserRole) {
  if (!config) {
    return undefined;
  }
  const fields = commonWrapperUtil.getMembershipFields(language, config, viewedUserRole, mentornityCookieContext.role);
  const detailViewFields = commonWrapperUtil.getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, false);
  const { organisationId } = mentornityCookieContext;
  const subActions = [{
      ui: {
        label: mentorintyBridgeApi.getEmbeddedTranslation(language, 'goToProfilePage'),
        name: 'go-to-profile-page',
      }
    }];
  return detailViewConfig.DetailViewConfig.decode({
    table: tableConfig.TableConfig.decode({
      fields,
      base: organisationId,
      name: viewedUserRole === 'mentee' ? 'mentees' : 'mentors',
    }),
    ui: {
      cardConfig: {
        header: {
          title: {
            fieldId: 'name',
          },
          actions: subActions
        },
        body: {
          fields: detailViewFields,
        }
      }
    },
  });
}

exports.getAvailableTimePickerViewConfig = getAvailableTimePickerViewConfig;
exports.getUserCardListViewConfig = getUserCardListViewConfig;
exports.getUserDetailDialogViewConfig = getUserDetailDialogViewConfig;
