import { C as CredentialRepository } from "./credential-aTtsoHE4.js";
import SalesforceCRMService from "./CrmService-cvBbMHTd.js";
import { S as SalesforceRecordEnum, R as RoutingReasons } from "./zod-9h3MdkVv.js";
import { E as EventTypeAppMetadataSchema } from "./zod-utils-0_1_B2QB.js";
import { p as prisma, E as EventTypeMetaDataSchema } from "./i18n-49-r55rr.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
};
