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 { convertMentornityFieldToDevingenField } from "../../api/mentorinty-bridge-api";
import { convertFieldsToFieldReferences } from "../common-wrapper-util";
import { UpdateUserMetadataHandler } from "./update-user-metadata-form-handler";
export function getFormParameters(mentornityCookieContext, config, userMetadata) {
  const data = {};
  const { role } = mentornityCookieContext;
  const metadataFields = ((role === 'mentor' ? config.metadatas.mentor : config.metadatas.mentee) || [])
    .map(convertMentornityFieldToDevingenField);
  metadataFields.forEach((field) => {
    data[field.id] = userMetadata.data[field.id];
  });
  return data;
}
export function getFormViewConfig(mentornityCookieContext, config, userMetadata) {
  if (!config) {
    return null;
  }
  const { role } = mentornityCookieContext;
  const fieldsDefinitionForTable = getMetadataFormFields(role, config);
  const fieldReferencesForForm = convertFieldsToFieldReferences(fieldsDefinitionForTable);
  return FormViewConfig.decode({
    _id: 'update-user-metadata-form',
    base: 'mentornity',
    table: TableConfig.decode({
      name: 'mentees',
      fields: fieldsDefinitionForTable,
    }),
    type: AppViewType.FormView,
    handler: new UpdateUserMetadataHandler(userMetadata),
    ui: {
      fields: fieldReferencesForForm
    }
  });
}
export function getMetadataFormFields(role, config) {
  let metadataFields = [];
  metadataFields = ((role === 'mentor' ? config.metadatas.mentor : config.metadatas.mentee) || [])
    .map(convertMentornityFieldToDevingenField);
  const fieldsDefinitionForTable = [...metadataFields];
  return fieldsDefinitionForTable;
}
