import 'package:flutter/material.dart';


typedef LangLocalization = Map<String, String>;

String _translation(LangLocalization loc, String key) {
  return loc[key] ?? '';
}

typedef Localizations = Map<String, LangLocalization>;

typedef Localizer = String Function(BuildContext);

class Localization {

  static const String keyHint = 'hint';
  static const String keyLink = 'link';
  static const String keyName = 'name';
  static const String keyError = 'error';
  static const String keyAddress = 'address';
  static const String keyDetails = 'details';
  static const String keyRoute = 'route';
  static const String keyLocationName = 'location_name';

  static String _localizeImp(Localizations localizations, String locale, String Function(LangLocalization) functor) {
    final localization = localizations[locale];
    if (localization != null) {
      return functor(localization);
    }
    return '';
  }

  static String _localize(Localizations localizations, String locale, String Function(LangLocalization) functor, {String? fallback}) {
    final res = _localizeImp(localizations, locale, functor);
    return res.isNotEmpty ? res : (fallback ?? '');
  }

  static bool localizationExists(Localizations loc, String locale) {
    return loc.containsKey(locale);
  }

  static String hint(Localizations loc, String locale, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, keyHint), fallback: fallback);
  }

  static String link(Localizations loc, String locale, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, keyLink), fallback: fallback);
  }

  static String name(Localizations loc, String locale, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, keyName), fallback: fallback);
  }

  static String error(Localizations loc, String locale, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, keyError), fallback: fallback);
  }

  static String address(Localizations loc, String locale, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, keyAddress), fallback: fallback);
  }

  static String details(Localizations loc, String locale, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, keyDetails), fallback: fallback);
  }

  static String route(Localizations loc, String locale, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, keyRoute), fallback: fallback);
  }

  static String locationName(Localizations loc, String locale, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, keyLocationName), fallback: fallback);
  }

  static String translate(Localizations loc, String locale, String key, {String? fallback}) {
    return _localize(loc, locale, (localization) => _translation(localization, key), fallback: fallback);
  }
}
