import 'package:fis_common/env/env.dart';
import 'package:flutter/foundation.dart';

///服务构建管道函数的声明
typedef ServiceBuilderPipe = Future<void> Function();

/// 服务装载器
class ServiceSetup {
  static bool _installed = false;

  /// 构建装载
  ///
  /// [builder] 全局构建管道
  ///
  /// [webBuilder] 纯web端构建管道
  ///
  /// [shellBuilder] 套壳Web构建管道
  ///
  /// [nativeBuilder] 原生应用构建管道
  Future<void> build({
    required ServiceBuilderPipe builder,
    ServiceBuilderPipe? webBuilder,
    ServiceBuilderPipe? shellBuilder,
    ServiceBuilderPipe? nativeBuilder,
  }) async {
    if (_installed) return;

    await builder();
    if (kIsWeb) {
      if (FPlatform.isPureWeb) {
        if (webBuilder != null) {
          await webBuilder();
        }
      } else {
        if (shellBuilder != null) {
          await shellBuilder();
        }
      }
    } else {
      if (nativeBuilder != null) {
        await nativeBuilder();
      }
    }
    _installed = true;
  }
}
