#include "detach.h"
#include "../tensor.h"
#include <torch/torch.h>

namespace TensorOps {

Napi::Value Detach(Tensor* self, const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();

  try {
    // Detach from computation graph
    // This creates a new tensor that shares the same storage but doesn't track gradients
    torch::Tensor result = self->tensor.detach();
    return Tensor::NewInstance(env, result);
  } catch (const std::exception& e) {
    Napi::Error::New(env, e.what()).ThrowAsJavaScriptException();
    return env.Undefined();
  }
}

} // namespace TensorOps
