import "dart:convert";
import "dart:io";

import "package:csv/csv.dart";
import "package:shared/data.dart";
export "package:shared/data.dart";

extension type TripID(String id) {
  TripID.fromJson(dynamic value) : id = value.toString();
}

extension DirectoryUtils on Directory {
  String operator /(String child) => "$path/$child";
}

extension MapListUtils<K, V> on Map<K, List<V>> {
  void addToList(K key, V value) {
    this[key] ??= [];
    this[key]!.add(value);
  }
}

extension MapSetUtils<K, V> on Map<K, Set<V>> {
  void addToSet(K key, V value) {
    this[key] ??= {};
    this[key]!.add(value);
  }
}

abstract class Parser<T> {
  Future<Iterable<T>> parse();
}

final csvConverter = CsvCodec(shouldParseNumbers: false).decoder;
Future<Iterable<CsvRow>> readCsv(File file) async {
  final contents = await file.readAsString();
  final csv = csvConverter.convert(contents);
  return csv.skip(1).map<CsvRow>((row) => row.cast<String>());
}

Future<List<Json>> readJson(File file) async {
  final contents = await file.readAsString();
  final json = jsonDecode(contents);
  return (json as List).cast<Json>();
}

final serverDir = Directory("../server");
final _dataDir = Directory(serverDir / "data");
final occtDataDir = Directory(_dataDir / "OCCT");
final bcDataDir = Directory(_dataDir / "BCT");
