"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("./user-OUg1A5rH.cjs");
const CrmServiceMap = {
  closecom: Promise.resolve().then(() => require("./CrmService-4fwUz8zi.cjs")),
  hubspot: Promise.resolve().then(() => require("./CrmService-ej-pXEOk.cjs")),
  "pipedrive-crm": Promise.resolve().then(() => require("./CrmService-U6oLH0wg.cjs")),
  salesforce: Promise.resolve().then(() => require("./CrmService-wI3OJJ6m.cjs")),
  "zoho-bigin": Promise.resolve().then(() => require("./CrmService-s1dzL0YV.cjs")),
  zohocrm: Promise.resolve().then(() => require("./CrmService-UmWp58Ym.cjs"))
};
const log$1 = index.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 = index.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);
    }
  }
}
exports.default = CrmManager;
