import { d as defineEventHandler, u as useLogger, w as publicSettings, c as createError, a2 as getMaxLastModifieds, h as handleCache, b as get, a3 as runtimeResolve } from '../../../nitro/nitro.mjs';
import path from 'path';
import { stat, readFile } from 'fs/promises';
import 'luxon';
import '@sequelize/core';
import 'html-to-text';
import 'zod';
import 'crypto';
import 'node:fs';
import 'sharp';
import 'node:stream';
import 'jsdom';
import 'dompurify';
import 'mime-types';
import 'fs';
import 'linkify-html';
import '@peertube/http-signature';
import 'jsonld';
import 'lodash.intersection';
import 'email-templates';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'node:events';
import 'node:buffer';
import 'node:path';
import 'vue';
import 'vue-router';
import 'dotenv';
import 'url';
import 'bcryptjs';
import 'slugify';
import 'umzug';
import 'consola';
import '@sequelize/core/decorators-legacy';
import 'util';
import 'mitt';
import 'ical.js';
import 'node:url';
import 'fast-xml-parser';

const localeStorage = runtimeResolve("../i18n/locales");
const customUserLocaleStorage = get().user_locale;
async function statLocale(storage, locale) {
  return stat(path.resolve(storage, locale + ".json")).catch(() => null);
}
async function getLocale(storage, locale) {
  const localePath = path.resolve(storage, locale + ".json");
  const localeContent = await readFile(localePath, "utf-8");
  return JSON.parse(localeContent);
}
const _locale__get = defineEventHandler(async (event) => {
  const log = useLogger("locale");
  const locale = event.context.params?.locale ?? publicSettings.instance_locale;
  const localeStats = await statLocale(localeStorage, locale);
  if (!localeStats) {
    log.warn(`Requested locale does not exists: ${locale}`);
    throw createError({ status: 404, message: `This locale (${locale}) does not exists!` });
  }
  const customUserLocaleStats = customUserLocaleStorage ? await statLocale(customUserLocaleStorage, locale) : null;
  try {
    const fallbackMessages = await getLocale(localeStorage, "en");
    const fallbackStats = await statLocale(localeStorage, "en");
    const lastModifieds = [localeStats.mtime, fallbackStats.mtime];
    let defaultLocaleMessages;
    if (customUserLocaleStats) {
      lastModifieds.push(customUserLocaleStats.mtime);
      defaultLocaleMessages = Object.assign(
        {},
        fallbackMessages,
        await getLocale(localeStorage, locale),
        await getLocale(customUserLocaleStorage, locale)
      );
    } else {
      defaultLocaleMessages = Object.assign(
        {},
        fallbackMessages,
        await getLocale(localeStorage, locale)
      );
    }
    let $vuetify;
    const vuetifyLocaleStats = await statLocale(localeStorage + "/vuetify/", locale);
    if (vuetifyLocaleStats) {
      $vuetify = await getLocale(localeStorage + "/vuetify/", locale);
      lastModifieds.push(vuetifyLocaleStats?.mtime);
    } else {
      $vuetify = await getLocale(localeStorage + "/vuetify/", "en");
      const vuetifyLocaleStats2 = await statLocale(localeStorage + "/vuetify/", "en");
      lastModifieds.push(vuetifyLocaleStats2.mtime);
    }
    const maxLastModified = getMaxLastModifieds(lastModifieds);
    if (await handleCache(event, maxLastModified, 60)) return;
    return Object.assign(defaultLocaleMessages, { $vuetify });
  } catch (e) {
    throw createError({ status: 400, message: String(e) });
  }
});

export { _locale__get as default };
