import { z } from "zod";
import { p as prisma, ao as EventTypeAutoTranslatedField, l as logger } from "./user--mpkKOwW.js";
const { i18n } = require("@calcom/config/next-i18next.config");
const locales = i18n.locales;
const localeOptions = locales.map((locale) => ({
  value: locale,
  label: new Intl.DisplayNames(locale, { type: "language" }).of(locale) || ""
}));
localeOptions.find(
  (locale) => locale.value === i18n.defaultLocale
);
class EventTypeTranslationRepository {
  static async upsertManyTitleTranslations(translations) {
    return await Promise.all(
      translations.map(({ userId, ...translation }) => {
        return prisma.eventTypeTranslation.upsert({
          where: {
            eventTypeId_field_targetLocale: {
              eventTypeId: translation.eventTypeId,
              field: EventTypeAutoTranslatedField.TITLE,
              targetLocale: translation.targetLocale
            }
          },
          update: {
            translatedText: translation.translatedText,
            updatedBy: userId
          },
          create: {
            ...translation,
            field: EventTypeAutoTranslatedField.TITLE,
            createdBy: userId
          }
        });
      })
    );
  }
  static async upsertManyDescriptionTranslations(translations) {
    return await Promise.all(
      translations.map(({ userId, ...translation }) => {
        return prisma.eventTypeTranslation.upsert({
          where: {
            eventTypeId_field_targetLocale: {
              eventTypeId: translation.eventTypeId,
              field: EventTypeAutoTranslatedField.DESCRIPTION,
              targetLocale: translation.targetLocale
            }
          },
          update: {
            translatedText: translation.translatedText,
            updatedBy: userId
          },
          create: {
            ...translation,
            field: EventTypeAutoTranslatedField.DESCRIPTION,
            createdBy: userId
          }
        });
      })
    );
  }
}
const ZTranslateEventDataPayloadSchema = z.object({
  eventTypeId: z.number(),
  userId: z.number(),
  description: z.string().nullable().optional(),
  title: z.string().optional(),
  userLocale: z.string()
});
const SUPPORTED_LOCALES = [
  "en",
  // English
  "es",
  // Spanish
  "de",
  // German
  "pt",
  // Portuguese
  "pt-BR",
  // Portuguese Brazilian
  "fr",
  // French
  "it",
  // Italian
  "ar",
  // Arabic
  "ru",
  // Russian
  "zh-CN",
  // Simplified Chinese
  "nl",
  // Dutch
  "zh-TW",
  // Traditional Chinese
  "ko",
  // Korean
  "ja",
  // Japanese
  "sv",
  // Swedish
  "da"
  // Danish
];
async function processTranslations({
  text,
  userLocale,
  eventTypeId,
  userId,
  field
}) {
  const { LingoDotDevService } = await import("./lingoDotDev-l8aWNbCU.js");
  try {
    const targetLocales = SUPPORTED_LOCALES.filter(
      (locale) => locale !== userLocale && locales.includes(locale)
    );
    const translations = await Promise.all(
      targetLocales.map((targetLocale) => LingoDotDevService.localizeText(text, userLocale, targetLocale))
    );
    const validTranslations = translations.filter((trans) => trans !== null).map((trans, index) => ({
      translatedText: trans,
      targetLocale: targetLocales[index]
    }));
    if (validTranslations.length > 0) {
      const translationData = validTranslations.map(({ translatedText, targetLocale }) => ({
        eventTypeId,
        sourceLocale: userLocale,
        targetLocale,
        translatedText,
        userId
      }));
      const upsertMany = field === EventTypeAutoTranslatedField.DESCRIPTION ? EventTypeTranslationRepository.upsertManyDescriptionTranslations : EventTypeTranslationRepository.upsertManyTitleTranslations;
      await upsertMany(translationData);
    }
  } catch (error) {
    logger.error(`Failed to process ${field} translations:`, error);
  }
}
async function translateEventTypeData(payload) {
  const { eventTypeId, description, title, userLocale, userId } = ZTranslateEventDataPayloadSchema.parse(
    JSON.parse(payload)
  );
  await Promise.all([
    description && processTranslations({
      text: description,
      userLocale,
      eventTypeId,
      userId,
      field: EventTypeAutoTranslatedField.DESCRIPTION
    }),
    title && processTranslations({
      text: title,
      userLocale,
      eventTypeId,
      userId,
      field: EventTypeAutoTranslatedField.TITLE
    })
  ]);
}
export {
  ZTranslateEventDataPayloadSchema,
  translateEventTypeData
};
