"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const credential = require("./credential-yvBSPLqv.cjs");
const index = require("./i18n-Bs8xJzoY.cjs");
const CrmService = require("./CrmService-wJz9qBdP.cjs");
class EventTypeService {
  static async getEventTypeAppDataFromId(eventTypeId, appSlug) {
    const eventType = await index.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 = index.EventTypeMetaDataSchema.safeParse(metadata);
    if (!parseEventTypeAppMetadata.success || !parseEventTypeAppMetadata.data?.apps)
      return null;
    const eventTypeAppMetadata = parseEventTypeAppMetadata.data.apps;
    const apps = index.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$1 = await credential.CredentialRepository.findFirstByIdWithKeyAndUser({ id: credentialId });
  if (!credential$1)
    return { email: null, recordType: null, recordId: null };
  const crm = new CrmService.default(credential$1, {});
  const userLookupEmail = await crm.findUserEmailFromLookupField(
    attendeeEmail,
    salesforceSettings.rrSKipToAccountLookupFieldName,
    index.SalesforceRecordEnum.ACCOUNT
  );
  return {
    email: userLookupEmail?.email ?? null,
    recordType: index.RoutingReasons.ACCOUNT_LOOKUP_FIELD,
    recordId: null
  };
};
const appBookingFormHandler = {
  salesforce: routingFormBookingFormHandler
};
exports.default = appBookingFormHandler;
