import 'package:design/design.dart';

class MatchParentDelegate extends SingleChildLayoutDelegate {
  @override
  bool shouldRelayout(covariant SingleChildLayoutDelegate oldDelegate) => true;

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    return BoxConstraints(
        minWidth: constraints.maxWidth,
        maxWidth: constraints.maxWidth,
        minHeight: constraints.maxHeight,
        maxHeight: constraints.maxHeight);
  }

  @override
  Size getSize(BoxConstraints constraints) {
    return constraints.biggest;
  }
}

class FillHeightDelegate extends SingleChildLayoutDelegate {
  @override
  bool shouldRelayout(covariant SingleChildLayoutDelegate oldDelegate) => true;

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    return BoxConstraints(
        minHeight: constraints.maxHeight, maxHeight: constraints.maxHeight);
  }

  @override
  Size getSize(BoxConstraints constraints) {
    return Size(clampDouble(0.0, constraints.minWidth, constraints.maxWidth),
        constraints.biggest.height);
  }
}

class FillWidthDelegate extends SingleChildLayoutDelegate {
  FillWidthDelegate();

  @override
  bool shouldRelayout(covariant SingleChildLayoutDelegate oldDelegate) {
    return true;
  }

  @override
  BoxConstraints getConstraintsForChild(BoxConstraints constraints) {
    return BoxConstraints(
        minHeight: constraints.maxHeight, maxHeight: constraints.maxHeight);
  }

  @override
  Size getSize(BoxConstraints constraints) {
    return Size(
      constraints.biggest.width,
      clampDouble(0.0, constraints.minHeight, constraints.maxHeight),
    );
  }
}

class MathParentWidget extends StatelessWidget {
  final Widget? child;

  const MathParentWidget(this.child, {super.key});

  @override
  Widget build(BuildContext context) {
    return CustomSingleChildLayout(
        delegate: MatchParentDelegate(), child: child);
  }
}

class FillHeightWidget extends StatelessWidget {
  final Widget? child;

  const FillHeightWidget(this.child, {super.key});

  @override
  Widget build(BuildContext context) {
    return CustomSingleChildLayout(
        delegate: FillHeightDelegate(), child: child);
  }
}

class FillWidthWidget extends StatelessWidget {
  final Widget? child;

  const FillWidthWidget(this.child, {super.key});

  @override
  Widget build(BuildContext context) {
    return CustomSingleChildLayout(delegate: FillWidthDelegate(), child: child);
  }
}
