import 'package:shared_preferences/shared_preferences.dart';

/// Interface for the local data source that manages locale persistence.
abstract interface class LocaleLocalDataSource {
  /// Saves the language code to local storage.
  Future<void> saveLanguageCode(String languageCode);

  /// Retrieves the saved language code from local storage.
  Future<String?> getLanguageCode();
}

/// Implementation of [LocaleLocalDataSource] using [SharedPreferencesAsync].
class LocaleLocalDataSourceImpl implements LocaleLocalDataSource {
  static const String _localeKey = 'app_locale';
  final SharedPreferencesAsync _sharedPreferences;

  /// Creates a [LocaleLocalDataSourceImpl] with the required [SharedPreferencesAsync] instance.
  LocaleLocalDataSourceImpl(this._sharedPreferences);

  @override
  Future<void> saveLanguageCode(String languageCode) async {
    await _sharedPreferences.setString(_localeKey, languageCode);
  }

  @override
  Future<String?> getLanguageCode() async {
    return _sharedPreferences.getString(_localeKey);
  }
}
