import { l as logger } from "./index-Cw1zbw54.js";
import { a as appStore } from "./index-m1t7Z5Lc.js";
import { a as safeStringify } from "./index-browser-c8sQ7p7S.js";
import { C as CredentialRepository } from "./credential-2kBaEJXl.js";
import z__default from "zod";
const log$2 = logger.getSubLogger({ prefix: ["AnalyticsManager"] });
const isAnalyticsService = (x) => !!x && typeof x === "object" && "lib" in x && typeof x.lib === "object" && !!x.lib && "AnalyticsService" in x.lib;
const getAnalyticsService = async ({
  credential
}) => {
  if (!credential || !credential.key)
    return null;
  const { type: analyticsType } = credential;
  const analyticsName = analyticsType.split("_")[0];
  const analyticsAppImportFn = appStore[analyticsName];
  if (!analyticsAppImportFn) {
    log$2.warn(`analytics app not implemented`);
    return null;
  }
  const analyticsApp = await analyticsAppImportFn();
  if (!isAnalyticsService(analyticsApp)) {
    log$2.warn(`Analytics is not implemented`);
    return null;
  }
  const AnalyticsService = analyticsApp.lib.AnalyticsService;
  log$2.info("Got analyticsApp", AnalyticsService);
  return new AnalyticsService(credential);
};
const log$1 = logger.getSubLogger({ prefix: ["AnalyticsManager"] });
class AnalyticsManager {
  constructor(credential) {
    this.analyticsService = null;
    this.credential = credential;
  }
  async getAnalyticsService(credential) {
    if (this.analyticsService)
      return this.analyticsService;
    const analyticsService = await getAnalyticsService({ credential });
    this.analyticsService = analyticsService;
    if (!this.analyticsService) {
      log$1.error("Analytics service initialization failed");
    }
    return analyticsService;
  }
  async sendEvent(props) {
    const analyticsService = await this.getAnalyticsService(this.credential);
    if (!analyticsService)
      return;
    return await analyticsService.sendEvent(props);
  }
}
const sendAnalyticsEventSchema = z__default.object({
  credentialId: z__default.number(),
  info: z__default.object({
    name: z__default.string(),
    email: z__default.string(),
    id: z__default.string(),
    eventName: z__default.string(),
    externalId: z__default.string().optional()
  })
});
const log = logger.getSubLogger({ prefix: [`[[tasker] sendAnalyticsEvent]`] });
async function sendAnalyticsEvent(payload) {
  try {
    const parsedPayload = sendAnalyticsEventSchema.safeParse(JSON.parse(payload));
    if (!parsedPayload.success) {
      throw new Error(`malformed payload in sendAnalyticsEvent: ${parsedPayload.error}`);
    }
    const { credentialId, info } = parsedPayload.data;
    const credential = await CredentialRepository.findFirstByIdWithKeyAndUser({ id: credentialId });
    if (!credential) {
      throw new Error("Invalid credential");
    }
    const manager = new AnalyticsManager(credential);
    if (!manager)
      return;
    await manager.sendEvent(info);
  } catch (err) {
    log.error(
      `[Will retry] Error creating analytics event: error: ${safeStringify(err)} payload: ${safeStringify({
        payload
      })}`
    );
    throw err;
  }
}
export {
  sendAnalyticsEvent
};
