
let wasm;

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

let cachegetUint8Memory0 = null;
function getUint8Memory0() {
    if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
        cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
    }
    return cachegetUint8Memory0;
}

function getStringFromWasm0(ptr, len) {
    return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

function isLikeNone(x) {
    return x === undefined || x === null;
}
/**
*    Creates a new Game State.
* @returns {State}
*/
export function new_game() {
    var ret = wasm.new_game();
    return State.__wrap(ret);
}

function _assertClass(instance, klass) {
    if (!(instance instanceof klass)) {
        throw new Error(`expected instance of ${klass.name}`);
    }
    return instance.ptr;
}
/**
* @param {State} state
* @param {number} index
*/
export function set_mark(state, index) {
    _assertClass(state, State);
    wasm.set_mark(state.ptr, index);
}

/**
* @param {State} state
* @param {number} step
*/
export function rewind(state, step) {
    _assertClass(state, State);
    wasm.rewind(state.ptr, step);
}

let cachegetInt32Memory0 = null;
function getInt32Memory0() {
    if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
        cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
    }
    return cachegetInt32Memory0;
}

function getArrayU8FromWasm0(ptr, len) {
    return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
*    Returns the Game Board at the current step.
*    The WASM doens't support exporting the history array,
*    THis will return the current board as a Box.
* @param {State} state
* @returns {Uint8Array}
*/
export function get_board(state) {
    try {
        const retptr = wasm.__wbindgen_export_0.value - 16;
        wasm.__wbindgen_export_0.value = retptr;
        _assertClass(state, State);
        wasm.get_board(retptr, state.ptr);
        var r0 = getInt32Memory0()[retptr / 4 + 0];
        var r1 = getInt32Memory0()[retptr / 4 + 1];
        var v0 = getArrayU8FromWasm0(r0, r1).slice();
        wasm.__wbindgen_free(r0, r1 * 1);
        return v0;
    } finally {
        wasm.__wbindgen_export_0.value += 16;
    }
}

/**
*    Returns the winner u8 or None
*    Copied from the react tutorial and converted to rust.
* @param {State} state
* @returns {number | undefined}
*/
export function get_winner(state) {
    _assertClass(state, State);
    var ret = wasm.get_winner(state.ptr);
    return ret === 0xFFFFFF ? undefined : ret;
}

/**
*/
export class State {

    static __wrap(ptr) {
        const obj = Object.create(State.prototype);
        obj.ptr = ptr;

        return obj;
    }

    free() {
        const ptr = this.ptr;
        this.ptr = 0;

        wasm.__wbg_state_free(ptr);
    }
    /**
    * @returns {number}
    */
    get step_number() {
        var ret = wasm.__wbg_get_state_step_number(this.ptr);
        return ret >>> 0;
    }
    /**
    * @param {number} arg0
    */
    set step_number(arg0) {
        wasm.__wbg_set_state_step_number(this.ptr, arg0);
    }
    /**
    * @returns {boolean}
    */
    get is_x_next() {
        var ret = wasm.__wbg_get_state_is_x_next(this.ptr);
        return ret !== 0;
    }
    /**
    * @param {boolean} arg0
    */
    set is_x_next(arg0) {
        wasm.__wbg_set_state_is_x_next(this.ptr, arg0);
    }
    /**
    * @returns {number | undefined}
    */
    get winner() {
        var ret = wasm.__wbg_get_state_winner(this.ptr);
        return ret === 0xFFFFFF ? undefined : ret;
    }
    /**
    * @param {number | undefined} arg0
    */
    set winner(arg0) {
        wasm.__wbg_set_state_winner(this.ptr, isLikeNone(arg0) ? 0xFFFFFF : arg0);
    }
}

async function load(module, imports) {
    if (typeof Response === 'function' && module instanceof Response) {

        if (typeof WebAssembly.instantiateStreaming === 'function') {
            try {
                return await WebAssembly.instantiateStreaming(module, imports);

            } catch (e) {
                if (module.headers.get('Content-Type') != 'application/wasm') {
                    console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);

                } else {
                    throw e;
                }
            }
        }

        const bytes = await module.arrayBuffer();
        return await WebAssembly.instantiate(bytes, imports);

    } else {

        const instance = await WebAssembly.instantiate(module, imports);

        if (instance instanceof WebAssembly.Instance) {
            return { instance, module };

        } else {
            return instance;
        }
    }
}

async function init(input) {
    if (typeof input === 'undefined') {
        input = import.meta.url.replace(/\.js$/, '_bg.wasm');
    }
    const imports = {};
    imports.wbg = {};
    imports.wbg.__wbindgen_throw = function(arg0, arg1) {
        throw new Error(getStringFromWasm0(arg0, arg1));
    };

    if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
        input = fetch(input);
    }

    const { instance, module } = await load(await input, imports);

    wasm = instance.exports;
    init.__wbindgen_wasm_module = module;

    return wasm;
}

export default init;

