var window = {};
var WA = WXWebAssembly;
var WebAssembly = WA;
WebAssembly.RuntimeErrror = Error;
var performance = {
  now: Date.now,
};

// Local variable to store WASM path
var wasmFilePath = null;

// Store instantiateWasm parameters for later use
var pendingInstantiation = null;

// Exported function to set WASM path
Module['setWasmPath'] = (path) => {
  console.log("Setting WASM path:", path);
  wasmFilePath = path;

  // If instantiateWasm was called before the path was set, call it now
  if (pendingInstantiation) {
    var {info, receiveInstance} = pendingInstantiation;
    pendingInstantiation = null;
    doInstantiateWasm(info, receiveInstance);
  }
};

function doInstantiateWasm(info, receiveInstance) {
  console.log("loading wasm...", info);
  console.log("Using WASM path:", wasmFilePath);

  WebAssembly.instantiate(wasmFilePath, info).then((result) => {
    console.log("result:", result);
    var inst = result['instance'];
    receiveInstance(inst);
  });
}

Module['instantiateWasm'] = (info, receiveInstance) => {
  // If WASM path is already set, proceed immediately
  if (wasmFilePath) {
    doInstantiateWasm(info, receiveInstance);
  } else {
    // Otherwise, store parameters for later use
    console.log("WASM path not set yet, deferring instantiation...");
    pendingInstantiation = {info, receiveInstance};
  }
}
