// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IOS_CHROME_BROWSER_SHARED_MODEL_UTILS_WEB_STATE_DEFERRED_EXECUTOR_H_
#define IOS_CHROME_BROWSER_SHARED_MODEL_UTILS_WEB_STATE_DEFERRED_EXECUTOR_H_

#import "base/ios/block_types.h"
#import "ios/web/public/web_state.h"
#import "ios/web/public/web_state_observer_bridge.h"

// Defines a block to be executed once the web state is realized.
using WebStateRealizedCompletionBlock = void (^)(web::WebState*);

// Defines a block to be executed once the web state is loaded, providing a
// boolean indicating success.
using WebStateLoadedCompletionBlock = void (^)(web::WebState*, BOOL);

@protocol WebStateDeferredExecutorDelegate;

// Utilitary to delay execution until the web state is loaded.
@interface WebStateDeferredExecutor : NSObject <CRWWebStateObserver>

// Delegate for the executor.
@property(nonatomic, weak) id<WebStateDeferredExecutorDelegate> delegate;

// Ensures the web state is loaded and executes the given `completion`.
- (void)ensureWebStateIsLoaded:(web::WebState*)webState
                withCompletion:(WebStateLoadedCompletionBlock)completion;

// Ensures the web state is realized and executes the given `completion`.
- (void)ensureWebStateIsRealized:(web::WebState*)webState
                  withCompletion:(WebStateRealizedCompletionBlock)completion;

@end

// Delegate for web state deferred executor.
@protocol WebStateDeferredExecutorDelegate

// Called before the web state is explicitly loaded.
- (void)webStateDeferredExecutor:(WebStateDeferredExecutor*)executor
                willLoadWebState:(web::WebState*)webState;
// Called after the web state is explicitly loaded.
- (void)webStateDeferredExecutor:(WebStateDeferredExecutor*)executor
                 didLoadWebState:(web::WebState*)webState
                         success:(BOOL)success;
// Called after the web state is forced realized.
- (void)webStateDeferredExecutor:(WebStateDeferredExecutor*)executor
        willForceRealizeWebState:(web::WebState*)webState;
// Called after the web state is forced realized.
- (void)webStateDeferredExecutor:(WebStateDeferredExecutor*)executor
         didForceRealizeWebState:(web::WebState*)webState;

@end

#endif  // IOS_CHROME_BROWSER_SHARED_MODEL_UTILS_WEB_STATE_DEFERRED_EXECUTOR_H_
