"use strict";

import { useCallback, useEffect, useMemo, useState } from 'react';
import { getLanguageMetadata, getLanguageName, getNativeLanguageName, normalizeLanguageCode } from '../../utils/languageUtils';
import { extractErrorMessage } from '../utils/errorHandlers';
const DEFAULT_LANGUAGE = 'en-US';

/**
 * Manage UI language state, persistence, and metadata derivation.
 *
 * @param options - Configuration for storage access and error reporting
 */
export const useLanguageManagement = ({
  storage,
  languageKey,
  onError,
  logger
}) => {
  const [currentLanguage, setCurrentLanguage] = useState(DEFAULT_LANGUAGE);
  const loadLanguageFromStorage = useCallback(async () => {
    if (!storage) {
      return;
    }
    try {
      const savedLanguageRaw = await storage.getItem(languageKey);
      const normalized = normalizeLanguageCode(savedLanguageRaw) || savedLanguageRaw;
      if (normalized) {
        setCurrentLanguage(normalized);
      }
    } catch (error) {
      const message = extractErrorMessage(error, 'Failed to load language preference');
      onError?.({
        message,
        code: 'LANGUAGE_LOAD_ERROR',
        status: 500
      });
      if (logger) {
        logger(message, error);
      } else if (__DEV__) {
        console.warn('Failed to load language preference:', error);
      }
    }
  }, [languageKey, logger, onError, storage]);
  useEffect(() => {
    loadLanguageFromStorage().catch(error => {
      if (logger) {
        logger('Unexpected error loading language', error);
      }
    });
  }, [loadLanguageFromStorage, logger]);
  const setLanguage = useCallback(async languageId => {
    if (!storage) {
      throw new Error('Storage not initialized');
    }
    const normalized = normalizeLanguageCode(languageId) || languageId;
    try {
      await storage.setItem(languageKey, normalized);
      setCurrentLanguage(normalized);
    } catch (error) {
      const message = extractErrorMessage(error, 'Failed to save language preference');
      onError?.({
        message,
        code: 'LANGUAGE_SAVE_ERROR',
        status: 500
      });
      if (logger) {
        logger(message, error);
      } else if (__DEV__) {
        console.warn('Failed to save language preference:', error);
      }
      throw error instanceof Error ? error : new Error(message);
    }
  }, [languageKey, logger, onError, storage]);
  const applyLanguagePreference = useCallback(async user => {
    if (!storage || !user) {
      return;
    }
    const userLanguage = user?.language;
    if (!userLanguage) {
      return;
    }
    try {
      const normalized = normalizeLanguageCode(userLanguage) || userLanguage;
      await storage.setItem(languageKey, normalized);
      setCurrentLanguage(normalized);
    } catch (error) {
      if (logger) {
        logger('Failed to apply server language preference', error);
      } else if (__DEV__) {
        console.warn('Failed to apply server language preference', error);
      }
    }
  }, [languageKey, logger, storage]);
  const metadata = useMemo(() => getLanguageMetadata(currentLanguage), [currentLanguage]);
  const languageName = useMemo(() => getLanguageName(currentLanguage), [currentLanguage]);
  const nativeLanguageName = useMemo(() => getNativeLanguageName(currentLanguage), [currentLanguage]);
  return {
    currentLanguage,
    metadata,
    languageName,
    nativeLanguageName,
    setLanguage,
    applyLanguagePreference,
    hydrateLanguage: loadLanguageFromStorage
  };
};
//# sourceMappingURL=useLanguageManagement.js.map