#import "NativeBlurHashBridge.h"

#include "NativeBlurHash.hpp"

@implementation NativeBlurHashBridge

+ (nullable NSData *)decodeBlurHash:(NSString *)blurHash
                              width:(NSInteger)width
                             height:(NSInteger)height
                              punch:(float)punch {
    if (blurHash.length < 6 || width <= 0 || height <= 0) {
        return nil;
    }

    std::vector<std::uint8_t> rgb;
    const bool ok = rosetta::nativeimage::decode_blurhash_to_rgb(
        std::string(blurHash.UTF8String ?: ""),
        static_cast<int>(width),
        static_cast<int>(height),
        punch,
        rgb
    );
    if (!ok) {
        return nil;
    }

    if (rgb.empty()) {
        return [NSData data];
    }
    return [NSData dataWithBytes:rgb.data() length:rgb.size()];
}

@end
