class ReshareViaService {
  final _postsVia = <String, ResharedViaData>{};

  ResharedViaData? getForPost(String postId) => _postsVia[postId];

  void upsertResharedVia({required String postId, required String resharerId}) {
    final resharedData = _postsVia.putIfAbsent(
      postId,
      () => ResharedViaData(postId: postId),
    );

    _postsVia[postId] = resharedData.withUpsertedResharer(resharerId);
  }

  void upsertRemovedSharer(
      {required String postId, required String resharerId}) {
    final resharedData = _postsVia.putIfAbsent(
      postId,
      () => ResharedViaData(postId: postId),
    );

    _postsVia[postId] = resharedData.withRemovedResharer(resharerId);
  }
}

class ResharedViaData {
  final String postId;
  final Set<String> resharers;

  bool get hasResharedVia => resharers.isNotEmpty;

  const ResharedViaData({
    required this.postId,
    this.resharers = const {},
  });

  ResharedViaData withUpsertedResharer(String resharerId) => ResharedViaData(
        postId: postId,
        resharers: {resharerId, ...resharers},
      );

  ResharedViaData withRemovedResharer(String resharerId) => ResharedViaData(
        postId: postId,
        resharers: resharers..remove(resharerId),
      );
}
