import { l as logger } from "./user--mpkKOwW.js";
const CrmServiceMap = {
  closecom: import("./CrmService-t8lShWR5.js"),
  hubspot: import("./CrmService-zBthekHB.js"),
  "pipedrive-crm": import("./CrmService-qTR017nB.js"),
  salesforce: import("./CrmService-UuTiY7xM.js"),
  "zoho-bigin": import("./CrmService-XeSSzmHu.js"),
  zohocrm: import("./CrmService-YIiNd5ZS.js")
};
const log$1 = logger.getSubLogger({ prefix: ["CrmManager"] });
const getCrm = async (credential, appOptions) => {
  if (!credential || !credential.key)
    return null;
  const { type: crmType } = credential;
  const crmName = crmType.split("_")[0];
  const crmServiceImportFn = await CrmServiceMap[crmName];
  if (!crmServiceImportFn) {
    log$1.warn(`crm of type ${crmType} is not implemented`);
    return null;
  }
  const CrmService = crmServiceImportFn.default;
  if (!CrmService) {
    log$1.warn(`crm of type ${crmType} is not implemented`);
    return null;
  }
  return new CrmService(credential, appOptions);
};
const getCrm$1 = getCrm;
const log = logger.getSubLogger({ prefix: ["CrmManager"] });
class CrmManager {
  constructor(credential, appOptions) {
    this.crmService = null;
    this.credential = credential;
    this.appOptions = appOptions;
  }
  async getCrmService(credential) {
    if (this.crmService)
      return this.crmService;
    const crmService = await getCrm$1(credential, this.appOptions);
    this.crmService = crmService;
    if (!this.crmService) {
      console.log("💀 Error initializing CRM service");
      log.error("CRM service initialization failed");
    }
    return crmService;
  }
  async createEvent(event) {
    const crmService = await this.getCrmService(this.credential);
    if (!crmService)
      return;
    const { skipContactCreation = false, ignoreGuests = false } = crmService.getAppOptions() || {};
    const eventAttendees = ignoreGuests ? [event.attendees[0]] : event.attendees;
    let contacts = await this.getContacts({ emails: eventAttendees.map((a) => a.email) }) || [];
    if (contacts.length == eventAttendees.length) {
      return await crmService.createEvent(event, contacts);
    }
    if (skipContactCreation)
      return;
    const contactSet = new Set(contacts.map((c) => c.email));
    const contactsToCreate = eventAttendees.filter((attendee) => !contactSet.has(attendee.email));
    const createdContacts = await this.createContacts(
      contactsToCreate,
      event.organizer?.email,
      event.responses
    );
    contacts = contacts.concat(createdContacts);
    return await crmService.createEvent(event, contacts);
  }
  async updateEvent(uid, event) {
    const crmService = await this.getCrmService(this.credential);
    return await crmService?.updateEvent(uid, event);
  }
  async deleteEvent(uid, event) {
    const crmService = await this.getCrmService(this.credential);
    return await crmService?.deleteEvent(uid, event);
  }
  async getContacts(params) {
    const crmService = await this.getCrmService(this.credential);
    const contacts = await crmService?.getContacts(params);
    return contacts;
  }
  async createContacts(contactsToCreate, organizerEmail, calEventResponses) {
    const crmService = await this.getCrmService(this.credential);
    const createdContacts = await crmService?.createContacts(contactsToCreate, organizerEmail, calEventResponses) || [];
    return createdContacts;
  }
  async handleAttendeeNoShow(bookingUid, attendees) {
    const crmService = await this.getCrmService(this.credential);
    if (crmService?.handleAttendeeNoShow) {
      await crmService.handleAttendeeNoShow(bookingUid, attendees);
    }
  }
}
export {
  CrmManager as default
};
