import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/dependency_injection/get_it.dart';
import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/flavors/flavor_settings.dart';
import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/utils/app_bloc_observer.dart';
import 'package:{{#snakeCase}}{{project_name}}{{/snakeCase}}/core/utils/wyatt_printer.dart';

Future<void> bootstrap(FutureOr<Widget> Function() builder) async {
  await runZonedGuarded(
    () async {
      WidgetsFlutterBinding.ensureInitialized();

      FlutterError.onError = (details) {
        WyattPrinter.get().e(
          '',
          details,
          details.stack,
        );
      };

      FlavorSettings.init();
      GetItInitializer.run();

      GoRouter.setUrlPathStrategy(UrlPathStrategy.path);

      if (!kReleaseMode) {
        final env = FlavorSettings.get();
        WyattPrinter.get().i('Flavor      : ${env.flavor.name}');
      }

      await BlocOverrides.runZoned(
        () async => runApp(await builder()),
        blocObserver: AppBlocObserver(),
      );
    },
    (error, stackTrace) => WyattPrinter.get().e(
      '',
      error,
      stackTrace,
    ),
  );
}
