function previewRebalance()
        public
        view
        returns (bool result, uint256 directionMask, uint256 amountIn, uint256 amountOut)
    {
        address tokenIn;
        address tokenOut;
        (tokenIn, tokenOut, amountIn) = _getTokenInOut();
        (amountOut, directionMask) = _getQuoteAndDirection(tokenIn, tokenOut, amountIn);
        result = amountOut > amountIn;
    }
function _getQuoteAndDirection(
        address tokenIn,
        address tokenOut,
        uint256 amountIn
    ) internal view returns (uint256 amountOut, uint256 directionMask) {
        (amountOut, , , ) = IQuoter(quoter).quoteExactInputSingleWithPool(
            IQuoter.QuoteExactInputSingleWithPoolParams({
                tokenIn: tokenIn,
                tokenOut: tokenOut,
                amountIn: amountIn,
                fee: fee,
                pool: uniswapPool,
                sqrtPriceLimitX96: 0
            })
        );
        >> directionMask = (tokenIn == weth) ? _BUY_MASK : _SELL_MASK;
    }
uint256 private constant _ONE_FOR_ZERO_MASK = 1 << 255; // Mask for identifying if the swap is one-for-zero
let zeroForOne := eq(and(_pool, _ONE_FOR_ZERO_MASK), 0)
function _getQuoteAndDirection(
    address tokenIn,
    address tokenOut,
    uint256 amountIn
) internal view returns (uint256 amountOut, uint256 directionMask) {
    // Retrieve token0 and token1 from the Uniswap pool
    address token0 = IUniswapV3Pool(uniswapPool).token0();
    address token1 = IUniswapV3Pool(uniswapPool).token1();

    // Call the quoter to get the amountOut
    (amountOut, , , ) = IQuoter(quoter).quoteExactInputSingleWithPool(
        IQuoter.QuoteExactInputSingleWithPoolParams({
            tokenIn: tokenIn,
            tokenOut: tokenOut,
            amountIn: amountIn,
            fee: fee,
            pool: uniswapPool,
            sqrtPriceLimitX96: 0
        })
    );

    // Determine directionMask based on tokenIn position (token0 or token1)
    if (tokenIn == token0) {
        directionMask = _SELL_MASK; // Zero-for-one direction
    } else {
        directionMask = _BUY_MASK; // One-for-zero direction
    }
}
