import 'package:freezed_annotation/freezed_annotation.dart';

part 'loadable_save_context.freezed.dart';
part 'loadable_save_context.g.dart';

@freezed
abstract class LoadableSaveContext with _$LoadableSaveContext {
  const LoadableSaveContext._();
  const factory LoadableSaveContext({required int timestamp}) =
      _LoadableSaveContext;

  factory LoadableSaveContext.fromJson(Map<String, dynamic> json) =>
      _$LoadableSaveContextFromJson(json);

  static String dataKey = 'data';
  static String metaKey = 'meta';

  static Map<String, dynamic> wrap(Map<String, dynamic>? data, int lastFetch) =>
      {
        dataKey: data,
        metaKey: LoadableSaveContext(timestamp: lastFetch).toJson(),
      };

  static ({Map<String, dynamic> data, LoadableSaveContext meta}) unwrap(
    Map<String, dynamic> data,
  ) => (
    data: data[dataKey] as Map<String, dynamic>,
    meta: LoadableSaveContext.fromJson(data[metaKey] as Map<String, dynamic>),
  );
}
