import 'package:flutter/material.dart';

import 'package:provider/provider.dart';

import 'package:pshared/generated/i18n/ps_localizations.dart';
import 'package:pshared/provider/template.dart';


class ResourceContainer<T extends GenericProvider> extends StatelessWidget {
  final Widget Function(BuildContext context, T provider) builder;
  final Widget? loading;
  final Widget? error;
  final Widget? empty;

  const ResourceContainer({
    required this.builder,
    this.loading,
    this.error,
    this.empty,
  });

  @override
  Widget build(BuildContext context) => Consumer<T>(builder: (context, provider, _) {
    final l10n = PSLocalizations.of(context)!;
    if (provider.isLoading) return loading ?? const Center(child: CircularProgressIndicator());
    if (provider.error != null) return error ?? Text(l10n.resourceLoadError);
    if (provider.items.isEmpty) return empty ?? Text(l10n.resourceEmpty);
    return builder(context, provider);
  });
}
