import 'package:flutter/material.dart';

import 'draggable_bottom_sheet.dart';
import 'draggable_bottom_sheet_controller.dart';

class BottomSheetManager {
  final Map<DraggableBottomSheetController, Widget Function()> _sheetBuilders;

  BottomSheetManager(this._sheetBuilders);

  bool get isAnyVisible =>
      _sheetBuilders.keys.any((controller) => controller.isVisible.value);

  void closeFirstVisible() {
    for (final controller in _sheetBuilders.keys) {
      if (controller.isVisible.value) {
        controller.toggle();
        break;
      }
    }
  }

  Widget buildVisibleSheet() {
    for (final entry in _sheetBuilders.entries) {
      if (entry.key.isVisible.value) {
        return _buildDraggableSheet(entry.key, entry.value());
      }
    }
    return const SizedBox.shrink();
  }

  Widget _buildDraggableSheet(
    DraggableBottomSheetController controller,
    Widget child,
  ) {
    return DraggableBottomSheet(
      controller: controller,
      backgroundColor: Colors.white,
      child: child,
    );
  }
}
