#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <SDWebImage/SDWebImageDownloaderRequestModifier.h>
#import <SDWebImage/SDWebImageCacheKeyFilter.h>
#import <SDWebImage/SDWebImageDefine.h>
#import <SDWebImage/SDImageLoader.h>

typedef NS_ENUM(NSInteger, FFFPriority) {
    FFFPriorityLow,
    FFFPriorityNormal,
    FFFPriorityHigh
};

typedef NS_ENUM(NSInteger, FFFCacheControl) {
    FFFCacheControlImmutable,
    FFFCacheControlWeb,
    FFFCacheControlCacheOnly
};

// Object containing an image uri and metadata.
@interface FFFastImageSource : NSObject

// uri for image, or base64
@property (nonatomic) NSURL* url;
// priority for image request
@property (nonatomic) FFFPriority priority;
// whether the source set a priority (preload is low priority otherwise)
@property (nonatomic) BOOL hasPriority;
// headers for the image request
@property (nonatomic) NSDictionary *headers;
// cache control mode
@property (nonatomic) FFFCacheControl cacheControl;
// The key to cache the image under instead of its url, or nil.
@property (nonatomic, copy) NSString *cacheKey;
// With several sources, the image's size at this url in pixels (width and
// height × scale), to pick the one closest to the view's size; zero if
// unknown.
@property (nonatomic) CGSize pixelSize;
// With several sources, the scale its width and height are multiplied by
// (1 if not given): an open side follows the image's pixels over it.
@property (nonatomic) CGFloat scale;
// Whether it's a bundled image (require()), whose scale is its file's.
@property (nonatomic) BOOL packagerAsset;
// Whether the decoded image is kept in the memory cache (`memoryCache`,
// default YES). Without it, it's only kept on disk: views don't leave it in
// memory, and preload doesn't decode it.
@property (nonatomic) BOOL memoryCache;
// Whether the image is kept in the disk cache (`diskCache`, default YES).
// Without it, it isn't read from or written to the disk cache, nor for `web`
// images the HTTP cache.
@property (nonatomic) BOOL diskCache;

- (instancetype)initWithURL:(NSURL *)url
                   priority:(FFFPriority)priority
                    headers:(NSDictionary *)headers
               cacheControl:(FFFCacheControl)cacheControl;

// Adds this source's headers to its image requests.
- (SDWebImageDownloaderRequestModifier *)requestModifier;

// Caches the image under cacheKey, or nil without one.
- (SDWebImageCacheKeyFilter *)cacheKeyFilter;

// The load options for `cache`: web follows the HTTP cache, cacheOnly never
// downloads. For views and preload alike.
- (SDWebImageOptions)cacheOptions;

// The loader for `cache: 'web'` images, whose HTTP cache is their own (see
// webURLCache), for photo library images (ph://, see FFFPhotosLoader), or nil
// for SDWebImage's shared one.
- (id<SDImageLoader>)imageLoader;

// Whether the source is a photo library image (ph://<localIdentifier>),
// which is decoded at about the view's size.
- (BOOL)isPhotoLibrary;

// Whether the source is a data uri: its data is the app's own, so `cache`
// doesn't apply (cacheControl is immutable), it's read by a downloader of its
// own (imageLoader), and cached under a hash of the uri (cacheKeyFilter).
- (BOOL)isDataUri;

// The error's message, for onError.
- (NSString *)errorMessage:(NSError *)error;

// A photo library image's own size in pixels (for onLoad when it comes from
// a cache, where the size it was decoded from isn't recorded), or zero.
- (CGSize)photoPixelSize;

// Whether the data is an image FastImage can show: a format SDWebImage
// recognizes (an SVG only if its first element is <svg>), or one ImageIO
// reads that SDWebImage's check doesn't know (AVIF, ICO, ICNS, PSD).
+ (BOOL)isImageData:(NSData *)data;
// The HTTP cache of `cache: 'web'` images: their own rather than the app's
// shared NSURLCache, so clearDiskCache can empty it without the app's other
// responses. 50 MB, as on Android.
+ (NSURLCache *)webURLCache;

// Empties webURLCache (for clearDiskCache). Its removal takes effect a few
// milliseconds after removeAllCachedResponses returns, and NSURLCache doesn't
// say when: until then a `cache: 'web'` load could still get a response from
// before the clear. So each response the web loader stores records the clear
// it came after, and for 5 s after a clear, a load ignores a cached response
// stored before it.
+ (void)clearWebURLCache;

// After a load of this source failed: for a `cache: 'web'` image whose data
// isn't an image, removes the response from the HTTP cache, which stored it as
// it downloaded. Otherwise the next loads would get it from there until it
// expires, and fail again. The web loader already doesn't store responses
// that aren't images (e.g. a captive portal's HTML page); this is for data
// that looks like an image but can't be decoded, or an app's own operation
// class.
- (void)forgetResponseAfterError:(NSError *)error;

@end
