import 'package:didvan/services/network/request.dart';
import 'package:didvan/services/network/request_helper.dart';
import 'package:collection/collection.dart';

class ServerDataProvider {
  static final List<MapEntry> directTypes = [];

  static Future<void> getData() async {
    await _getDirectTypes();
  }

  static int labelToTypeId(String label) => label.contains('پشتیبانی')
      ? 7
      : directTypes
              .firstWhereOrNull((element) => element.value.contains(label))
              ?.key ??
          7;

  static Future<void> _getDirectTypes() async {
    final service = RequestService(RequestHelper.directTypes);
    await service.httpGet();
    if (service.isSuccess) {
      final types = service.result['types'];
      for (var i = 0; i < types.length; i++) {
        directTypes.add(MapEntry(types[i]['id'], types[i]['label']));
      }
    } else {
      throw 'Fetching direct types failed!';
    }
  }
}
