import init, {greet, GameWasm} from '../node_modules/word_grid/word_grid.js';
import {Game} from "./elements";
import {createRoot} from "react-dom/client";
import * as React from "react";

// @ts-ignore
const dictionary_url = new URL("../../resources/dictionary.csv", import.meta.url);

let dictionary_text: string = null;

async function run() {
    // First up we need to actually load the wasm file, so we use the
    // default export to inform it where the wasm file is located on the
    // server, and then we wait on the returned promise to wait for the
    // wasm to be loaded.
    //
    // It may look like this: `await init('./pkg/without_a_bundler_bg.wasm');`,
    // but there is also a handy default inside `init` function, which uses
    // `import.meta` to locate the wasm file relatively to js file.
    //
    // Note that instead of a string you can also pass in any of the
    // following things:
    //
    // * `WebAssembly.Module`
    //
    // * `ArrayBuffer`
    //
    // * `Response`
    //
    // * `Promise` which returns any of the above, e.g. `fetch("./path/to/wasm")`
    //
    // This gives you complete control over how the module is loaded
    // and compiled.
    //
    // Also note that the promise, when resolved, yields the wasm module's
    // exports which is the same as importing the `*_bg` module in other
    // modes
    await init();

    // need to retrieve the dictionary.csv file as text
    if(dictionary_text == null){

        dictionary_text = await fetch(dictionary_url).then((response) => {
            return response.text()
        })
    }

    let game = new GameWasm(BigInt(1234), dictionary_text);



    const cellTypes = game.get_board_cell_types();
    console.log({cellTypes});

    const root = createRoot(document.getElementById("root"));
    root.render(<Game wasm={game} />);

}

run();
