"use client";
import { createContext, useContext } from 'react';
import { jsx } from 'react/jsx-runtime';

// src/lib/i18n-context.tsx
var I18nContext = createContext(null);
function I18nProvider({ children, value }) {
  return /* @__PURE__ */ jsx(I18nContext.Provider, { value, children });
}
function useTranslations(namespace) {
  const context = useContext(I18nContext);
  if (!context) {
    const fallback = ((key, params) => {
      let result = namespace ? `${namespace}.${key}` : key;
      if (params) {
        for (const [paramKey, paramValue] of Object.entries(params)) {
          result = result.replace(`{${paramKey}}`, String(paramValue));
        }
      }
      return result;
    });
    fallback.raw = (key) => key;
    return fallback;
  }
  const translateFn = context.translate(namespace);
  if (!translateFn.raw) {
    translateFn.raw = (key) => {
      const messages = context.messages;
      if (!messages) return void 0;
      const fullKey = namespace ? `${namespace}.${key}` : key;
      return getNestedValue(messages, fullKey);
    };
  }
  return translateFn;
}
function getNestedValue(object, path) {
  const parts = path.split(".");
  let current = object;
  for (const part of parts) {
    if (current === null || current === void 0 || typeof current !== "object") return void 0;
    current = current[part];
  }
  return current;
}
function useLocale() {
  const context = useContext(I18nContext);
  return context?.locale ?? "en";
}
function useFormatter() {
  const locale = useLocale();
  return {
    dateTime: (date, options) => new Intl.DateTimeFormat(locale, options).format(date),
    relativeTime: (date) => {
      const now = Date.now();
      const diffMs = date.getTime() - now;
      const diffSeconds = Math.round(diffMs / 1e3);
      const diffMinutes = Math.round(diffMs / 6e4);
      const diffHours = Math.round(diffMs / 36e5);
      const diffDays = Math.round(diffMs / 864e5);
      const rtf = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
      if (Math.abs(diffSeconds) < 60) return rtf.format(diffSeconds, "second");
      if (Math.abs(diffMinutes) < 60) return rtf.format(diffMinutes, "minute");
      if (Math.abs(diffHours) < 24) return rtf.format(diffHours, "hour");
      return rtf.format(diffDays, "day");
    },
    number: (value, options) => new Intl.NumberFormat(locale, options).format(value)
  };
}
function createI18nFromMessages(locale, messages) {
  return {
    locale,
    messages,
    translate: (namespace) => {
      const fn = ((key, params) => {
        const fullKey = namespace ? `${namespace}.${key}` : key;
        const raw = getNestedValue(messages, fullKey);
        let result = typeof raw === "string" ? raw : key;
        if (params) {
          for (const [paramKey, paramValue] of Object.entries(params)) {
            result = result.replace(new RegExp(`\\{${paramKey}\\}`, "g"), String(paramValue));
          }
        }
        return result;
      });
      fn.raw = (key) => {
        const fullKey = namespace ? `${namespace}.${key}` : key;
        return getNestedValue(messages, fullKey);
      };
      return fn;
    }
  };
}

export { I18nProvider, createI18nFromMessages, useFormatter, useLocale, useTranslations };
//# sourceMappingURL=chunk-7VJ7CMMT.mjs.map
//# sourceMappingURL=chunk-7VJ7CMMT.mjs.map