import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { CardListViewConfig } from '@dvn/core/models/card-list-view-config';
import { DetailViewConfig } from '@dvn/core/models/detail-view-config';
import { TableConfig } from '@dvn/core/models/table-config';
import { TakvimAvailableTimePickerViewConfig } from '@dvn/takvim/views/dvn-app-view-takvim-available-time-picker-view/takvim-available-time-picker-view-config';
import { generateCalendarConfig, getEmbeddedTranslation } from '../../api/mentorinty-bridge-api';
import { convertFieldsToFieldReferences, getAvailableLegacyProfileFields, getAvailableMetadataFields, getMembershipFields, getUserDetailViewFieldConfigs } from '../common-wrapper-util';
import { ImagePosition, TitlePosition } from '@dvn/alaca/components/dvn-card-header/models';
export 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 = getMembershipFields(language, config, viewedUserRole, userRole);
  const fieldConfigReferences = 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 TextField('search', getEmbeddedTranslation(language, 'search')) },
    ...convertFieldsToFieldReferences([
      // remove title and bio because they'll be searched via the 'search' field above
      ...getAvailableLegacyProfileFields(language, config, viewedUserRole).filter(f => !['title', 'bio'].includes(f.id)),
      ...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: getEmbeddedTranslation(language, 'sendMessage'),
        name: 'send-message',
        tint: 'primary',
        scale: 'small',
        view: 'outline'
      }
    });
  }
  subActions.push(...[
    {
      ui: {
        label: getEmbeddedTranslation(language, 'showMore'),
        name: 'view-details',
        tint: 'primary',
        scale: 'small',
        view: 'outline'
      }
    }
  ]);
  return 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: ImagePosition.TopLeft,
            },
            title: {
              fieldId: 'user.name',
              position: TitlePosition.RightOfImage,
            },
            subtitleActions: subActions
          },
          body: {
            fields: cardBodyFields,
          },
          footer: cardFooter
        },
      },
    },
    table: {
      fields: membershipFields,
      roleName: viewedUserRole,
      name: `${viewedUserRole}-card-list`,
    },
    query: {
      fields: cardBodyFields,
    },
  });
}
export function getAvailableTimePickerViewConfig(config) {
  if (!config) {
    return undefined;
  }
  return TakvimAvailableTimePickerViewConfig.decode({
    _id: 'user-card-list-page-available-time-picker-view',
    i18nEntryId: 'user-card-list-page-available-time-picker-view',
    table: generateCalendarConfig(config),
    successNavigationAction: {
      type: 'navigation',
      direction: 'forward',
      name: 'open-meeting-detail-after-creation',
      params: {
        conversionConfig: {
          'item._id': 'meetingId',
        }
      }
    },
  });
}
export function getUserDetailDialogViewConfig(mentornityCookieContext, language, config, viewedUserRole) {
  if (!config) {
    return undefined;
  }
  const fields = getMembershipFields(language, config, viewedUserRole, mentornityCookieContext.role);
  const detailViewFields = getUserDetailViewFieldConfigs(language, config, viewedUserRole, mentornityCookieContext.role, false);
  const { organisationId } = mentornityCookieContext;
  const subActions = [{
      ui: {
        label: getEmbeddedTranslation(language, 'goToProfilePage'),
        name: 'go-to-profile-page',
      }
    }];
  return DetailViewConfig.decode({
    table: TableConfig.decode({
      fields,
      base: organisationId,
      name: viewedUserRole === 'mentee' ? 'mentees' : 'mentors',
    }),
    ui: {
      cardConfig: {
        header: {
          title: {
            fieldId: 'name',
          },
          actions: subActions
        },
        body: {
          fields: detailViewFields,
        }
      }
    },
  });
}
