import 'dart:convert';
import 'package:drift/drift.dart';

class MapConverter extends TypeConverter<Map<String, dynamic>, String> {
  const MapConverter();

  @override
  Map<String, dynamic> fromSql(String fromDb) {
    if (fromDb.isEmpty) return {};
    try {
      return json.decode(fromDb) as Map<String, dynamic>;
    } catch (_) {
      return {};
    }
  }

  @override
  String toSql(Map<String, dynamic> value) => json.encode(value);
}

class ListConverter extends TypeConverter<List<dynamic>, String> {
  const ListConverter();

  @override
  List<dynamic> fromSql(String fromDb) {
    if (fromDb.isEmpty) return [];
    try {
      return json.decode(fromDb) as List<dynamic>;
    } catch (_) {
      return [];
    }
  }

  @override
  String toSql(List<dynamic> value) => json.encode(value);
}
