apply method

Applies the guard. TODO add detailed comments

Implementation

bool apply(
  BuildContext context,
  BeamerDelegate delegate,
  BeamLocation origin,
  List<BeamPage> currentPages,
  BeamLocation target,
) {
  final checkPassed = check(context, target);
  if (checkPassed) {
    return false;
  }

  onCheckFailed?.call(context, target);

  if (showPage != null) {
    final redirectBeamLocation =
        GuardShowPage(target.state.routeInformation, showPage!);
    if (replaceCurrentStack) {
      delegate.beamToReplacement(redirectBeamLocation);
    } else {
      delegate.beamTo(redirectBeamLocation);
    }
    return true;
  }

  // just block navigation
  // revert the configuration of delegate
  if (beamTo == null && beamToNamed == null) {
    delegate.configuration = origin.state.routeInformation;
    return true;
  }

  if (beamTo != null) {
    final redirectBeamLocation = beamTo!(context, origin, target);
    if (redirectBeamLocation.state.routeInformation.uri ==
        target.state.routeInformation.uri) {
      // just block if this will produce an immediate infinite loop
      return true;
    }
    if (redirectBeamLocation.state.routeInformation.uri ==
        origin.state.routeInformation.uri) {
      // just block if redirect is the current route
      return true;
    }
    if (replaceCurrentStack) {
      delegate.beamToReplacement(redirectBeamLocation);
    } else {
      delegate.beamTo(redirectBeamLocation);
    }
    return true;
  }

  if (beamToNamed != null) {
    final redirectNamed = beamToNamed!(origin, target);
    if (redirectNamed == target.state.routeInformation.uri.toString()) {
      // just block if this will produce an immediate infinite loop
      return true;
    }
    if (redirectNamed == origin.state.routeInformation.uri.toString()) {
      // just block if redirect is the current route
      return true;
    }
    if (replaceCurrentStack) {
      delegate.beamToReplacementNamed(redirectNamed);
    } else {
      delegate.beamToNamed(redirectNamed);
    }
    return true;
  }

  return false;
}