"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useLanguageManagement = void 0;
var _react = require("react");
var _languageUtils = require("../../../utils/languageUtils");
var _errorHandlers = require("../utils/errorHandlers");
const DEFAULT_LANGUAGE = 'en-US';

/**
 * Manage UI language state, persistence, and metadata derivation.
 *
 * @param options - Configuration for storage access and error reporting
 */
const useLanguageManagement = ({
  storage,
  languageKey,
  onError,
  logger
}) => {
  const [currentLanguage, setCurrentLanguage] = (0, _react.useState)(DEFAULT_LANGUAGE);
  const loadLanguageFromStorage = (0, _react.useCallback)(async () => {
    if (!storage) {
      return;
    }
    try {
      const savedLanguageRaw = await storage.getItem(languageKey);
      const normalized = (0, _languageUtils.normalizeLanguageCode)(savedLanguageRaw) || savedLanguageRaw;
      if (normalized) {
        setCurrentLanguage(normalized);
      }
    } catch (error) {
      const message = (0, _errorHandlers.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]);
  (0, _react.useEffect)(() => {
    loadLanguageFromStorage().catch(error => {
      if (logger) {
        logger('Unexpected error loading language', error);
      }
    });
  }, [loadLanguageFromStorage, logger]);
  const setLanguage = (0, _react.useCallback)(async languageId => {
    if (!storage) {
      throw new Error('Storage not initialized');
    }
    const normalized = (0, _languageUtils.normalizeLanguageCode)(languageId) || languageId;
    try {
      await storage.setItem(languageKey, normalized);
      setCurrentLanguage(normalized);
    } catch (error) {
      const message = (0, _errorHandlers.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 = (0, _react.useCallback)(async user => {
    if (!storage || !user) {
      return;
    }
    const userLanguage = user?.language;
    if (!userLanguage) {
      return;
    }
    try {
      const normalized = (0, _languageUtils.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 = (0, _react.useMemo)(() => (0, _languageUtils.getLanguageMetadata)(currentLanguage), [currentLanguage]);
  const languageName = (0, _react.useMemo)(() => (0, _languageUtils.getLanguageName)(currentLanguage), [currentLanguage]);
  const nativeLanguageName = (0, _react.useMemo)(() => (0, _languageUtils.getNativeLanguageName)(currentLanguage), [currentLanguage]);
  return {
    currentLanguage,
    metadata,
    languageName,
    nativeLanguageName,
    setLanguage,
    applyLanguagePreference,
    hydrateLanguage: loadLanguageFromStorage
  };
};
exports.useLanguageManagement = useLanguageManagement;
//# sourceMappingURL=useLanguageManagement.js.map