import 'package:aves/model/filters/filters.dart';
import 'package:flutter/widgets.dart';

class EntryUriSetFilter extends CollectionFilter {
  static const type = 'entryUriSet';

  final Set<String> uris;
  late final List<String> _sortedUris;
  late final EntryPredicate _test;

  @override
  List<Object?> get props => [_sortedUris, reversed];

  EntryUriSetFilter(
    this.uris, {
    super.reversed = false,
  }) {
    _sortedUris = uris.toList()..sort();
    _test = (entry) => uris.contains(entry.uri);
  }

  factory EntryUriSetFilter.fromMap(Map<String, dynamic> json) {
    final uriStrings =
        (json['uris'] as List?)?.cast<String>() ?? const <String>[];

    return EntryUriSetFilter(
      uriStrings.toSet(),
      reversed: json['reversed'] ?? false,
    );
  }

  @override
  Map<String, dynamic> toMap() => {
        'type': type,
        'uris': _sortedUris,
        'reversed': reversed,
      };

  @override
  EntryPredicate get positiveTest => _test;

  @override
  bool get exclusiveProp => false;

  @override
  String get universalLabel => 'Map visible entries';

  @override
  String getLabel(BuildContext context) => universalLabel;

  @override
  String get category => type;

  @override
  String get key =>
      '$type-$reversed-${_sortedUris.length}-${_sortedUris.join('|').hashCode}';
}
