import { decodeFields } from '@dvn/core/doldurma/src/decoder';
import { DetailViewConfig } from '@dvn/core/models/detail-view-config';
import { TableConfig } from '@dvn/core/models/table-config';
import { AtamaMemberPairsOverviewViewConfig } from '../../../../atama/src/views/atama-member-pairs-overview-view-config';
import { InfoVisibility } from '../../../../mentornity-js-client/models';
import { commonUserInfoFields } from '../../api/mentorinty-bridge-api';
import { pairConfig } from '../../constants/pair-config';
import { convertFieldsToFieldReferences, getFieldsToShow, getPairConfigOptionsFromOrganisationConfig } from '../common-wrapper-util';
import { en, tr } from './i18n';
export function getPairOverviewParameters(mentornityCookieContext) {
  const { role } = mentornityCookieContext;
  // 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';
  return {
    memberToViewRefFieldId: memberToViewRefFieldIdValue,
    userRefFieldId: userRefFieldIdValue,
    viewIdToNavigateToSelectPair: 'pair-selection-page',
    viewIdToViewRequestSender: 'user-page',
  };
}
export function getPairOverviewViewConfig(mentornityCookieContext, language, config) {
  if (!config) {
    return null;
  }
  const i18n = language === 'tr' ? tr : en;
  const { role } = mentornityCookieContext;
  const viewingGroups = role === 'mentor' && config.menteeType === 'group';
  const profileFieldVisibility = role === 'mentor' ? config.profileFieldVisibilities.mentee : config.profileFieldVisibilities.mentor;
  const defaultUserFields = ['title', 'bio', 'skills', 'imagePlaceholderValue'].filter(id => profileFieldVisibility[id]).map(id => ({ fieldId: id }));
  const defaultGroupFields = ['description', 'imagePlaceholderValue'].map(id => ({ fieldId: id }));
  const fields = viewingGroups ? [] : getFieldsToShow(role, config);
  const cardBodyFields = (viewingGroups ? defaultGroupFields : [...defaultUserFields, ...convertFieldsToFieldReferences(fields)])
    .slice(0, 3)
    .map(fieldConfig => (Object.assign(Object.assign({}, fieldConfig), { lines: 1 }))) // to trim long texts
    .filter(field => !['fullName', 'imageUrl', 'imagePlaceholderValue'].includes(field.fieldId));
  return AtamaMemberPairsOverviewViewConfig.decode({
    _id: 'pair-overview-page',
    ui: {
      i18n: i18n.view,
      main: {
        cardViewConfig: {
          header: {
            cover: {
              size: 100,
            },
            image: {
              fieldId: 'imageUrl',
              placeholderFieldId: 'imagePlaceholderValue',
              shape: viewingGroups ? 'rounded' : undefined,
              size: 72,
            },
            title: {
              fieldId: viewingGroups ? 'title' : 'fullName',
            },
          },
          body: {
            fields: cardBodyFields,
          },
          footer: {
            actions: [
              {
                data: {
                  type: 'navigation',
                  direction: 'forward',
                  placement: 'view',
                  viewId: 'subscriptions',
                  params: {
                    conversionConfig: {
                      'item._id': 'memberToViewRefFieldValue',
                      'item.subscription._id': 'pairId',
                    }
                  }
                },
                ui: {
                  label: i18n.view.viewAndMeet || 'View & Meet',
                  name: 'open-pair-detail-view',
                  view: 'ghost',
                }
              }
            ]
          }
        },
      },
    },
    params: {},
    table: pairConfig(config, undefined, getPairConfigOptionsFromOrganisationConfig(config)),
  });
}
export function getUserDetailDialogParams(actionData) {
  return {
    itemId: actionData.params.item._id,
  };
}
export function getUserDetailDialogViewConfig(mentornityCookieContext, config) {
  if (!config) {
    return undefined;
  }
  const { organisationId, role } = mentornityCookieContext;
  const profileFieldVisibility = role === 'mentor' ? config.profileFieldVisibilities.mentee : config.profileFieldVisibilities.mentor;
  const defaultUserFields = ['title', 'bio', 'skills'].filter(id => profileFieldVisibility[id]).map(id => ({ fieldId: id }));
  const fields = getFieldsToShow(role, config);
  const detailViewMembershipFields = decodeFields([...commonUserInfoFields, ...fields]);
  const detailViewCardFields = [...defaultUserFields, ...convertFieldsToFieldReferences(fields)]
    .filter(field => !['fullName', 'imageUrl'].includes(field.fieldId));
  const showMessageButton = [
    InfoVisibility.allMentees,
    InfoVisibility.everyone,
  ].includes(config.conversationPermissions.mentor);
  return DetailViewConfig.decode({
    table: TableConfig.decode({
      base: organisationId,
      name: 'mentees',
    }),
    ui: {
      fields: detailViewMembershipFields,
      cardConfig: {
        header: {
          title: {
            fieldId: 'fullName',
          },
          actions: showMessageButton ? [
            {
              ui: {
                label: 'Send Message',
                name: 'send-message',
              }
            }
          ] : [],
        },
        body: {
          fields: detailViewCardFields,
        }
      }
    },
  });
}
