//
//  RNFSTurboHostObject.cpp
//  react-native-fs-turbo
//
//  Created by Sergei Kazakov on 03.08.24.
//

#include "RNFSTurboHostObject.h"
#include "RNFSTurboLogger.h"

namespace cmpayc::rnfsturbo {

using namespace facebook;
namespace fs = std::filesystem;
 
RNFSTurboHostObject::RNFSTurboHostObject(std::shared_ptr<react::CallInvoker> jsInvoker) {
  RNFSTurboLogger::log("RNFSTurbo", "Initializing RNFSTurbo");
  _jsInvoker = jsInvoker;
#ifdef __ANDROID__
  JNIEnv *env = facebook::jni::Environment::current();
  platformHelper = new RNFSTurboPlatformHelper(env);
#endif
#ifdef __APPLE__
  platformHelper = new RNFSTurboPlatformHelper();
#endif

}

RNFSTurboHostObject::~RNFSTurboHostObject() {
  delete platformHelper;
  platformHelper = nullptr;
}

std::vector<jsi::PropNameID> RNFSTurboHostObject::getPropertyNames(jsi::Runtime& rt) {
  return jsi::PropNameID::names(rt, "readDir", "readDirAssets", "readdir", "stat", "readFile", "read", "readFileAssets", "readFileRes", "writeFile", "appendFile", "write", "moveFile", "copyFolder", "copyFile", "copyFileAssets", "copyFileRes", "copyAssetsFileIOS", "copyAssetsVideoIOS", "unlink" "exists", "existsAssets", "existsRes", "hash", "touch", "mkdir", "downloadFile", "stopDownload", "resumeDownload", "isResumable", "completeHandlerIOS", "uploadFiles", "stopUpload", "getFSInfo", "scanFile", "getAllExternalFilesDirs", "pathForGroup");
}

jsi::Value RNFSTurboHostObject::get(jsi::Runtime& runtime, const jsi::PropNameID& propNameId) {
  std::string propName = propNameId.utf8(runtime);

  RNFSTurboLogger::log("RNFSTurbo", "call func %s...", propName.c_str());
  
  if (propName == "stat") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "stat", "First argument ('filepath') has to be of type string"));
        }
        if (count > 2) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "stat", "Too many arguments"));
        }

        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));

        bool isNewFormat{false};
        if (count > 1 && arguments[1].isBool()) {
          isNewFormat = arguments[1].asBool();
        }

        struct stat t_stat;
        int res = stat(filePath.c_str(), &t_stat);
        if (res < 0) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", filePath.c_str(), "stat", strerror(errno)));
        }

        jsi::Object obj = jsi::Object(runtime);
        obj.setProperty(runtime, "path", jsi::String::createFromUtf8(runtime, filePath));
        obj.setProperty(runtime, "ctime", jsi::Value(static_cast<double>(t_stat.st_ctime)));
        obj.setProperty(runtime, "mtime", jsi::Value(static_cast<double>(t_stat.st_mtime)));
        obj.setProperty(runtime, "size", jsi::Value(static_cast<double>(t_stat.st_size)));
        obj.setProperty(runtime, "mode", jsi::Value(static_cast<int>(t_stat.st_mode)));
        obj.setProperty(runtime, "originalFilepath", jsi::String::createFromUtf8(runtime, filePath));
        if (isNewFormat) {
          obj.setProperty(runtime, "isDirectory", jsi::Value(static_cast<bool>(S_ISDIR(t_stat.st_mode))));
        } else {
          obj.setProperty(runtime, "isDirectory", jsi::Function::createFromHostFunction(
            runtime,
            jsi::PropNameID::forAscii(runtime, "isDirectory"),
            0,
            [t_stat](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
              return jsi::Value(static_cast<bool>(S_ISDIR(t_stat.st_mode)));
            }
          ));
        }
        if (isNewFormat) {
          obj.setProperty(runtime, "isFile", jsi::Value(static_cast<bool>(S_ISREG(t_stat.st_mode))));
        } else {
          obj.setProperty(runtime, "isFile", jsi::Function::createFromHostFunction(
            runtime,
            jsi::PropNameID::forAscii(runtime, "isFile"),
            0,
            [t_stat](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
              return jsi::Value(static_cast<bool>(S_ISREG(t_stat.st_mode)));
            }
          ));
        }
        
        return obj;
      }
    );
  }
  if (propName == "readDir" || propName == "readDirAssets" || propName == "readdir") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this, propName](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifndef __ANDROID__
        if (propName == "readDirAssets") {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "readDirAssets", "Command only for Android"));
        }
#endif
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "First argument ('filepath') has to be of type string"));
        }
        if (count > 2) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Too many arguments"));
        }
        bool isAndroidAssets = propName == "readDirAssets";

        std::string dirPath = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        bool isNewFormat{false};
        if (count > 1 && arguments[1].isBool()) {
          isNewFormat = arguments[1].asBool();
        }
        
        struct dirent *dent = nullptr;
        struct stat t_stat;
        int len{0};
        DIR *dir = nullptr;
        std::vector<ReadDirItem> assetsDirItems;
        
        if (isAndroidAssets) {
#ifdef __ANDROID__
          try {
            assetsDirItems = platformHelper->readDirAssets(dirPath.c_str());
            len = assetsDirItems.size();
          } catch (const char *error_message) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", dirPath.c_str(), propName.c_str(), error_message));
          }
#endif
        } else {
          dir = opendir(dirPath.c_str());
          if (dir == NULL){
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", dirPath.c_str(), propName.c_str(), strerror(errno)));
          }
          
          while ((dent = readdir(dir)) != NULL){
            if (std::strcmp(dent->d_name, ".") != 0 && std::strcmp(dent->d_name, "..") != 0) {
              len++;
            }
          }
          if (len < 1) {
            closedir(dir);
            jsi::Array arr = jsi::Array(runtime, 0);
            return arr;
          }
          rewinddir(dir);
        }
        std::vector<ReadDirItem>::iterator assetsDirItemsIt = assetsDirItems.begin();
        
        jsi::Array arr = jsi::Array(runtime, len);
        len = 0;
        while (true) {
          if (isAndroidAssets) {
            if (assetsDirItemsIt == end(assetsDirItems)) {
              break;
            }
          } else {
            dent = readdir(dir);
            if (dent == NULL) {
              break;
            }
            if (std::strcmp(dent->d_name, ".") == 0 || std::strcmp(dent->d_name, "..") == 0) {
              continue;
            }
          }
          if (propName == "readdir") {
            arr.setValueAtIndex(
              runtime,
              len,
              jsi::String::createFromUtf8(runtime, dent->d_name)
            );
          } else {
            std::string absolutePath(dirPath);
            if (isAndroidAssets) {
              absolutePath = assetsDirItemsIt->path;
            } else {
              absolutePath.append("/");
              absolutePath.append(dent->d_name);
              stat(absolutePath.c_str(), &t_stat);
            }
            jsi::Object obj = jsi::Object(runtime);
            obj.setProperty(runtime, "path", jsi::String::createFromUtf8(runtime, absolutePath));
            obj.setProperty(
              runtime,
              "ctime",
              jsi::Value(static_cast<double>(isAndroidAssets ? 0 : t_stat.st_ctime))
            );
            obj.setProperty(
              runtime,
              "mtime",
              jsi::Value(static_cast<double>(isAndroidAssets ? 0 : t_stat.st_mtime))
            );
            obj.setProperty(
              runtime,
              "size",
              jsi::Value(static_cast<double>(isAndroidAssets ? assetsDirItemsIt->size : t_stat.st_size))
            );
            if (isNewFormat) {
              obj.setProperty(
                runtime,
                "mode",
                jsi::Value(static_cast<int>(isAndroidAssets ? 0 : t_stat.st_mode))
              );
            }
            obj.setProperty(
              runtime,
              "name",
              jsi::String::createFromUtf8(runtime, isAndroidAssets ? assetsDirItemsIt->name : dent->d_name)
            );
            bool isDirectory = isAndroidAssets ? assetsDirItemsIt->isDirectory : S_ISDIR(t_stat.st_mode);
            if (isNewFormat) {
              obj.setProperty(runtime, "isDirectory", jsi::Value(isDirectory));
            } else {
              obj.setProperty(runtime, "isDirectory", jsi::Function::createFromHostFunction(
                runtime,
                jsi::PropNameID::forAscii(runtime, "isDirectory"),
                0,
                [isDirectory](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
                  return jsi::Value(isDirectory);
                }
              ));
            }
            bool isFile = isAndroidAssets ? !assetsDirItemsIt->isDirectory : S_ISREG(t_stat.st_mode);
            if (isNewFormat) {
              obj.setProperty(runtime, "isFile", jsi::Value(isFile));
            } else {
              obj.setProperty(runtime, "isFile", jsi::Function::createFromHostFunction(
                runtime,
                jsi::PropNameID::forAscii(runtime, "isFile"),
                0,
                [isFile](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args, size_t count) {
                  return jsi::Value(isFile);
                }
              ));
            }
            
            arr.setValueAtIndex(runtime, len, std::move(obj));
          }
          if (isAndroidAssets) {
            assetsDirItemsIt++;
          }
          len++;
        }
        if (propName != "readDirAssets") {
          closedir(dir);
        }

        return arr;
      }
    );
  }
  if (propName == "readFile" || propName == "readFileAssets" || propName == "readFileRes" || propName == "read") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this, propName](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifndef __ANDROID__
        if (propName == "readFileAssets" || propName == "readFileRes") {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Command only for Android"));
        }
#endif
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "First argument ('filepath') has to be of type string"));
        }
        if ((propName == "read" && count > 4) || (propName != "read" && count > 2)) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Too many arguments"));
        }

        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        long length{0};
        if (propName == "read" && count > 1 && arguments[1].isNumber()) {
          length = arguments[1].asNumber();
        }
        long offset{0};
        if (propName == "read" && count > 2 && arguments[2].isNumber()) {
          offset = arguments[2].asNumber();
        }
        std::string encoding{"utf8"};
#ifdef RNFSTURBO_USE_ENCRYPTION
        bool encrypted{false};
        int passphraseLength{0};
        int ivLength{0};
        std::vector<unsigned char> passphrase;
        std::vector<unsigned char> iv;
        std::string mode{"ecb"};
        std::string padding{"pkcs5/pkcs7"};
#endif
        int optionsIndex{-1};
        bool optionsIsObject{false};
        if (propName == "read" && count == 4 && arguments[3].isString()) {
          optionsIndex = 3;
        } else if (propName != "read" && count == 2 && arguments[1].isString()) {
          optionsIndex = 1;
        } else if (propName == "read" && count == 4 && arguments[3].isObject()) {
          optionsIndex = 3;
          optionsIsObject = true;
        } else if (propName != "read" && count == 2 && arguments[1].isObject()) {
          optionsIndex = 1;
          optionsIsObject = true;
        }
        if (optionsIndex > -1 && !optionsIsObject) {
          encoding = arguments[optionsIndex].asString(runtime).utf8(runtime);
        } else if (optionsIndex > -1 && optionsIsObject) {
          auto optionsObject = arguments[optionsIndex].asObject(runtime);
          if (optionsObject.hasProperty(runtime, "encoding")) {
            auto encodingOption = optionsObject.getProperty(runtime, "encoding");
            if (encodingOption.isString()) {
              encoding = encodingOption.asString(runtime).utf8(runtime);
            }
          }
#ifdef RNFSTURBO_USE_ENCRYPTION
          processEncryptionOptions(
            runtime,
            propName,
            optionsObject,
            encrypted,
            passphraseLength,
            passphrase,
            ivLength,
            iv,
            mode,
            padding
          );
#endif
        }
        if (
          encoding != "utf8" &&
          encoding != "base64" &&
          encoding != "uint8" &&
          encoding != "uint16" &&
          encoding != "uint32" &&
          encoding != "float32" &&
          encoding != "ascii"
        ) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", propName.c_str(), "Wrong encoding", encoding.c_str()));
        }
#ifdef RNFSTURBO_USE_ENCRYPTION
        if (encrypted && passphraseLength == 0) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Passphrase is required for encryption"));
        }
#endif

        try {
          if (propName == "readFileAssets" || propName == "readFileRes") {
#ifdef __ANDROID__
            std::string resultString = platformHelper->readFileAssetsOrRes(filePath.c_str(), propName == "readFileRes");
            return encoding == "ascii"
              ? jsi::String::createFromAscii(runtime, resultString)
              : jsi::String::createFromUtf8(
                  runtime,
                  encoding == "base64" ? base64::to_base64(resultString) : resultString
              );
#endif
          } else {
            if (encoding == "uint8" || encoding == "uint16" || encoding == "uint32") {
              FileBuffer buffer = readFileUintUniversal(encoding, filePath.c_str(), (int)offset, (int)length);
              jsi::Array res = std::visit([&](auto&& vec) -> jsi::Array {
                jsi::Array arr(runtime, vec.size());
                for (size_t i = 0; i < vec.size(); ++i) {
                  arr.setValueAtIndex(
                    runtime,
                    i,
                    jsi::Value(static_cast<double>(vec[i]))
                  );
                }
                return arr;
              }, buffer);
              return res;
            } else if (encoding == "float32") {
              std::vector<float> buffer = readFileFloat32(filePath.c_str(), (int)offset, (int)length);
              jsi::Array res = jsi::Array(runtime, buffer.size());
              int len = 0;
              for (const float i : buffer) {
                res.setValueAtIndex(
                  runtime,
                  len,
                  jsi::Value(i)
                );
                len++;
              }
              return res;
            } else {
              std::string buffer = readFile(filePath.c_str(), (int)offset, (int)length);
#ifdef RNFSTURBO_USE_ENCRYPTION
              if (encrypted) {
                auto krypt = createCipherMode(
                  runtime,
                  propName,
                  mode,
                  padding,
                  passphrase.data(),
                  passphrase.size()
                );
                ByteArray decryptedBytes = krypt->decrypt(
                  reinterpret_cast<unsigned char*>(buffer.data()),
                  buffer.size(),
                  iv.data()
                );

                std::string decryptedContent(
                  reinterpret_cast<const char*>(decryptedBytes.array),
                  decryptedBytes.length
                );
                return encoding == "ascii"
                  ? jsi::String::createFromAscii(runtime, decryptedContent)
                  : jsi::String::createFromUtf8(
                      runtime,
                      encoding == "base64" ? base64::to_base64(decryptedContent) : decryptedContent
                  );
              } else {
#endif
                return encoding == "ascii"
                  ? jsi::String::createFromAscii(runtime, buffer)
                  : jsi::String::createFromUtf8(
                      runtime,
                      encoding == "base64" ? base64::to_base64(buffer) : buffer
                  );
#ifdef RNFSTURBO_USE_ENCRYPTION
              }
#endif
            }
          }
        } catch (const char *error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", filePath.c_str(), propName.c_str(), error_message));
        } catch (std::exception const& e) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", filePath.c_str(), propName.c_str(), e.what()));
        }

        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "writeFile" || propName == "appendFile" || propName == "write") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this, propName](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "First argument ('filepath') has to be of type string"));
        }
        std::string encoding{"utf8"};
        std::map<std::string, std::string> options;
        int optionsIndex{-1};
        bool optionsIsObject{false};
#ifdef RNFSTURBO_USE_ENCRYPTION
        bool encrypted{false};
        std::string mode{"ecb"};
        std::string padding{"pkcs5/pkcs7"};
        int passphraseLength{0};
        int ivLength{0};
        std::vector<unsigned char> passphrase;
        std::vector<unsigned char> iv;
#endif
        if (propName == "write" && count == 4 && arguments[3].isString()) {
          optionsIndex = 3;
        } else if (propName != "write" && count == 3 && arguments[2].isString()) {
          optionsIndex = 2;
        } else if (propName == "write" && count == 4 && arguments[3].isObject()) {
          optionsIndex = 3;
          optionsIsObject = true;
        } else if (propName != "write" && count == 3 && arguments[2].isObject()) {
          optionsIndex = 2;
          optionsIsObject = true;
        }
        if (optionsIndex > -1 && !optionsIsObject) {
          encoding = arguments[optionsIndex].asString(runtime).utf8(runtime);
        } else if (optionsIndex > -1 && optionsIsObject) {
          auto optionsObject = arguments[optionsIndex].asObject(runtime);
          if (optionsObject.hasProperty(runtime, "encoding")) {
            auto encodingOption = optionsObject.getProperty(runtime, "encoding");
            if (encodingOption.isString()) {
              encoding = encodingOption.asString(runtime).utf8(runtime);
            }
          }
#ifdef RNFSTURBO_USE_ENCRYPTION
          processEncryptionOptions(
            runtime,
            propName,
            optionsObject,
            encrypted,
            passphraseLength,
            passphrase,
            ivLength,
            iv,
            mode,
            padding
          );
#endif
#ifdef __APPLE__
          if (optionsObject.hasProperty(runtime, "NSFileProtectionKey")) {
            auto protectionOption = optionsObject.getProperty(runtime, "NSFileProtectionKey");
            if (protectionOption.isString()) {
              options["NSFileProtectionKey"] = protectionOption.asString(runtime).utf8(runtime);
            }
          }
#endif
        }
        if (
          encoding != "utf8" &&
          encoding != "base64" &&
          encoding != "uint8" &&
          encoding != "uint16" &&
          encoding != "uint32" &&
          encoding != "float32" &&
          encoding != "ascii"
        ) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", propName.c_str(), "Wrong encoding", encoding.c_str()));
        }
        if (
          (
            encoding == "uint8" ||
            encoding == "uint16" ||
            encoding == "uint32" ||
            encoding == "float32"
          ) && (count < 2 || !arguments[1].isObject())
        ) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Second argument ('content') has to be of type number[]"));
        } else if (
          encoding != "uint8" &&
          encoding != "uint16" &&
          encoding != "uint32" &&
          encoding != "float32" &&
          (count < 2 || !arguments[1].isString())
        ) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Second argument ('content') has to be of type string"));
        }
        if ((propName == "write" && count > 4) || (propName != "write" && count > 3)) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Too many arguments"));
        }
#ifdef RNFSTURBO_USE_ENCRYPTION
        if (encrypted && passphraseLength == 0) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Passphrase is required for encryption"));
        }
#endif
        int offset{-1};
        if (propName == "write" && count > 2 && arguments[2].isNumber()) {
          offset = arguments[2].asNumber();
        }
        if (offset < -1) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Negative offset"));
        }

        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));

        std::string content{""};
        uint8_t *contentArrUint8 = nullptr;
        uint16_t *contentArrUint16 = nullptr;
        uint32_t *contentArrUint32 = nullptr;
        float *contentArrFloat32 = nullptr;
        int contentLength{0};
        if (encoding == "uint8" || encoding == "uint16" || encoding == "uint32" || encoding == "float32") {
          jsi::Array jsiArr = arguments[1].asObject(runtime).asArray(runtime);
          if (!jsiArr.isArray(runtime)) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Second argument ('content') has to be of type number[]"));
          }
          if (encoding == "float32") {
            contentArrFloat32 = new float[jsiArr.size(runtime)];
            contentLength = (int) jsiArr.size(runtime) * sizeof(float);
          } else if (encoding == "uint8") {
            contentArrUint8 = new uint8_t[jsiArr.size(runtime)];
            contentLength = (int) jsiArr.size(runtime) * sizeof(uint8_t);
          } else if (encoding == "uint16") {
            contentArrUint16 = new uint16_t[jsiArr.size(runtime)];
            contentLength = (int) jsiArr.size(runtime) * sizeof(uint16_t);
          } else if (encoding == "uint32") {
            contentArrUint32 = new uint32_t[jsiArr.size(runtime)];
            contentLength = (int) jsiArr.size(runtime) * sizeof(uint32_t);
          }
          for (int i = 0; i < jsiArr.size(runtime); i++) {
            if (!jsiArr.getValueAtIndex(runtime, i).isNumber()) {
              throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Every element of content array has to be of type number"));
            }
            if (encoding == "float32") {
              contentArrFloat32[i] = (float) jsiArr.getValueAtIndex(runtime, i).asNumber();
            } else if (encoding == "uint8") {
              contentArrUint8[i] = (uint8_t) jsiArr.getValueAtIndex(runtime, i).asNumber();
            } else if (encoding == "uint16") {
              contentArrUint16[i] = fromBigEndian((uint16_t) jsiArr.getValueAtIndex(runtime, i).asNumber());
            } else if (encoding == "uint32") {
              contentArrUint32[i] = fromBigEndian((uint32_t) jsiArr.getValueAtIndex(runtime, i).asNumber());
            }
          }
        } else {
          content = arguments[1].asString(runtime).utf8(runtime);
          if (encoding == "base64") {
            content = base64::from_base64(content);
          }
        }
        
        try {
          struct stat t_stat;
          bool fileExists = stat(filePath.c_str(), &t_stat) >= 0;
          if (encoding == "uint8" || encoding == "uint16" || encoding == "uint32") {
            if (fileExists && propName == "write" && offset > -1 && encoding == "uint8") {
              std::string replaceString(
                reinterpret_cast<char*>(contentArrUint8),
                contentLength
              );
              writeWithOffset(filePath.c_str(), replaceString, offset);
            } else if (fileExists && propName == "write" && offset > -1 && encoding == "uint16") {
              std::string replaceString(
                reinterpret_cast<char*>(contentArrUint16),
                contentLength
              );
              writeWithOffset(filePath.c_str(), replaceString, offset);
            } else if (fileExists && propName == "write" && offset > -1 && encoding == "uint32") {
              std::string replaceString(
                reinterpret_cast<char*>(contentArrUint32),
                contentLength
              );
              writeWithOffset(filePath.c_str(), replaceString, offset);
            } else {
              writeFileUintUniversal(
                encoding,
                filePath.c_str(),
                contentArrUint8,
                contentArrUint16,
                contentArrUint32,
                contentLength,
                fileExists && (propName == "appendFile" || (propName == "write" && offset == -1))
              );
            }
          } else if (encoding == "float32") {
            if (fileExists && propName == "write" && offset > -1) {
              std::string replaceString(
                reinterpret_cast<char*>(contentArrFloat32),
                contentLength
              );
              writeWithOffset(filePath.c_str(), replaceString, offset);
            } else {
              writeFileFloat32(
                filePath.c_str(),
                contentArrFloat32,
                contentLength,
                fileExists && (propName == "appendFile" || (propName == "write" && offset == -1))
              );
            }
          } else {
            if (fileExists && propName == "write" && offset > -1) {
              writeWithOffset(
                filePath.c_str(),
                content,
                offset
              );
#ifdef RNFSTURBO_USE_ENCRYPTION
            } else if (encrypted) {
              auto krypt = createCipherMode(
                runtime,
                propName,
                mode,
                padding,
                passphrase.data(),
                passphrase.size()
              );
              ByteArray cipher = krypt->encrypt(
                reinterpret_cast<unsigned char*>(content.data()),
                content.size(),
                iv.data()
              );
              
              std::string encryptedContent(
                reinterpret_cast<const char*>(cipher.array),
                cipher.length
              );
              writeFile(
                filePath.c_str(),
                encryptedContent,
                fileExists && (propName == "appendFile" || (propName == "write" && offset == -1))
              );
#endif
            } else {
              writeFile(
                filePath.c_str(),
                content,
                fileExists && (propName == "appendFile" || (propName == "write" && offset == -1))
              );
            }
          }
#ifdef __APPLE__
          if (options.count("NSFileProtectionKey") > 0) {
            platformHelper->setResourceValue(
              filePath.c_str(),
              "NSFileProtectionKey",
              options["NSFileProtectionKey"].c_str()
            );
          }
#endif
        } catch (const char *error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", filePath.c_str(), propName.c_str(), error_message));
        } catch (std::exception const& e) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", filePath.c_str(), propName.c_str(), e.what()));
        }
        if (contentArrUint8 != nullptr) {
          delete[] contentArrUint8;
        }
        if (contentArrUint16 != nullptr) {
          delete[] contentArrUint16;
        }
        if (contentArrUint32 != nullptr) {
          delete[] contentArrUint32;
        }
        if (contentArrFloat32 != nullptr) {
          delete[] contentArrFloat32;
        }

        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "moveFile") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "moveFile", "First argument ('filepath') has to be of type string"));
        }
        if (count < 2 || !arguments[1].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "moveFile", "Second argument ('destPath') has to be of type string"));
        }

        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        std::string destPath = cleanPath(arguments[1].asString(runtime).utf8(runtime));
        
        std::map<std::string, std::string> options;
        if (count == 3 && arguments[2].isObject()) {
          auto optionsObject = arguments[2].asObject(runtime);
#ifdef __APPLE__
          if (optionsObject.hasProperty(runtime, "NSFileProtectionKey")) {
            auto protectionOption = optionsObject.getProperty(runtime, "NSFileProtectionKey");
            if (protectionOption.isString()) {
              options["NSFileProtectionKey"] = protectionOption.asString(runtime).utf8(runtime);
            }
          }
#endif
        }

        try {
          fs::rename(filePath.c_str(), destPath.c_str());
          
#ifdef __APPLE__
          if (options.count("NSFileProtectionKey") > 0) {
            platformHelper->setResourceValue(
              destPath.c_str(),
              "NSFileProtectionKey",
              options["NSFileProtectionKey"].c_str()
            );
          }
#endif
        } catch (fs::filesystem_error& e) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "moveFile", e.what()));
        }

        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "copyFolder") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyFolder", "First argument ('filepath') has to be of type string"));
        }
        if (count < 2 || !arguments[1].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyFolder", "Second argument ('destPath') has to be of type string"));
        }

        std::string srcFolderPath = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        std::string destFolderPath = cleanPath(arguments[1].asString(runtime).utf8(runtime));
        
        std::map<std::string, std::string> options;
        if (count == 3 && arguments[2].isObject()) {
          auto optionsObject = arguments[2].asObject(runtime);
#ifdef __APPLE__
          if (optionsObject.hasProperty(runtime, "NSFileProtectionKey")) {
            auto protectionOption = optionsObject.getProperty(runtime, "NSFileProtectionKey");
            if (protectionOption.isString()) {
              options["NSFileProtectionKey"] = protectionOption.asString(runtime).utf8(runtime);
            }
          }
#endif
        }

        struct stat t_stat;
        int res = stat(srcFolderPath.c_str(), &t_stat);
        if (res < 0) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", srcFolderPath.c_str(), "copyFolder", strerror(errno)));
        }
        if (!S_ISDIR(t_stat.st_mode)) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", srcFolderPath.c_str(), "copyFolder", "Not a directory"));
        }

        try {
          fs::copy(srcFolderPath, destFolderPath, fs::copy_options::overwrite_existing | fs::copy_options::recursive);
          
#ifdef __APPLE__
          if (options.count("NSFileProtectionKey") > 0) {
            platformHelper->setResourceValue(
              destFolderPath.c_str(),
              "NSFileProtectionKey",
              options["NSFileProtectionKey"].c_str()
            );
          }
#endif
        } catch (fs::filesystem_error& e) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyFolder", e.what()));
        }

        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "copyFile" || propName == "copyFileAssets" || propName == "copyFileRes") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this, propName](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifndef __ANDROID__
        if (propName == "copyFileAssets" || propName == "copyFileRes") {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Command only for Android"));
        }
#endif
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "First argument ('filepath') has to be of type string"));
        }
        if (count < 2 || !arguments[1].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Second argument ('destPath') has to be of type string"));
        }

        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        std::string destPath = cleanPath(arguments[1].asString(runtime).utf8(runtime));
        
        std::map<std::string, std::string> options;
        if (count == 3 && arguments[2].isObject()) {
          auto optionsObject = arguments[2].asObject(runtime);
#ifdef __APPLE__
          if (optionsObject.hasProperty(runtime, "NSFileProtectionKey")) {
            auto protectionOption = optionsObject.getProperty(runtime, "NSFileProtectionKey");
            if (protectionOption.isString()) {
              options["NSFileProtectionKey"] = protectionOption.asString(runtime).utf8(runtime);
            }
          }
#endif
        }

        try {
          if (propName == "copyFileAssets" || propName == "copyFileRes") {
#ifdef __ANDROID__
            platformHelper->copyFileAssetsOrRes(filePath.c_str(), destPath.c_str(), propName == "copyFileRes");
#endif
          } else {
            fs::copy(filePath.c_str(), destPath.c_str());
            
#ifdef __APPLE__
          if (options.count("NSFileProtectionKey") > 0) {
            platformHelper->setResourceValue(
              destPath.c_str(),
              "NSFileProtectionKey",
              options["NSFileProtectionKey"].c_str()
            );
          }
#endif
          }
        } catch (fs::filesystem_error& e) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), e.what()));
        } catch (const char* error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", filePath.c_str(), propName.c_str(), error_message));
        }

        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "copyAssetsFileIOS") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifdef __APPLE__
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsFileIOS", "First argument ('imageUri') has to be of type string"));
        }
        if (count < 2 || !arguments[1].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsFileIOS", "Second argument ('destPath') has to be of type string"));
        }
        if (count < 3 || !arguments[2].isNumber()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsFileIOS", "Third argument ('width') has to be of type number"));
        }
        if (count < 4 || !arguments[3].isNumber()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsFileIOS", "Fourth argument ('height') has to be of type number"));
        }

        std::string imageUri = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        std::string destPath = cleanPath(arguments[1].asString(runtime).utf8(runtime));

        int width = arguments[2].asNumber();
        int height = arguments[3].asNumber();
        float scale{1.0};
        if (count > 4 && arguments[4].isNumber()) {
          scale = arguments[4].asNumber();
        }
        float compression{1.0};
        if (count > 5 && arguments[5].isNumber()) {
          compression = arguments[5].asNumber();
        }
        std::string resizeMode{"contain"};
        if (count > 6 && arguments[6].isString()) {
          resizeMode = arguments[6].asString(runtime).utf8(runtime);
        }
        
        if (imageUri.rfind("ph://", 0) == std::string::npos) {
          try {
            fs::copy(imageUri.c_str(), destPath.c_str());
          } catch (fs::filesystem_error& e) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsFileIOS", e.what()));
          }
          return jsi::String::createFromUtf8(runtime, destPath);
        }

        try {
          platformHelper->copyAssetsFileIOS(imageUri.c_str(), destPath.c_str(), width, height, scale, compression, resizeMode.c_str());
        } catch (const char* error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", imageUri.c_str(), "copyAssetsFileIOS", error_message));
        }
        
        return jsi::String::createFromUtf8(runtime, destPath);
#else
        throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsFileIOS", "Command only for iOS"));
#endif
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "copyAssetsVideoIOS") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifdef __APPLE__
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsVideoIOS", "First argument ('videoUri') has to be of type string"));
        }
        if (count < 2 || !arguments[1].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsVideoIOS", "Second argument ('destPath') has to be of type string"));
        }

        std::string videoUri = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        std::string destPath = cleanPath(arguments[1].asString(runtime).utf8(runtime));

        if (videoUri.rfind("ph://", 0) == std::string::npos) {
          try {
            fs::copy(videoUri.c_str(), destPath.c_str());
          } catch (fs::filesystem_error& e) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsVideoIOS", e.what()));
          }
          return jsi::String::createFromUtf8(runtime, destPath);
        }

        try {
          platformHelper->copyAssetsVideoIOS(videoUri.c_str(), destPath.c_str());
        } catch (const char* error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", videoUri.c_str(), "copyAssetsVideoIOS", error_message));
        } catch (const std::exception& e) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", videoUri.c_str(), "copyAssetsVideoIOS", e.what()));
        }

        return jsi::String::createFromUtf8(runtime, destPath);
#else
        throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "copyAssetsVideoIOS", "Command only for iOS"));
#endif
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "unlink") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "unlink", "First argument ('filepath') has to be of type string"));
        }
        if (count > 2) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "touch", "Too many arguments"));
        }
        
        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        bool checkExistence{true};
        if (count > 1 && arguments[1].isBool()) {
          checkExistence = arguments[1].asBool();
        }
        
        if (checkExistence) {
          struct stat t_stat;
          bool exists = stat(filePath.c_str(), &t_stat) >= 0;
          if (!exists) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", "unlink", "No such file or directory", filePath.c_str()));
          }
        }

        try {
          fs::remove_all(filePath.c_str());
        } catch (fs::filesystem_error& e) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "unlink", e.what()));
        }
        
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "exists" || propName == "existsAssets" || propName == "existsRes") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this, propName](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifndef __ANDROID__
        if (propName == "existsAssets" || propName == "existsRes") {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "Command only for Android"));
        }
#endif
        if (count != 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", propName.c_str(), "First argument ('filepath') has to be of type string"));
        }
        
        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));

        bool exists{false};
        if (propName == "existsAssets" || propName == "existsRes") {
#ifdef __ANDROID__
          exists = platformHelper->existsAssetsOrRes(filePath.c_str(), propName == "existsRes");
#endif
        } else {
          struct stat t_stat;
          exists = stat(filePath.c_str(), &t_stat) >= 0;
        }
        
        return jsi::Value(exists);
      }
    );
  }
  if (propName == "hash") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 2 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "hash", "First argument ('filepath') has to be of type string"));
        }
        if (count < 2 || !arguments[1].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "hash", "Second argument ('algorithm') has to be of type string"));
        }

        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));
        std::string algorithm = arguments[1].asString(runtime).utf8(runtime);

        try {
          std::string buffer = readFile(filePath.c_str(), 0, -1);
          jsi::String res = jsi::String::createFromUtf8(runtime, "");

          if (algorithm == "md5") {
            res = jsi::String::createFromUtf8(runtime, md5(buffer));
          } else if (algorithm == "sha1") {
            res = jsi::String::createFromUtf8(runtime, sha1(buffer));
          } else if (algorithm == "sha224") {
            res = jsi::String::createFromUtf8(runtime, sha224(buffer));
          } else if (algorithm == "sha256") {
            res = jsi::String::createFromUtf8(runtime, sha256(buffer));
          } else if (algorithm == "sha384") {
            res = jsi::String::createFromUtf8(runtime, sha384(buffer));
          } else if (algorithm == "sha512") {
            res = jsi::String::createFromUtf8(runtime, sha512(buffer));
          } else {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", "hash", "Wrong algorithm", algorithm.c_str()));
          }

          return res;
        } catch (const char* error_message) {
          throw jsi::JSError(runtime, error_message);
        }
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "touch") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "touch", "First argument ('filepath') has to be of type string"));
        }
        if (count > 3) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "touch", "Too many arguments"));
        }

        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));

        long int mtime{0};
        bool isMTime{false};
        if (count > 1 && arguments[1].isNumber()) {
          mtime = arguments[1].asNumber() / 1000;
          isMTime = true;
        }
        long int ctime{0};
        bool isCTime{false};
        if (count > 2 && arguments[2].isNumber()) {
          ctime = arguments[2].asNumber() / 1000;
          isCTime = true;
        }

        struct stat t_stat;
        int res = stat(filePath.c_str(), &t_stat);
        if (res < 0) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s: %s", filePath.c_str(), "touch", strerror(errno)));
        }

        if (isMTime) {
          struct utimbuf new_times;
          new_times.modtime = mtime > 0 ? mtime : std::time(0);
          new_times.actime = mtime > 0 ? mtime : std::time(0);
          utime(filePath.c_str(), &new_times);
        }
        if (isCTime) {
          // Not available
        }
        stat(filePath.c_str(), &t_stat);

        return jsi::Value(static_cast<int>(t_stat.st_mtime));
      }
    );
  }
  if (propName == "mkdir") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if ((count != 1 && count != 2) || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "mkdir", "First argument ('filepath') has to be of type string"));
        }

        std::string filePath = cleanPath(arguments[0].asString(runtime).utf8(runtime));

        std::map<std::string, std::string> options;
        if (count == 2 && arguments[1].isObject()) {
          auto optionsObject = arguments[1].asObject(runtime);
#ifdef __APPLE__
          if (optionsObject.hasProperty(runtime, "NSFileProtectionKey")) {
            auto protectionOption = optionsObject.getProperty(runtime, "NSFileProtectionKey");
            if (protectionOption.isString()) {
              options["NSFileProtectionKey"] = protectionOption.asString(runtime).utf8(runtime);
            }
          }
          if (optionsObject.hasProperty(runtime, "NSURLIsExcludedFromBackupKey")) {
            auto excludeFromBackupOption = optionsObject.getProperty(runtime, "NSURLIsExcludedFromBackupKey");
            if (excludeFromBackupOption.isBool()) {
              options["NSURLIsExcludedFromBackupKey"] = excludeFromBackupOption.getBool() ? "YES" : "NO";
            }
          }
#endif
        }

        try {
          fs::create_directories(filePath);

#ifdef __APPLE__
          if (options.count("NSFileProtectionKey") > 0) {
            platformHelper->setResourceValue(
              filePath.c_str(),
              "NSFileProtectionKey",
              options["NSFileProtectionKey"].c_str()
            );
          }
          if (options.count("NSURLIsExcludedFromBackupKey") > 0) {
            platformHelper->setResourceValue(
              filePath.c_str(),
              "NSURLIsExcludedFromBackupKey",
              options["NSURLIsExcludedFromBackupKey"].c_str()
            );
          }
#endif
        } catch (fs::filesystem_error& e) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "mkdir", e.what()));
        }

        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "downloadFile") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 3 || !arguments[0].isObject()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "downloadFile", "First argument ('options') has to be of type object"));
        }
        if (count > 3) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "downloadFile", "Too many arguments"));
        }
        jsi::Object options = arguments[0].asObject(runtime);
        if (!options.hasProperty(runtime, "fromUrl")) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "downloadFile", "fromUrl option is mandatory"));
        }
        if (!options.hasProperty(runtime, "toFile")) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "downloadFile", "toFile option is mandatory"));
        }
        std::shared_ptr<jsi::Function> completeFunc = std::make_shared<jsi::Function>(arguments[1].asObject(runtime).asFunction(runtime));
        std::shared_ptr<jsi::Function> errorFunc = std::make_shared<jsi::Function>(arguments[2].asObject(runtime).asFunction(runtime));
        std::string fromUrl{""};
        std::string toFile{""};
        std::map<std::string, std::string> headers;
        bool background{false};
        bool discretionary{false};
        bool cacheable{true};
        int progressInterval{0};
        float progressDivider{0};
        std::shared_ptr<jsi::Function> beginCallbackFunc;
        std::shared_ptr<jsi::Function> progressCallbackFunc;
        std::shared_ptr<jsi::Function> resumableCallbackFunc;
        int connectionTimeout{0};
        int readTimeout{0};
        int backgroundTimeout{0};
        jsi::Value optionFromUrl = options.getProperty(runtime, "fromUrl");
        if (optionFromUrl.isString()) {
          fromUrl = optionFromUrl.asString(runtime).utf8(runtime);
          if (fromUrl.size() == 0) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "downloadFile", "fromUrl option is mandatory"));
          }
        } else {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "downloadFile", "fromUrl option should be a string"));
        }
        jsi::Value optionToFile = options.getProperty(runtime, "toFile");
        if (optionToFile.isString()) {
          toFile = cleanPath(optionToFile.asString(runtime).utf8(runtime));
          if (toFile.size() == 0) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "downloadFile", "toFile option is mandatory"));
          }
        } else {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "downloadFile", "toFile option should be a string"));
        }
        if (options.hasProperty(runtime, "headers")) {
          jsi::Value optionHeaders = options.getProperty(runtime, "headers");
          if (optionHeaders.isObject()) {
            jsi::Object tempHeaders = optionHeaders.asObject(runtime);
            jsi::Array headerNames = tempHeaders.getPropertyNames(runtime);
            for (int i = 0; i < headerNames.size(runtime); i++){
              std::string headerKey = headerNames.getValueAtIndex(runtime, i).asString(runtime).utf8(runtime);
              std::string headerValue = tempHeaders.getProperty(runtime, headerKey.c_str()).asString(runtime).utf8(runtime);
              headers[headerKey] = headerValue;
            }
          }
        }
        if (options.hasProperty(runtime, "begin")) {
          jsi::Value optionBegin = options.getProperty(runtime, "begin");
          if (optionBegin.isObject()) {
            beginCallbackFunc = std::make_shared<jsi::Function>(optionBegin.asObject(runtime).asFunction(runtime));
          }
        }
        if (options.hasProperty(runtime, "progress")) {
          jsi::Value optionProgress = options.getProperty(runtime, "progress");
          if (optionProgress.isObject()) {
            progressCallbackFunc = std::make_shared<jsi::Function>(optionProgress.asObject(runtime).asFunction(runtime));
          }
        }
        if (options.hasProperty(runtime, "resumable")) {
          jsi::Value optionResumable = options.getProperty(runtime, "resumable");
          if (optionResumable.isObject()) {
            resumableCallbackFunc = std::make_shared<jsi::Function>(optionResumable.asObject(runtime).asFunction(runtime));
          }
        }
        if (options.hasProperty(runtime, "background")) {
          jsi::Value optionBackground = options.getProperty(runtime, "background");
          if (optionBackground.isBool()) {
            background = optionBackground.asBool();
          }
        }
        if (options.hasProperty(runtime, "discretionary")) {
          jsi::Value optionDiscretionary = options.getProperty(runtime, "discretionary");
          if (optionDiscretionary.isBool()) {
            discretionary = optionDiscretionary.asBool();
          }
        }
        if (options.hasProperty(runtime, "cacheable")) {
          jsi::Value optionCacheable = options.getProperty(runtime, "cacheable");
          if (optionCacheable.isBool()) {
            cacheable = optionCacheable.asBool();
          }
        }
        if (options.hasProperty(runtime, "progressInterval")) {
          jsi::Value optionProgressInterval = options.getProperty(runtime, "progressInterval");
          if (optionProgressInterval.isNumber()) {
            progressInterval = optionProgressInterval.asNumber();
          }
        }
        if (options.hasProperty(runtime, "progressDivider")) {
          jsi::Value optionProgressDivider = options.getProperty(runtime, "progressDivider");
          if (optionProgressDivider.isNumber()) {
            progressDivider = optionProgressDivider.asNumber();
          }
        }
        if (options.hasProperty(runtime, "connectionTimeout")) {
          jsi::Value optionConnectionTimeout = options.getProperty(runtime, "connectionTimeout");
          if (optionConnectionTimeout.isNumber()) {
            connectionTimeout = optionConnectionTimeout.asNumber();
          }
        }
        if (options.hasProperty(runtime, "readTimeout")) {
          jsi::Value optionReadTimeout = options.getProperty(runtime, "readTimeout");
          if (optionReadTimeout.isNumber()) {
            readTimeout = optionReadTimeout.asNumber();
          }
        }
        if (options.hasProperty(runtime, "backgroundTimeout")) {
          jsi::Value optionBackgroundTimeout = options.getProperty(runtime, "backgroundTimeout");
          if (optionBackgroundTimeout.isNumber()) {
            backgroundTimeout = optionBackgroundTimeout.asNumber();
          }
        }
        
        RNFSTurboPlatformHelper::jobId += 1;
        int jobId = RNFSTurboPlatformHelper::jobId;
        
        RNFSTurboCompleteDownloadCallback completeCallback = [&runtime, completeFunc, this](int jobId, int statusCode, double bytesWritten) -> void {
          _jsInvoker->invokeAsync([&runtime, completeFunc, jobId, statusCode, bytesWritten]() {
            jsi::Object result = jsi::Object(runtime);
            result.setProperty(runtime, "jobId", jsi::Value(jobId));
            result.setProperty(runtime, "statusCode", jsi::Value(statusCode));
            result.setProperty(runtime, "bytesWritten", jsi::Value(bytesWritten));
            completeFunc->call(runtime, std::move(result));
          });
        };
        
        RNFSTurboErrorCallback errorCallback = [&runtime, errorFunc, this](int jobId, const char* errorMessage) -> void {
          std::string message = errorMessage != nullptr ? errorMessage : "Unknown error";
          _jsInvoker->invokeAsync([&runtime, errorFunc, jobId, message = std::move(message)]() {
            jsi::Object result = jsi::Object(runtime);
            result.setProperty(runtime, "jobId", jsi::Value(jobId));
            result.setProperty(runtime, "errorMessage", jsi::String::createFromUtf8(runtime, message));
            errorFunc->call(runtime, std::move(result));
          });
        };
        
        std::optional<RNFSTurboBeginDownloadCallback> beginCallback = std::nullopt;
        if (beginCallbackFunc) {
          beginCallback = [&runtime, beginCallbackFunc, this](int jobId, int statusCode, double contentLength, std::map<std::string, std::string> headers) -> void {
            _jsInvoker->invokeAsync([&runtime, beginCallbackFunc, jobId, statusCode, contentLength, headers]() {
              jsi::Object result = jsi::Object(runtime);
              result.setProperty(runtime, "jobId", jsi::Value(jobId));
              result.setProperty(runtime, "statusCode", jsi::Value(statusCode));
              result.setProperty(runtime, "contentLength", jsi::Value(contentLength));
              jsi::Object jsHeaders = jsi::Object(runtime);
              for (auto const& [key, val] : headers) {
                jsHeaders.setProperty(runtime, key.c_str(), jsi::String::createFromUtf8(runtime, val));
              }
              result.setProperty(runtime, "headers", jsHeaders);
              beginCallbackFunc->call(runtime, std::move(result));
            });
          };
        }
        std::optional<RNFSTurboProgressDownloadCallback> progressCallback = std::nullopt;
        if (progressCallbackFunc) {
          progressCallback = [&runtime, progressCallbackFunc, this](int jobId, double contentLength, double bytesWritten) -> void {
            _jsInvoker->invokeAsync([&runtime, progressCallbackFunc, jobId, contentLength, bytesWritten]() {
              jsi::Object result = jsi::Object(runtime);
              result.setProperty(runtime, "jobId", jsi::Value(jobId));
              result.setProperty(runtime, "contentLength", jsi::Value(contentLength));
              result.setProperty(runtime, "bytesWritten", jsi::Value(bytesWritten));
              progressCallbackFunc->call(runtime, std::move(result));
            });
          };
        }
        std::optional<RNFSTurboResumableDownloadCallback> resumableCallback = std::nullopt;
        if (resumableCallbackFunc) {
          resumableCallback = [&runtime, resumableCallbackFunc, this](int jobId) -> void {
            _jsInvoker->invokeAsync([&runtime, resumableCallbackFunc, jobId]() {
              resumableCallbackFunc->call(runtime, jsi::Value(jobId));
            });
          };
        }
        
        try {
          platformHelper->downloadFile(
            jobId,
            fromUrl.c_str(),
            toFile.c_str(),
            headers,
            background,
            discretionary,
            cacheable,
            progressInterval,
            progressDivider,
            connectionTimeout,
            readTimeout,
            backgroundTimeout,
            completeCallback,
            errorCallback,
            beginCallback,
            progressCallback,
            resumableCallback
          );
        } catch (const char* errorMessage) {
          jsi::Object result = jsi::Object(runtime);
          result.setProperty(runtime, "jobId", jsi::Value(jobId));
          result.setProperty(runtime, "errorMessage", jsi::String::createFromUtf8(runtime, errorMessage));
          errorFunc->call(runtime, std::move(result));
        }

        return jsi::Value(jobId);
      }
    );
  }
  if (propName == "stopDownload") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count != 1 || !arguments[0].isNumber()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "stopDownload", "First argument ('jobId') has to be of type number"));
        }
        
        int jobId = arguments[0].asNumber();

        platformHelper->stopDownload(jobId);
        
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "resumeDownload") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifdef __APPLE__
        if (count != 1 || !arguments[0].isNumber()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "resumeDownload", "First argument ('jobId') has to be of type number"));
        }
        
        int jobId = arguments[0].asNumber();

        platformHelper->resumeDownload(jobId);
#else
        throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "resumeDownload", "Command only for iOS"));
#endif
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "isResumable") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifdef __APPLE__
        if (count != 1 || !arguments[0].isNumber()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "isResumable", "First argument ('jobId') has to be of type number"));
        }
        
        int jobId = arguments[0].asNumber();

        bool isResumable = platformHelper->isResumable(jobId);
        return jsi::Value(isResumable);
#else
        throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "isResumable", "Command only for iOS"));
#endif
      }
    );
  }
  if (propName == "completeHandlerIOS") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifdef __APPLE__
        if (count != 1 || !arguments[0].isNumber()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "completeHandlerIOS", "First argument ('jobId') has to be of type number"));
        }
        
        int jobId = arguments[0].asNumber();

        platformHelper->completeHandlerIOS(jobId);
#else
        throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "completeHandlerIOS", "Command only for iOS"));
#endif
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "uploadFiles") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count < 3 || !arguments[0].isObject()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "First argument ('options') has to be of type object"));
        }
        if (count > 3) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "Too many arguments"));
        }
        jsi::Object options = arguments[0].asObject(runtime);
        if (!options.hasProperty(runtime, "toUrl")) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "toUrl option is mandatory"));
        }
        if (!options.hasProperty(runtime, "files")) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "files option is mandatory"));
        }
        std::shared_ptr<jsi::Function> completeFunc = std::make_shared<jsi::Function>(arguments[1].asObject(runtime).asFunction(runtime));
        std::shared_ptr<jsi::Function> errorFunc = std::make_shared<jsi::Function>(arguments[2].asObject(runtime).asFunction(runtime));
        std::string toUrl{""};
        bool binaryStreamOnly{false};
        std::map<std::string, std::string> headers;
        std::map<std::string, std::string> fields;
        std::string method{"POST"};
        std::shared_ptr<jsi::Function> beginCallbackFunc;
        std::shared_ptr<jsi::Function> progressCallbackFunc;
        jsi::Value optionToUrl = options.getProperty(runtime, "toUrl");
        if (optionToUrl.isString()) {
          toUrl = optionToUrl.asString(runtime).utf8(runtime);
          if (toUrl.size() == 0) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "toUrl option is mandatory"));
          }
        } else {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "toUrl option should be a string"));
        }
        if (options.hasProperty(runtime, "binaryStreamOnly")) {
          jsi::Value optionBinaryStreamOnly = options.getProperty(runtime, "binaryStreamOnly");
          if (optionBinaryStreamOnly.isBool()) {
            binaryStreamOnly = optionBinaryStreamOnly.asBool();
          }
        }
        jsi::Value optionFiles = options.getProperty(runtime, "files");
        if (!optionFiles.isObject()) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "files option should be an object"));
        }
        jsi::Array tmpFiles = optionFiles.asObject(runtime).asArray(runtime);
        int filesNum = (int)tmpFiles.size(runtime);
        if (filesNum == 0) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "files option is mandatory"));
        }
        UploadFileItem files[filesNum];
        for (int i = 0; i < filesNum; i++){
          jsi::Object obj = tmpFiles.getValueAtIndex(runtime, i).asObject(runtime);
          if (
              !obj.hasProperty(runtime, "name") ||
              !obj.hasProperty(runtime, "filename") ||
              !obj.hasProperty(runtime, "filepath") ||
              !obj.hasProperty(runtime, "filetype")
          ) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "Wrong file data"));
          }
          jsi::Value tmpName = obj.getProperty(runtime, "name");
          jsi::Value tmpFilename = obj.getProperty(runtime, "filename");
          jsi::Value tmpFilepath = obj.getProperty(runtime, "filepath");
          jsi::Value tmpFiletype = obj.getProperty(runtime, "filetype");
          if (
              !tmpName.isString() ||
              !tmpFilename.isString() ||
              !tmpFilepath.isString() ||
              !tmpFiletype.isString()
          ) {
            throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "uploadFiles", "Wrong file data"));
          }
          files[i] = {
            tmpName.asString(runtime).utf8(runtime),
            tmpFilename.asString(runtime).utf8(runtime),
            cleanPath(tmpFilepath.asString(runtime).utf8(runtime)),
            tmpFiletype.asString(runtime).utf8(runtime),
          };
        }
        if (options.hasProperty(runtime, "headers")) {
          jsi::Value optionHeaders = options.getProperty(runtime, "headers");
          if (optionHeaders.isObject()) {
            jsi::Object tempHeaders = optionHeaders.asObject(runtime);
            jsi::Array headerNames = tempHeaders.getPropertyNames(runtime);
            for (int i = 0; i < headerNames.size(runtime); i++){
              std::string headerKey = headerNames.getValueAtIndex(runtime, i).asString(runtime).utf8(runtime);
              std::string headerValue = tempHeaders.getProperty(runtime, headerKey.c_str()).asString(runtime).utf8(runtime);
              headers[headerKey] = headerValue;
            }
          }
        }
        if (options.hasProperty(runtime, "fields")) {
          jsi::Value optionFields = options.getProperty(runtime, "fields");
          if (optionFields.isObject()) {
            jsi::Object tempFields = optionFields.asObject(runtime);
            jsi::Array fieldNames = tempFields.getPropertyNames(runtime);
            for (int i = 0; i < fieldNames.size(runtime); i++){
              std::string fieldKey = fieldNames.getValueAtIndex(runtime, i).asString(runtime).utf8(runtime);
              std::string fieldValue = tempFields.getProperty(runtime, fieldKey.c_str()).asString(runtime).utf8(runtime);
              fields[fieldKey] = fieldValue;
            }
          }
        }
        if (options.hasProperty(runtime, "method")) {
          jsi::Value optionMethod = options.getProperty(runtime, "method");
          if (optionMethod.isString()) {
            method = optionMethod.asString(runtime).utf8(runtime);
          }
        }
        if (options.hasProperty(runtime, "begin")) {
          jsi::Value optionBegin = options.getProperty(runtime, "begin");
          if (optionBegin.isObject()) {
            beginCallbackFunc = std::make_shared<jsi::Function>(optionBegin.asObject(runtime).asFunction(runtime));
          }
        }
        if (options.hasProperty(runtime, "progress")) {
          jsi::Value optionProgress = options.getProperty(runtime, "progress");
          if (optionProgress.isObject()) {
            progressCallbackFunc = std::make_shared<jsi::Function>(optionProgress.asObject(runtime).asFunction(runtime));
          }
        }
        
        RNFSTurboPlatformHelper::jobId += 1;
        int jobId = RNFSTurboPlatformHelper::jobId;
        
        RNFSTurboCompleteUploadCallback completeCallback = [&runtime, completeFunc, this](int jobId, int statusCode, std::map<std::string, std::string> headers, const char* body) -> void {
          std::string bodyString = body != nullptr ? std::string(body) : std::string();
          _jsInvoker->invokeAsync([&runtime, completeFunc, jobId, statusCode, headers = std::move(headers), bodyString = std::move(bodyString)]() {
            jsi::Object result = jsi::Object(runtime);
            result.setProperty(runtime, "jobId", jsi::Value(jobId));
            result.setProperty(runtime, "statusCode", jsi::Value(statusCode));
            jsi::Object jsHeaders = jsi::Object(runtime);
            for (auto const& [key, val] : headers) {
              jsHeaders.setProperty(runtime, key.c_str(), jsi::String::createFromUtf8(runtime, val));
            }
            result.setProperty(runtime, "headers", jsHeaders);
            result.setProperty(runtime, "body", jsi::String::createFromUtf8(runtime, bodyString));
            completeFunc->call(runtime, std::move(result));
          });
        };
        
        RNFSTurboErrorCallback errorCallback = [&runtime, errorFunc, this](int jobId, const char* errorMessage) -> void {
          std::string message = errorMessage != nullptr ? errorMessage : "Unknown error";
          _jsInvoker->invokeAsync([&runtime, errorFunc, jobId, message = std::move(message)]() {
            jsi::Object result = jsi::Object(runtime);
            result.setProperty(runtime, "jobId", jsi::Value(jobId));
            result.setProperty(runtime, "errorMessage", jsi::String::createFromUtf8(runtime, message));
            errorFunc->call(runtime, std::move(result));
          });
        };
        
        std::optional<RNFSTurboBeginUploadCallback> beginCallback = std::nullopt;
        if (beginCallbackFunc) {
          beginCallback = [&runtime, beginCallbackFunc, this](int jobId) -> void {
            _jsInvoker->invokeAsync([&runtime, beginCallbackFunc, jobId]() {
              jsi::Object result = jsi::Object(runtime);
              result.setProperty(runtime, "jobId", jsi::Value(jobId));
              beginCallbackFunc->call(runtime, std::move(result));
            });
          };
        }
        std::optional<RNFSTurboProgressUploadCallback> progressCallback = std::nullopt;
        if (progressCallbackFunc) {
          progressCallback = [&runtime, progressCallbackFunc, this](int jobId, double totalBytesExpectedToSend, double totalBytesSent) -> void {
            _jsInvoker->invokeAsync([&runtime, progressCallbackFunc, jobId, totalBytesExpectedToSend, totalBytesSent]() {
              jsi::Object result = jsi::Object(runtime);
              result.setProperty(runtime, "jobId", jsi::Value(jobId));
              result.setProperty(runtime, "totalBytesExpectedToSend", jsi::Value(totalBytesExpectedToSend));
              result.setProperty(runtime, "totalBytesSent", jsi::Value(totalBytesSent));
              progressCallbackFunc->call(runtime, std::move(result));
            });
          };
        }
        
        try {
          platformHelper->uploadFiles(
            jobId,
            toUrl.c_str(),
            binaryStreamOnly,
            files,
            filesNum,
            headers,
            fields,
            method.c_str(),
            completeCallback,
            errorCallback,
            beginCallback,
            progressCallback
          );
        } catch (const char* errorMessage) {
          jsi::Object result = jsi::Object(runtime);
          result.setProperty(runtime, "jobId", jsi::Value(jobId));
          result.setProperty(runtime, "errorMessage", jsi::String::createFromUtf8(runtime, errorMessage));
          errorFunc->call(runtime, std::move(result));
        }

        return jsi::Value(jobId);
      }
    );
  }
  if (propName == "stopUpload") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count != 1 || !arguments[0].isNumber()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "stopUpload", "First argument ('jobId') has to be of type number"));
        }
        
        int jobId = arguments[0].asNumber();

        platformHelper->stopUpload(jobId);
        
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "getFSInfo") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
        if (count > 0) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "getFSInfo", "The function does not take arguments"));
        }
        
        try {
          FSInfo fsInfo = platformHelper->getFSInfo();
          
          jsi::Object result = jsi::Object(runtime);
          result.setProperty(runtime, "totalSpace", jsi::Value(static_cast<double>(fsInfo.totalSpace)));
          result.setProperty(runtime, "freeSpace", jsi::Value(static_cast<double>(fsInfo.freeSpace)));
          if (fsInfo.totalSpaceEx.has_value()) {
            result.setProperty(runtime, "totalSpaceEx", jsi::Value(static_cast<double>(fsInfo.totalSpaceEx.value())));
          }
          if (fsInfo.freeSpaceEx.has_value()) {
            result.setProperty(runtime, "freeSpaceEx", jsi::Value(static_cast<double>(fsInfo.freeSpaceEx.value())));
          }
#ifdef RNFSTURBO_USE_ENCRYPTION
          result.setProperty(runtime, "encryptionEnabled", jsi::Value(true));
#else
          result.setProperty(runtime, "encryptionEnabled", jsi::Value(false));
#endif
          
          return result;
        } catch (const char* error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "getFSInfo", error_message));
        }
        
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "scanFile") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifdef __ANDROID__
        if (count < 2 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "scanFile", "First argument ('path') has to be of type string"));
        }
        if (count > 2) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "scanFile", "Too many arguments"));
        }

        std::string path = cleanPath(arguments[0].asString(runtime).utf8(runtime));

        std::shared_ptr<jsi::Function> scanFunc = std::make_shared<jsi::Function>(
          arguments[1].asObject(runtime).asFunction(runtime)
        );

        RNFSTurboPlatformHelper::jobId += 1;
        int jobId = RNFSTurboPlatformHelper::jobId;

        RNFSTurboScanCallback scanCallback = [&runtime, scanFunc, this](int jobId, std::string path) -> void {
          _jsInvoker->invokeAsync([&runtime, scanFunc, jobId, path]() {
            jsi::Object result = jsi::Object(runtime);
            result.setProperty(runtime, "jobId", jsi::Value(jobId));
            result.setProperty(runtime, "path", jsi::String::createFromUtf8(runtime, path));
            scanFunc->call(runtime, std::move(result));
          });
        };

        try {
          platformHelper->scanFile(jobId, path.c_str(), scanCallback);
        } catch (const char* error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "scanFile", error_message));
        }
        return jsi::Value(jobId);
#else
        throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "scanFile", "Command only for Android"));
#endif
      }
    );
  }
  if (propName == "getAllExternalFilesDirs") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifdef __ANDROID__
        if (count > 0) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "getAllExternalFilesDirs", "The function does not take arguments"));
        }

        try {
          std::vector<std::string> items = platformHelper->getAllExternalFilesDirs();
          int len = items.size();

          std::vector<std::string>::iterator itemsIt = items.begin();

          jsi::Array arr = jsi::Array(runtime, len);

          len = 0;
          while (itemsIt != end(items)) {
            arr.setValueAtIndex(
              runtime,
              len,
              jsi::String::createFromUtf8(runtime, *itemsIt)
            );
            itemsIt++;
            len++;
          }

          return arr;
        } catch (const char* error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "getAllExternalFilesDirs", error_message));
        }
#else
        throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "getAllExternalFilesDirs", "Command only for Android"));
#endif
        return jsi::Value::undefined();
      }
    );
  }
  if (propName == "pathForGroup") {
    return jsi::Function::createFromHostFunction(
      runtime, jsi::PropNameID::forAscii(runtime, propName),
      1,
      [this](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value {
#ifdef __APPLE__
        if (count != 1 || !arguments[0].isString()) [[unlikely]] {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "pathForGroup", "First argument ('groupIdentifier') has to be of type string"));
        }
        
        std::string groupIdentifier = arguments[0].asString(runtime).utf8(runtime);

        try {
          std::string path = platformHelper->pathForGroup(groupIdentifier.c_str());

          return jsi::String::createFromUtf8(runtime, path);
        } catch (const char* error_message) {
          throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "pathForGroup", error_message));
        }
#else
        throw jsi::JSError(runtime, RNFSTurboLogger::sprintf("%s: %s", "pathForGroup", "Command only for iOS"));
#endif
        return jsi::Value::undefined();
      }
    );
  }

  return jsi::Value::undefined();
}

}
