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 } from '../../api/mentorinty-bridge-api';
import { convertFieldsToFieldReferences, getAvailableLegacyProfileFields, getAvailableMetadataFields, getMembershipFields, getUserDetailViewFieldConfigs } from '../common-wrapper-util';
export function getUserCardListViewConfig(mentornityCookieContext, _language, config, viewedUserType) {
  if (!config) {
    return null;
  }
  // 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(config, viewedUserType, mentornityCookieContext.role);
  const fieldConfigReferences = getUserDetailViewFieldConfigs(config, viewedUserType, mentornityCookieContext.role, true, 4);
  // add the big button for schedule meeting and view details actions
  const cardBodyFields = [...fieldConfigReferences, {
      customViewTag: 'mentornity-schedule-meeting-button-on-card',
    }];
  // simple search ui config
  const quickSearchUIConfig = [];
  const quickSearchFieldConfigReferences = [
    { field: new TextField('search', 'Search') },
    ...convertFieldsToFieldReferences([
      // remove title and bio because they'' be searched via the 'search' field above
      ...getAvailableLegacyProfileFields(config, viewedUserType).filter(f => !['title', 'bio'].includes(f.id)),
      ...getAvailableMetadataFields(mentornityCookieContext.role, 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' });
    }
  });
  return CardListViewConfig.decode({
    _id: `${viewedUserType}-card-list`,
    ui: {
      quickSearch: {
        fields: quickSearchFieldConfigReferences,
        fieldUIConfigs: quickSearchUIConfig,
      },
      main: {
        cardViewConfig: {
          header: {
            cover: {
              size: 100,
            },
            image: {
              fieldId: 'user.imageUrl',
              placeholderFieldId: 'user.initials',
              size: 72,
            },
            title: {
              fieldId: 'user.name',
            },
          },
          body: {
            fields: cardBodyFields,
          },
        },
      },
    },
    table: {
      fields: membershipFields,
      roleName: viewedUserType,
      name: `${viewedUserType}-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',
    table: generateCalendarConfig(config),
    successNavigationAction: {
      type: 'navigation',
      direction: 'forward',
      name: 'open-meeting-detail-after-creation',
      params: {
        conversionConfig: {
          'item._id': 'meetingId',
        }
      }
    },
  });
}
export function getUserDetailDialogViewConfig(mentornityCookieContext, config, viewedUserRole) {
  if (!config) {
    return undefined;
  }
  const fields = getMembershipFields(config, viewedUserRole, mentornityCookieContext.role);
  const detailViewFields = getUserDetailViewFieldConfigs(config, viewedUserRole, mentornityCookieContext.role, false);
  const { organisationId } = mentornityCookieContext;
  return DetailViewConfig.decode({
    table: TableConfig.decode({
      base: organisationId,
      name: viewedUserRole === 'mentee' ? 'mentees' : 'mentors',
    }),
    ui: {
      fields,
      cardConfig: {
        header: {
          title: {
            fieldId: 'name',
          },
          actions: [
            {
              ui: {
                label: 'Go to Profile Page',
                name: 'go-to-profile-page',
              }
            }
          ],
        },
        body: {
          fields: detailViewFields,
        }
      }
    },
  });
}
