import 'package:flutter_mozilla_components/src/pigeons/gecko.g.dart';

final _apiInstance = GeckoPrefApi();

class GeckoPrefService {
  Future<Map<String, Object>> getAllPrefs() {
    return _apiInstance.getPrefs(null);
  }

  Future<Map<String, Object>> getPrefs(List<String> prefs) {
    return _apiInstance.getPrefs(prefs);
  }

  Future<Map<String, Object>> applyPrefs(Map<String, Object> prefs) {
    final buffer = prefs.entries.map((pref) {
      final value = switch (pref.value) {
        final bool x => '$x',
        final int x => '$x',
        final String x => "'$x'", //Use single ticks to allow json
        _ => throw Exception('Unknow pref type'),
      };

      return 'user_pref("${pref.key}", $value);';
    }).join();

    return _apiInstance.applyPrefs(buffer);
  }

  Future<void> resetPrefs(List<String> prefs) {
    return _apiInstance.resetPrefs(prefs);
  }
}
