// Query parameter constants
const String _limitParam = 'limit';
const String _offsetParam = 'offset';
const String _archivedParam = 'archived';
const String _cursorParam = 'cursor';

void _addIfNotNull(Map<String, String> params, String key, dynamic value) {
  if (value != null) {
    params[key] = value.toString();
  }
}

void _addIfNotBlank(Map<String, String> params, String key, String? value) {
  final trimmed = value?.trim();
  if (trimmed != null && trimmed.isNotEmpty) {
    params[key] = trimmed;
  }
}

Uri paramsToUri({
  required String path,
  int? limit,
  int? offset,
  bool? fetchArchived,
  Map<String, String>? params,
}) {
  Map<String, String> queryParams = params ?? {};
  _addIfNotNull(queryParams, _limitParam, limit);
  _addIfNotNull(queryParams, _offsetParam, offset);
  _addIfNotNull(queryParams, _archivedParam, fetchArchived);
  
  // Build URL with query parameters using Uri class
  return Uri(
    path: path,
    queryParameters: queryParams.isEmpty ? null : queryParams,
  );
}


String paramsToUriString({
  required String path,
  Map<String, String> queryParams = const {},
  int? limit,
  int? offset,
  bool? fetchArchived,
}) => paramsToUri(path: path, limit: limit, offset: offset, fetchArchived: fetchArchived).toString();

Uri cursorParamsToUri({
  required String path,
  int? limit,
  String? cursor,
  Map<String, String> queryParams = const {},
}) {
  final params = Map<String, String>.from(queryParams);
  _addIfNotNull(params, _limitParam, limit);
  _addIfNotBlank(params, _cursorParam, cursor);
  params.removeWhere((_, value) => value.trim().isEmpty);

  return Uri(
    path: path,
    queryParameters: params.isEmpty ? null : params,
  );
}

String cursorParamsToUriString({
  required String path,
  int? limit,
  String? cursor,
  Map<String, String> queryParams = const {},
}) => cursorParamsToUri(
  path: path,
  limit: limit,
  cursor: cursor,
  queryParams: queryParams,
).toString();
