// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

import 'package:flutter/widgets.dart';
import 'package:wyatt_architecture/wyatt_architecture.dart';
import 'package:wyatt_i18n/wyatt_i18n.dart';

typedef LocaleTransformer = String Function(Locale);

/// {@template i18n_data_source}
/// Base class for i18n data sources.
///
/// This class is used to load i18n files from a source.
/// It returns a [Future] that resolves to a [String] containing the i18n file
/// contents.
/// {@endtemplate}
abstract class I18nDataSource extends BaseDataSource {
  /// {@macro i18n_data_source}
  I18nDataSource({
    this.format = Format.arb,
    this.defaultLocale = const Locale('en'),
    this.separator = '.',
    this.localeTransformer,
  });

  /// The format of the i18n file.
  final Format format;

  /// The default locale.
  /// This is used when the locale is `null` in the [load] method.
  Locale defaultLocale;

  /// The separator used in file name.
  /// This is used to separate the baseName and the locale.
  ///
  /// For example, if the baseName is `i18n` and the locale is `en`, if the
  /// separator is `_`, the path will be `i18n_en`.
  final String separator;

  /// The list of parsers used to parse the i18n file.
  /// This is used to parse the i18n file into a [Map].
  // final List<Parser<String, Map<String, dynamic>>> decoders;

  /// The current i18n file loaded from the source.
  /// This is used to avoid loading the same file twice.
  I18n? currentI18nFile;

  /// Function used to transform the [Locale] into a string.
  /// This is used to get the file name from the [Locale].
  ///
  /// For example, if the [Locale] is `en_US`, the file name will be `en_us`.
  ///
  /// But maybe you want to use `en-us` instead. In this case, you can use
  /// ```dart
  /// localeTransformer: (locale) => locale.toString().replaceAll('_', '-'),
  /// ```
  /// to replace the `_` with `-`.
  ///
  /// If you want to use `en` instead of `en_US`, you can use
  /// ```dart
  /// localeTransformer: (locale) => locale.languageCode,
  /// ```
  ///
  /// If this is `null`, the default transformer will be used.
  /// The default transformer will use the LocaleParser serializer method.
  LocaleTransformer? localeTransformer;

  /// Loads the i18n file from the source.
  /// If [locale] is not `null`, it will load the file with the given [locale].
  /// Otherwise, it will load the file from the default location.
  Future<I18n> load({Locale? locale});

  /// Loads the i18n file from the source.
  ///
  /// This method is used to load the i18n file from the given [Uri].
  Future<I18n> loadFrom(Uri uri);
}
