// This is a generated file, modify: generate/templates/templates/struct_content.cc

// generated from struct_content.cc
#include <nan.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // win32

extern "C" {
  #include <git2.h>
 }

#include <iostream>
#include "../include/nodegit.h"
#include "../include/lock_master.h"
#include "../include/functions/copy.h"
#include "../include/diff_options.h"
#include "nodegit_wrapper.cc"

  #include "../include/strarray.h"
  #include "../include/diff_delta.h"
 
using namespace v8;
using namespace node;
using namespace std;

 
ConfigurableGitDiffOptions::ConfigurableGitDiffOptions(nodegit::Context *nodegitContext)
  : nodegit::ConfigurableClassWrapper<GitDiffOptionsTraits>(nodegitContext)
{
     git_diff_options  wrappedValue = GIT_DIFF_OPTIONS_INIT;
    this->raw = (git_diff_options*) malloc(sizeof(git_diff_options ));
    memcpy(this->raw, &wrappedValue, sizeof(git_diff_options ));
 }

ConfigurableGitDiffOptions::~ConfigurableGitDiffOptions() {
              if (this->raw->pathspec.count) {
          for (size_t i = 0; i < this->raw->pathspec.count; ++i) {
            delete this->raw->pathspec.strings[i];
          }
          delete[] this->raw->pathspec.strings;
        }
                        delete this->raw->old_prefix;
          delete this->raw->new_prefix;
   }

nodegit::ConfigurableClassWrapper<GitDiffOptionsTraits>::v8ConversionResult ConfigurableGitDiffOptions::fromJavascript(nodegit::Context *nodegitContext, v8::Local<v8::Value> input) {
  if (!input->IsObject()) {
    return {
      "Must pass object for ConfigurableGitDiffOptions"
    };
  }

  Nan::HandleScope scope;
  v8::Local<v8::Object> inputObj = input.As<v8::Object>();
  std::shared_ptr<ConfigurableGitDiffOptions> output(new ConfigurableGitDiffOptions(nodegitContext));

  // unpack the data into the correct fields
         {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "version");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to version"
              };
            }

            output->raw->version = static_cast<unsigned int>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "flags");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to flags"
              };
            }

            output->raw->flags = static_cast<uint32_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "ignoreSubmodules");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to ignoreSubmodules"
              };
            }

            output->raw->ignore_submodules = static_cast<git_submodule_ignore_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
            output->raw->pathspec.count = 0;
          output->raw->pathspec.strings = nullptr;

          {
            v8::Local<v8::Value> maybeStrarray = nodegit::safeGetField(inputObj, "pathspec");
            if (!maybeStrarray.IsEmpty() && !maybeStrarray->IsUndefined() && !maybeStrarray->IsNull()) {
              if (maybeStrarray->IsArray()) {
                v8::Local<v8::Array> strarrayValue = maybeStrarray.As<v8::Array>();
                // validate the StrArray is indeed a list of strings
                for (uint32_t i = 0; i < strarrayValue->Length(); ++i) {
                  // TODO confirm that sparse array at least boils down to undefined
                  v8::Local<v8::Value> arrayValue = Nan::Get(strarrayValue, i).ToLocalChecked();
                  if (!arrayValue->IsString()) {
                    return {
                      "Must pass String or Array of strings to pathspec"
                    };
                  }
                }

                StrArrayConverter::ConvertInto(&output->raw->pathspec, strarrayValue);
              } else if (maybeStrarray->IsString()) {
                v8::Local<v8::String> strarrayValue = maybeStrarray.As<v8::String>();
                StrArrayConverter::ConvertInto(&output->raw->pathspec, strarrayValue);
              } else {
                return {
                  "Must pass String or Array of strings to pathspec"
                };
              }
            }
          }
           {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "notifyCb");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to notifyCb"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle =  0 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->notifyCb.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->notify_cb = (git_diff_notify_cb)notifyCb_cppCallback;
          }
        }
          {
          v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(inputObj, "progressCb");
          if (!maybeCallback.IsEmpty() && !maybeCallback->IsUndefined() && !maybeCallback->IsNull()) {
            if (!maybeCallback->IsFunction() && !maybeCallback->IsObject()) {
              return {
                "Must pass Function or CallbackSpecifier to progressCb"
              };
            }

            std::unique_ptr<Nan::Callback> callback;
            uint32_t throttle =  0 ;
            bool waitForResult = true;

            if (maybeCallback->IsFunction()) {
              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));
            } else {
              v8::Local<v8::Object> callbackSpecifier = maybeCallback.As<v8::Object>();
              v8::Local<v8::Value> maybeCallback = nodegit::safeGetField(callbackSpecifier, "callback");
              if (maybeCallback.IsEmpty() || !maybeCallback->IsFunction()) {
                return {
                  "Must pass callback to CallbackSpecifier"
                };
              }

              callback.reset(new Nan::Callback(maybeCallback.As<v8::Function>()));

              v8::Local<v8::Value> maybeThrottle = nodegit::safeGetField(callbackSpecifier, "throttle");
              if (!maybeThrottle.IsEmpty() && !maybeThrottle->IsUndefined() && !maybeThrottle->IsNull()) {
                if (!maybeThrottle->IsNumber()) {
                  return {
                    "Must pass zero or positive number as throttle to CallbackSpecifier"
                  };
                }

                throttle = maybeThrottle->Uint32Value(Nan::GetCurrentContext()).FromJust();
              }

              v8::Local<v8::Value> maybeWaitForResult = nodegit::safeGetField(callbackSpecifier, "waitForResult");
              if (!maybeWaitForResult.IsEmpty() && !maybeWaitForResult->IsUndefined() && !maybeWaitForResult->IsNull()) {
                if (!maybeWaitForResult->IsBoolean()) {
                  return {
                    "Must pass a boolean as waitForResult to callbackSpecifier"
                  };
                }

                waitForResult = Nan::To<bool>(maybeWaitForResult).FromJust();
              }
            }

            output->progressCb.SetCallback(std::move(callback), throttle, waitForResult);
            output->raw->progress_cb = (git_diff_progress_cb)progressCb_cppCallback;
          }
        }
          output->raw->payload = (void *)output.get();
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "contextLines");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to contextLines"
              };
            }

            output->raw->context_lines = static_cast<uint32_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "interhunkLines");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to interhunkLines"
              };
            }

            output->raw->interhunk_lines = static_cast<uint32_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "idAbbrev");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to idAbbrev"
              };
            }

            output->raw->id_abbrev = static_cast<uint16_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
           {
          v8::Local<v8::Value> maybeNumber = nodegit::safeGetField(inputObj, "maxSize");
          if (!maybeNumber.IsEmpty() && !maybeNumber->IsUndefined() && !maybeNumber->IsNull()) {
            if (!maybeNumber->IsNumber()) {
              return {
                "Must pass Int32 to maxSize"
              };
            }

            output->raw->max_size = static_cast<git_off_t>(maybeNumber->Int32Value(Nan::GetCurrentContext()).FromJust());
          }
        }
          output->raw->old_prefix = nullptr;
        {
          v8::Local<v8::Value> maybeString = nodegit::safeGetField(inputObj, "oldPrefix");
          if (!maybeString.IsEmpty() && !maybeString->IsUndefined() && !maybeString->IsNull()) {
            if (!maybeString->IsString()) {
              return {
                "Must pass string to oldPrefix"
              };
            }

            Nan::Utf8String utf8String(maybeString.As<v8::String>());
            output->raw->old_prefix = strdup(*utf8String);
          }
        }
          output->raw->new_prefix = nullptr;
        {
          v8::Local<v8::Value> maybeString = nodegit::safeGetField(inputObj, "newPrefix");
          if (!maybeString.IsEmpty() && !maybeString->IsUndefined() && !maybeString->IsNull()) {
            if (!maybeString->IsString()) {
              return {
                "Must pass string to newPrefix"
              };
            }

            Nan::Utf8String utf8String(maybeString.As<v8::String>());
            output->raw->new_prefix = strdup(*utf8String);
          }
        }
    
  return {
    output
  };
}

              ConfigurableGitDiffOptions* ConfigurableGitDiffOptions::notifyCb_getInstanceFromBaton(NotifyCbBaton* baton) {
           return static_cast<ConfigurableGitDiffOptions*>(baton->
                 payload
  );
       }

      int ConfigurableGitDiffOptions::notifyCb_cppCallback (
          const git_diff * diff_so_far,           git_diff_delta * delta_to_add,           const char * matched_pathspec,           void * payload        ) {
        NotifyCbBaton *baton =
          new NotifyCbBaton(1);

          baton->diff_so_far = diff_so_far;
          baton->delta_to_add = delta_to_add;
          baton->matched_pathspec = matched_pathspec;
          baton->payload = payload;
 
        ConfigurableGitDiffOptions* instance = notifyCb_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->notifyCb.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->notifyCb.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(notifyCb_async, notifyCb_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(notifyCb_async, notifyCb_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitDiffOptions::notifyCb_cancelAsync(void *untypedBaton) {
        NotifyCbBaton* baton = static_cast<NotifyCbBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitDiffOptions::notifyCb_async(void *untypedBaton) {
        Nan::HandleScope scope;

        NotifyCbBaton* baton = static_cast<NotifyCbBaton*>(untypedBaton);
        ConfigurableGitDiffOptions* instance = notifyCb_getInstanceFromBaton(baton);

        if (instance->notifyCb.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[2] = {
               GitDiffDelta::New(baton->delta_to_add, false)
 ,               baton->matched_pathspec == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->matched_pathspec).ToLocalChecked()
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->notifyCb.GetCallback()))(
          baton->GetAsyncResource(),
          2,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitDiffOptions::notifyCb_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitDiffOptions::notifyCb_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        NotifyCbBaton* baton = static_cast<NotifyCbBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitDiffOptions* instance = static_cast<ConfigurableGitDiffOptions*>(baton->   payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
        ConfigurableGitDiffOptions* ConfigurableGitDiffOptions::progressCb_getInstanceFromBaton(ProgressCbBaton* baton) {
           return static_cast<ConfigurableGitDiffOptions*>(baton->
                 payload
  );
       }

      int ConfigurableGitDiffOptions::progressCb_cppCallback (
          const git_diff * diff_so_far,           const char * old_path,           const char * new_path,           void * payload        ) {
        ProgressCbBaton *baton =
          new ProgressCbBaton(1);

          baton->diff_so_far = diff_so_far;
          baton->old_path = old_path;
          baton->new_path = new_path;
          baton->payload = payload;
 
        ConfigurableGitDiffOptions* instance = progressCb_getInstanceFromBaton(baton);

           int result;

          if (instance->nodegitContext != nodegit::ThreadPool::GetCurrentContext()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->progressCb.WillBeThrottled()) {
            result = baton->defaultResult;
            delete baton;
          } else if (instance->progressCb.ShouldWaitForResult()) {
            result = baton->ExecuteAsync(progressCb_async, progressCb_cancelAsync);
            delete baton;
          } else {
            result = baton->defaultResult;
            baton->ExecuteAsync(progressCb_async, progressCb_cancelAsync, nodegit::deleteBaton);
          }
          return result;
       }

      void ConfigurableGitDiffOptions::progressCb_cancelAsync(void *untypedBaton) {
        ProgressCbBaton* baton = static_cast<ProgressCbBaton*>(untypedBaton);
          baton->result = -1;
         baton->Done();
      }

      void ConfigurableGitDiffOptions::progressCb_async(void *untypedBaton) {
        Nan::HandleScope scope;

        ProgressCbBaton* baton = static_cast<ProgressCbBaton*>(untypedBaton);
        ConfigurableGitDiffOptions* instance = progressCb_getInstanceFromBaton(baton);

        if (instance->progressCb.GetCallback()->IsEmpty()) {
            baton->result = baton->defaultResult; // no results acquired
           baton->Done();
          return;
        }

           v8::Local<Value> argv[2] = {
               baton->old_path == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->old_path).ToLocalChecked()
 ,               baton->new_path == NULL
                ? Nan::EmptyString()
                : Nan::New( baton->new_path).ToLocalChecked()
            };
 
        Nan::TryCatch tryCatch;

        Nan::MaybeLocal<v8::Value> maybeResult = (*(instance->progressCb.GetCallback()))(
          baton->GetAsyncResource(),
          2,
          argv
        );
        v8::Local<v8::Value> result;
        if (!maybeResult.IsEmpty()) {
          result = maybeResult.ToLocalChecked();
        }

        if (PromiseCompletion::ForwardIfPromise(result, baton, ConfigurableGitDiffOptions::progressCb_promiseCompleted)) {
          return;
        }

             if (result.IsEmpty() || result->IsNativeError()) {
              baton->result = -1;
            }
            else if (!result->IsNull() && !result->IsUndefined()) {
               if (result->IsNumber()) {
                baton->result = Nan::To<int>(result).FromJust();
              }
              else {
                baton->result = baton->defaultResult;
              }
             }
            else {
              baton->result = baton->defaultResult;
            }
           baton->Done();
       }

      void ConfigurableGitDiffOptions::progressCb_promiseCompleted(bool isFulfilled, nodegit::AsyncBaton *_baton, v8::Local<v8::Value> result) {
        Nan::HandleScope scope;

        ProgressCbBaton* baton = static_cast<ProgressCbBaton*>(_baton);
           if (isFulfilled) {
              if (result.IsEmpty() || result->IsNativeError()) {
                baton->result = -1;
              }
              else if (!result->IsNull() && !result->IsUndefined()) {
                 if (result->IsNumber()) {
                  baton->result = Nan::To<int>(result).FromJust();
                }
                else{
                  baton->result = baton->defaultResult;
                }
               }
              else {
                baton->result = baton->defaultResult;
              }
           }
          else {
            // promise was rejected
               ConfigurableGitDiffOptions* instance = static_cast<ConfigurableGitDiffOptions*>(baton->   payload  );
             baton->SetCallbackError(result);
            baton->result = -1;
          }
          baton->Done();
       }
                 
// force base class template instantiation, to make sure we get all the
// methods, statics, etc.
template class nodegit::ConfigurableClassWrapper<GitDiffOptionsTraits>;
