"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("./index-QSf6WJj0.cjs");
const index$1 = require("./index-ouzKDQ88.cjs");
const indexBrowser = require("./index-browser-RtwB1hqI.cjs");
const credential = require("./credential-afgv3289.cjs");
const z = require("zod");
const log$2 = index.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: credential2
}) => {
  if (!credential2 || !credential2.key)
    return null;
  const { type: analyticsType } = credential2;
  const analyticsName = analyticsType.split("_")[0];
  const analyticsAppImportFn = index$1.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(credential2);
};
const log$1 = index.logger.getSubLogger({ prefix: ["AnalyticsManager"] });
class AnalyticsManager {
  constructor(credential2) {
    this.analyticsService = null;
    this.credential = credential2;
  }
  async getAnalyticsService(credential2) {
    if (this.analyticsService)
      return this.analyticsService;
    const analyticsService = await getAnalyticsService({ credential: credential2 });
    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.object({
  credentialId: z.number(),
  info: z.object({
    name: z.string(),
    email: z.string(),
    id: z.string(),
    eventName: z.string(),
    externalId: z.string().optional()
  })
});
const log = index.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$1 = await credential.CredentialRepository.findFirstByIdWithKeyAndUser({ id: credentialId });
    if (!credential$1) {
      throw new Error("Invalid credential");
    }
    const manager = new AnalyticsManager(credential$1);
    if (!manager)
      return;
    await manager.sendEvent(info);
  } catch (err) {
    log.error(
      `[Will retry] Error creating analytics event: error: ${indexBrowser.safeStringify(err)} payload: ${indexBrowser.safeStringify({
        payload
      })}`
    );
    throw err;
  }
}
exports.sendAnalyticsEvent = sendAnalyticsEvent;
