"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.OxyServicesLanguageMixin = OxyServicesLanguageMixin;
var _languageUtils = require("../../utils/languageUtils");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } /**
 * Language Methods Mixin
 */
function OxyServicesLanguageMixin(Base) {
  return class extends Base {
    constructor(...args) {
      super(...args);
    }
    /**
     * Get appropriate storage for the platform (similar to DeviceManager)
     */
    async getStorage() {
      const isReactNative = typeof navigator !== 'undefined' && navigator.product === 'ReactNative';
      if (isReactNative) {
        try {
          const asyncStorageModule = await Promise.resolve().then(() => _interopRequireWildcard(require('@react-native-async-storage/async-storage')));
          const storage = asyncStorageModule.default;
          return {
            getItem: storage.getItem.bind(storage),
            setItem: storage.setItem.bind(storage),
            removeItem: storage.removeItem.bind(storage)
          };
        } catch (error) {
          console.error('AsyncStorage not available in React Native:', error);
          throw new Error('AsyncStorage is required in React Native environment');
        }
      } else {
        // Use localStorage for web
        return {
          getItem: async key => {
            if (typeof window !== 'undefined' && window.localStorage) {
              return localStorage.getItem(key);
            }
            return null;
          },
          setItem: async (key, value) => {
            if (typeof window !== 'undefined' && window.localStorage) {
              localStorage.setItem(key, value);
            }
          },
          removeItem: async key => {
            if (typeof window !== 'undefined' && window.localStorage) {
              localStorage.removeItem(key);
            }
          }
        };
      }
    }

    /**
     * Get the current language from storage or user profile
     * @param storageKeyPrefix - Optional prefix for storage key (default: 'oxy_session')
     * @returns The current language code (e.g., 'en-US') or null if not set
     */
    async getCurrentLanguage(storageKeyPrefix = 'oxy_session') {
      try {
        // First try to get from user profile if authenticated
        try {
          const user = await this.getCurrentUser();
          const userLanguage = user?.language;
          if (userLanguage) {
            return (0, _languageUtils.normalizeLanguageCode)(userLanguage) || userLanguage;
          }
        } catch (e) {
          // User not authenticated or error, continue to storage
        }

        // Fall back to storage
        const storage = await this.getStorage();
        const storageKey = `${storageKeyPrefix}_language`;
        const storedLanguage = await storage.getItem(storageKey);
        if (storedLanguage) {
          return (0, _languageUtils.normalizeLanguageCode)(storedLanguage) || storedLanguage;
        }
        return null;
      } catch (error) {
        if (__DEV__) {
          console.warn('Failed to get current language:', error);
        }
        return null;
      }
    }

    /**
     * Get the current language with metadata (name, nativeName, etc.)
     * @param storageKeyPrefix - Optional prefix for storage key (default: 'oxy_session')
     * @returns Language metadata object or null if not set
     */
    async getCurrentLanguageMetadata(storageKeyPrefix = 'oxy_session') {
      const languageCode = await this.getCurrentLanguage(storageKeyPrefix);
      return (0, _languageUtils.getLanguageMetadata)(languageCode);
    }

    /**
     * Get the current language name (e.g., 'English')
     * @param storageKeyPrefix - Optional prefix for storage key (default: 'oxy_session')
     * @returns Language name or null if not set
     */
    async getCurrentLanguageName(storageKeyPrefix = 'oxy_session') {
      const languageCode = await this.getCurrentLanguage(storageKeyPrefix);
      if (!languageCode) return null;
      return (0, _languageUtils.getLanguageName)(languageCode);
    }

    /**
     * Get the current native language name (e.g., 'Español')
     * @param storageKeyPrefix - Optional prefix for storage key (default: 'oxy_session')
     * @returns Native language name or null if not set
     */
    async getCurrentNativeLanguageName(storageKeyPrefix = 'oxy_session') {
      const languageCode = await this.getCurrentLanguage(storageKeyPrefix);
      if (!languageCode) return null;
      return (0, _languageUtils.getNativeLanguageName)(languageCode);
    }
  };
}
//# sourceMappingURL=OxyServices.language.js.map