import 'dart:convert'; import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:intl/intl.dart'; import 'package:timeago/timeago.dart' as timeago; import 'lat_lng.dart'; import 'place.dart'; import 'uploaded_file.dart'; import '/backend/schema/structs/index.dart'; import '/auth/custom_auth/auth_util.dart'; String? kortDatum(String? rawDatum) { if (rawDatum == null || rawDatum.trim().isEmpty) { return rawDatum; } const weekdagen = { 'maandag': 'ma', 'dinsdag': 'di', 'woensdag': 'wo', 'donderdag': 'do', 'vrijdag': 'vr', 'zaterdag': 'za', 'zondag': 'zo', }; const maandVolledig = { 'januari': 'jan', 'februari': 'feb', 'maart': 'mrt', 'april': 'apr', 'mei': 'mei', 'juni': 'jun', 'juli': 'jul', 'augustus': 'aug', 'september': 'sep', 'oktober': 'okt', 'november': 'nov', 'december': 'dec', }; const maandKort = { 'jan', 'feb', 'mrt', 'apr', 'mei', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec', }; // Alles vanaf de komma (bv. ", 15:00") negeren. final zonderTijd = rawDatum.split(',').first.trim().toLowerCase(); final delen = zonderTijd.split(RegExp(r'\s+')); if (delen.length < 3) { return rawDatum; } final weekdag = weekdagen[delen[0]]; final dag = delen[1]; final maandRuw = delen[2].replaceAll('.', ''); final maand = maandVolledig[maandRuw] ?? (maandKort.contains(maandRuw) ? maandRuw : null); if (weekdag == null || maand == null) { return rawDatum; } final tijdDelen = rawDatum.split(','); final tijd = tijdDelen.length > 1 ? tijdDelen.sublist(1).join(',').trim() : ''; return tijd.isNotEmpty ? '$weekdag $dag $maand $tijd' : '$weekdag $dag $maand'; } String? provincieNaamById( List? lijst, String? id, ) { return lijst ?.firstWhere((e) => e.provincieid == id, orElse: () => ProvincieModelStruct()) .provinciename; } /// id String? gemeenteNaamById( List? lijst, String? id, ) { return lijst ?.firstWhere((e) => e.gemeenteid == id, orElse: () => GemeenteModelStruct()) .gemeentename; } String? favorietenBodyNode(String nid) { return '{"entity_id": ' + nid + ', "entity_type": "node"}'; } String googleMapsUrl( String adres, String plaats, ) { return 'https://www.google.com/maps/search/?api=1&query=' + Uri.encodeComponent('$adres, $plaats'); } List filterHorecagelegenheden( List items, String zoekterm, String? categorieFilter, String titelPad, String categoriePad, ) { final titelKey = titelPad.startsWith(r'$.') ? titelPad.substring(2) : titelPad; final categorieKey = categoriePad.startsWith(r'$.') ? categoriePad.substring(2) : categoriePad; final term = zoekterm .trim() .toLowerCase() .replaceAll('à', 'a') .replaceAll('á', 'a') .replaceAll('ä', 'a') .replaceAll('â', 'a') .replaceAll('è', 'e') .replaceAll('é', 'e') .replaceAll('ë', 'e') .replaceAll('ê', 'e') .replaceAll('ì', 'i') .replaceAll('í', 'i') .replaceAll('ï', 'i') .replaceAll('î', 'i') .replaceAll('ò', 'o') .replaceAll('ó', 'o') .replaceAll('ö', 'o') .replaceAll('ô', 'o') .replaceAll('ù', 'u') .replaceAll('ú', 'u') .replaceAll('ü', 'u') .replaceAll('û', 'u') .replaceAll('ñ', 'n') .replaceAll('ç', 'c'); final catFilter = categorieFilter?.trim(); return items.where((item) { final map = item is Map ? item : null; if (term.isNotEmpty) { final titelRaw = map?[titelKey]?.toString() ?? ''; final titel = titelRaw .toLowerCase() .replaceAll('à', 'a') .replaceAll('á', 'a') .replaceAll('ä', 'a') .replaceAll('â', 'a') .replaceAll('è', 'e') .replaceAll('é', 'e') .replaceAll('ë', 'e') .replaceAll('ê', 'e') .replaceAll('ì', 'i') .replaceAll('í', 'i') .replaceAll('ï', 'i') .replaceAll('î', 'i') .replaceAll('ò', 'o') .replaceAll('ó', 'o') .replaceAll('ö', 'o') .replaceAll('ô', 'o') .replaceAll('ù', 'u') .replaceAll('ú', 'u') .replaceAll('ü', 'u') .replaceAll('û', 'u') .replaceAll('ñ', 'n') .replaceAll('ç', 'c'); if (!titel.contains(term)) { return false; } } if (catFilter != null && catFilter.isNotEmpty) { final categorie = map?[categorieKey]; final lijst = (categorie is List) ? categorie : []; final match = lijst .any((c) => c.toString().toLowerCase() == catFilter.toLowerCase()); if (!match) { return false; } } return true; }).toList(); }