@Contract("null, _, _, _ -> null")
  private static PsiInstanceOfExpression findInstanceOf(@Nullable PsiExpression condition,
                                                        @NotNull PsiTypeCastExpression cast,
                                                        boolean whenTrue, @Nullable PsiVariable variable) {
    if (condition == null) return null;
    if (condition instanceof PsiParenthesizedExpression parenthesized) {
      return findInstanceOf(parenthesized.getExpression(), cast, whenTrue, variable);
    }
    if (BoolUtils.isNegation(condition)) {
      return findInstanceOf(BoolUtils.getNegated(condition), cast, !whenTrue, variable);
    }
    if (condition instanceof PsiPolyadicExpression polyadic) {
      IElementType tokenType = polyadic.getOperationTokenType();
      if (tokenType == JavaTokenType.ANDAND && whenTrue ||
          tokenType == JavaTokenType.OROR && !whenTrue) {
        for (PsiExpression operand : polyadic.getOperands()) {
          if (PsiTreeUtil.isContextAncestor(operand, cast, false)) return null;
          PsiInstanceOfExpression result = findInstanceOf(operand, cast, whenTrue, variable);
          if (result != null) {
            return result;
          }
        }
      }
    }
    if (condition instanceof PsiInstanceOfExpression instanceOf && whenTrue) {
      PsiTypeElement typeElement = findCheckTypeElement(instanceOf);
      if (typeElement != null) {
        PsiType type = typeElement.getType();
        PsiType castType = Objects.requireNonNull(cast.getCastType()).getType();
        PsiExpression castOperand = Objects.requireNonNull(cast.getOperand());
        if (PsiEquivalenceUtil.areElementsEquivalent(instanceOf.getOperand(), castOperand)) {
          if (typeCompatible(type, castType, castOperand) ||
              PsiUtil.isJvmLocalVariable(variable) && isSafeToNarrowType(variable, cast, type)) {
            return instanceOf;
          }
        }
      }
    }
    return null;
  }
@Contract("null, _, _, _ -> null")
  private static PsiInstanceOfExpression findInstanceOf(@Nullable PsiExpression condition,
                                                        @NotNull PsiTypeCastExpression cast,
                                                        boolean whenTrue, @Nullable PsiVariable variable)