#include "mul.h"
#include "../tensor.h"
#include "../utils.h"
#include <torch/torch.h>

namespace TensorOps {

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

  if (info.Length() < 1) {
    Napi::TypeError::New(env, "Expected at least 1 argument").ThrowAsJavaScriptException();
    return env.Null();
  }

  torch::Tensor result;

  // Check if argument is a number (scalar) or Tensor
  if (info[0].IsNumber()) {
    double scalar = info[0].As<Napi::Number>().DoubleValue();
    result = self->tensor * scalar;
  } else if (info[0].IsObject()) {
    Tensor* other = Napi::ObjectWrap<Tensor>::Unwrap(info[0].As<Napi::Object>());
    result = self->tensor * other->tensor;
  } else {
    Napi::TypeError::New(env, "Expected Tensor or number").ThrowAsJavaScriptException();
    return env.Null();
  }

  return Tensor::NewInstance(env, result);
}

}  // namespace TensorOps
