import 'dart:ui';
import 'package:core_localization/src/l10n/strings.g.dart';

import '../../domain/repositories/locale_repository_interface.dart';
import '../datasources/locale_local_data_source.dart';

/// Implementation of [LocaleRepositoryInterface] that coordinates with a local data source.
///
/// This class handles the mapping between domain [Locale] objects and the raw
/// strings handled by the [LocaleLocalDataSource].
class LocaleRepositoryImpl implements LocaleRepositoryInterface {
  /// Creates a [LocaleRepositoryImpl] with the provided [localDataSource].
  LocaleRepositoryImpl({required this.localDataSource});

  final LocaleLocalDataSource localDataSource;

  @override
  Future<void> saveLocale(Locale locale) {
    return localDataSource.saveLanguageCode(locale.languageCode);
  }

  @override
  Future<Locale> getSavedLocale() async {
    return getDefaultLocale();

    /// TODO: FEATURE_NOT_IMPLEMENTED: Implement saved locale retrieval later
    final String? languageCode = await localDataSource.getLanguageCode();
    if (languageCode != null) {
      return Locale(languageCode);
    }
  }

  @override
  Locale getDefaultLocale() {
    final Locale deviceLocale = AppLocaleUtils.findDeviceLocale().flutterLocale;
    if (getSupportedLocales().contains(deviceLocale)) {
      return deviceLocale;
    }
    return const Locale('en');
  }

  @override
  List<Locale> getSupportedLocales() => AppLocaleUtils.supportedLocales;
}
