import { generateFieldModelFromRawData } from '@dvn/core/doldurma/src/FieldModelGenerator';
import { NumberField } from '@dvn/core/doldurma/src/fields/number';
import { TextField } from '@dvn/core/doldurma/src/fields/text';
import { ModifierTrimmer } from '@dvn/core/doldurma/src/interceptors/modifiers/ModifierTrimmer';
import { ValidatorLength } from '@dvn/core/doldurma/src/interceptors/validators/ValidatorLength';
import { FormViewConfig } from '@dvn/core/models/form-view-config';
import { TableConfig } from '@dvn/core/models/table-config';
import { AppViewType } from '@dvn/core/models/view-config';
import { ConfigMentorMenteeSubscription } from '../../../../mentornity-js-client/models';
import { convertMentornityFieldToDevingenField } from '../../api/mentorinty-bridge-api';
import skills from '../../constants/skills';
import skillsEN from '../../constants/skills-en';
import { convertFieldsToFieldReferences } from '../common-wrapper-util';
import { en, tr, pt_br } from './i18n';
import { UpdateMembershipInfoFormHandler } from './update-membership-info-form-handler';
export function getFormParameters(mentornityCookieContext, config, user, userMetadata, userMentorship) {
  const data = {
    firstName: user.firstName,
    lastName: user.lastName,
    bio: user.bio,
    title: user.title,
    skills: user.skills,
  };
  const { role } = mentornityCookieContext;
  const canEditMetadata = role === 'mentor'
    ? config.metadatas.mentorsCanEditMetadata
    : config.metadatas.menteesCanEditMetadata;
  if (canEditMetadata) {
    const metadataFields = ((role === 'mentor' ? config.metadatas.mentor : config.metadatas.mentee) || [])
      .map(convertMentornityFieldToDevingenField);
    metadataFields.forEach((field) => {
      data[field.id] = userMetadata.data[field.id];
    });
  }
  if (userMentorship) {
    data.subscriptionLimit = userMentorship.subscriptionLimit;
  }
  return data;
}
export function getFormViewConfig(mentornityCookieContext, language, config, viewConfigWrapper, user, userMetadata, userMentorship) {
  var _a;
  if (!config) {
    return null;
  }
  const i18n = language === 'tr'
    ? tr
    : (language === 'pt-br' ? pt_br : en);
  const { role } = mentornityCookieContext;
  const fieldsDefinitionForTable = getAvailableMembershipFormFields(i18n, role, config);
  const fieldReferencesForForm = ((_a = viewConfigWrapper === null || viewConfigWrapper === void 0 ? void 0 : viewConfigWrapper.dvnAppViewConfig) === null || _a === void 0 ? void 0 : _a.ui.fields)
    || convertFieldsToFieldReferences(fieldsDefinitionForTable);
  const canEditMetadata = role === 'mentor'
    ? config.metadatas.mentorsCanEditMetadata
    : config.metadatas.menteesCanEditMetadata;
  return FormViewConfig.decode({
    _id: 'update-membership-info-page',
    base: 'mentornity',
    title: i18n.formView.myInfo,
    table: TableConfig.decode({
      name: 'mentees',
      fields: fieldsDefinitionForTable,
    }),
    type: AppViewType.FormView,
    handler: new UpdateMembershipInfoFormHandler(user, canEditMetadata ? userMetadata : undefined, userMentorship, language),
    ui: {
      fields: fieldReferencesForForm,
      i18n: i18n.formView,
    }
  });
}
export function getAvailableMembershipFormFields(i18n, role, config) {
  const profileFieldVisibility = role === 'mentor'
    ? config.profileFieldVisibilities.mentor
    : config.profileFieldVisibilities.mentee;
  const defaultUserFields = [
    new TextField('firstName', i18n.formFields.firstName)
      .addInterceptor('onSubmit', new ValidatorLength(2, 32))
      .addInterceptor('onSubmit', new ModifierTrimmer())
      .require(),
    new TextField('lastName', i18n.formFields.lastName)
      .addInterceptor('onSubmit', new ValidatorLength(2, 32))
      .addInterceptor('onSubmit', new ModifierTrimmer())
      .require()
  ];
  if (!config.webAppConfiguration.membershipForm) {
    defaultUserFields.push(...[
      new TextField('title', i18n.formFields.title)
        .addInterceptor('onSubmit', new ModifierTrimmer()),
      new TextField('bio', i18n.formFields.bio)
        .setLines(3)
        .setLinesMax(10),
      generateFieldModelFromRawData({
        id: 'skills',
        title: i18n.formFields.skills,
        type: 'multiple-choice',
        inputType: 'select',
        options: skills.map(skill => ({
          label: skillsEN[skill], value: skill,
        })),
      }),
    ].filter(field => profileFieldVisibility[field.id]));
  }
  const canEditMetadata = role === 'mentor'
    ? config.metadatas.mentorsCanEditMetadata
    : config.metadatas.menteesCanEditMetadata;
  let metadataFields = [];
  if (canEditMetadata) {
    metadataFields = ((role === 'mentor' ? config.metadatas.mentor : config.metadatas.mentee) || [])
      .map(convertMentornityFieldToDevingenField);
  }
  const isPairedProgram = config.mentorMenteeSubscription === ConfigMentorMenteeSubscription.Restricted;
  const fieldsDefinitionForTable = (role === 'mentor' && isPairedProgram)
    ? [...defaultUserFields, new NumberField('subscriptionLimit', i18n.formFields.pairLimit), ...metadataFields]
    : [...defaultUserFields, ...metadataFields];
  return fieldsDefinitionForTable;
}
