import { C as CredentialRepository } from "./credential-9TDHyS5F.js";
import { p as prisma, E as EventTypeMetaDataSchema, V as EventTypeAppMetadataSchema, F as SalesforceRecordEnum, z as RoutingReasons } from "./i18n-VAwx2uzJ.js";
import SalesforceCRMService from "./CrmService-AceJaSwn.js";
class EventTypeService {
  static async getEventTypeAppDataFromId(eventTypeId, appSlug) {
    const eventType = await prisma.eventType.findUnique({
      where: {
        id: eventTypeId
      },
      select: {
        metadata: true
      }
    });
    if (!eventType)
      return null;
    return this.getEventTypeAppDataFromMetadata(eventType.metadata, appSlug);
  }
  static getEventTypeAppDataFromMetadata(metadata, appSlug) {
    const parseEventTypeAppMetadata = EventTypeMetaDataSchema.safeParse(metadata);
    if (!parseEventTypeAppMetadata.success || !parseEventTypeAppMetadata.data?.apps)
      return null;
    const eventTypeAppMetadata = parseEventTypeAppMetadata.data.apps;
    const apps = EventTypeAppMetadataSchema.parse(eventTypeAppMetadata);
    const appMetadata = apps[appSlug];
    return appMetadata;
  }
}
const routingFormBookingFormHandler = async (attendeeEmail, attributeRoutingConfig, eventTypeId) => {
  const salesforceSettings = attributeRoutingConfig?.salesforce;
  if (!salesforceSettings || !salesforceSettings.rrSkipToAccountLookupField || !salesforceSettings.rrSKipToAccountLookupFieldName)
    return { email: null, recordType: null, recordId: null };
  const appData = await EventTypeService.getEventTypeAppDataFromId(eventTypeId, "salesforce");
  const credentialId = appData.credentialId;
  const credential = await CredentialRepository.findFirstByIdWithKeyAndUser({ id: credentialId });
  if (!credential)
    return { email: null, recordType: null, recordId: null };
  const crm = new SalesforceCRMService(credential, {});
  const userLookupEmail = await crm.findUserEmailFromLookupField(
    attendeeEmail,
    salesforceSettings.rrSKipToAccountLookupFieldName,
    SalesforceRecordEnum.ACCOUNT
  );
  return {
    email: userLookupEmail?.email ?? null,
    recordType: RoutingReasons.ACCOUNT_LOOKUP_FIELD,
    recordId: null
  };
};
const appBookingFormHandler = {
  salesforce: routingFormBookingFormHandler
};
export {
  appBookingFormHandler as default
};
