/* tslint:disable */
import * as wasm from './memo_js_bg';
import { AsyncIteratorWrapper } from './support';
import { GitProviderWrapper } from './support';
import { ChangeObserver } from './support';

const __wbg_next_36898abb8b1d4689_target = AsyncIteratorWrapper.prototype.next || function() {
    throw new Error(`wasm-bindgen: AsyncIteratorWrapper.prototype.next does not exist`);
};

const stack = [];

const slab = [{ obj: undefined }, { obj: null }, { obj: true }, { obj: false }];

function getObject(idx) {
    if ((idx & 1) === 1) {
        return stack[idx >> 1];
    } else {
        const val = slab[idx >> 1];

        return val.obj;

    }
}

let slab_next = slab.length;

function addHeapObject(obj) {
    if (slab_next === slab.length) slab.push(slab.length + 1);
    const idx = slab_next;
    const next = slab[idx];

    slab_next = next;

    slab[idx] = { obj, cnt: 1 };
    return idx << 1;
}

export function __wbg_next_36898abb8b1d4689(arg0) {
    return addHeapObject(__wbg_next_36898abb8b1d4689_target.call(getObject(arg0)));
}

const __wbg_baseEntries_472995b0f2e03ea8_target = GitProviderWrapper.prototype.baseEntries || function() {
    throw new Error(`wasm-bindgen: GitProviderWrapper.prototype.baseEntries does not exist`);
};

const lTextDecoder = typeof TextDecoder === 'undefined' ? require('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8');

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

function getStringFromWasm(ptr, len) {
    return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
}

export function __wbg_baseEntries_472995b0f2e03ea8(arg0, arg1, arg2) {
    let varg1 = getStringFromWasm(arg1, arg2);
    return addHeapObject(__wbg_baseEntries_472995b0f2e03ea8_target.call(getObject(arg0), varg1));
}

const __wbg_baseText_ec612619fbf25c04_target = GitProviderWrapper.prototype.baseText || function() {
    throw new Error(`wasm-bindgen: GitProviderWrapper.prototype.baseText does not exist`);
};

export function __wbg_baseText_ec612619fbf25c04(arg0, arg1, arg2, arg3, arg4) {
    let varg1 = getStringFromWasm(arg1, arg2);
    let varg3 = getStringFromWasm(arg3, arg4);
    return addHeapObject(__wbg_baseText_ec612619fbf25c04_target.call(getObject(arg0), varg1, varg3));
}

const __wbg_textChanged_0eb193e1edd4ba9b_target = ChangeObserver.prototype.textChanged || function() {
    throw new Error(`wasm-bindgen: ChangeObserver.prototype.textChanged does not exist`);
};

function dropRef(idx) {

    idx = idx >> 1;
    if (idx < 4) return;
    let obj = slab[idx];

    obj.cnt -= 1;
    if (obj.cnt > 0) return;

    // If we hit 0 then free up our space in the slab
    slab[idx] = slab_next;
    slab_next = idx;
}

function takeObject(idx) {
    const ret = getObject(idx);
    dropRef(idx);
    return ret;
}

export function __wbg_textChanged_0eb193e1edd4ba9b(arg0, arg1, arg2) {
    __wbg_textChanged_0eb193e1edd4ba9b_target.call(getObject(arg0), takeObject(arg1), takeObject(arg2));
}

function passArray8ToWasm(arg) {
    const ptr = wasm.__wbindgen_malloc(arg.length * 1);
    getUint8Memory().set(arg, ptr / 1);
    return [ptr, arg.length];
}

const lTextEncoder = typeof TextEncoder === 'undefined' ? require('util').TextEncoder : TextEncoder;

let cachedTextEncoder = new lTextEncoder('utf-8');

function passStringToWasm(arg) {

    const buf = cachedTextEncoder.encode(arg);
    const ptr = wasm.__wbindgen_malloc(buf.length);
    getUint8Memory().set(buf, ptr);
    return [ptr, buf.length];
}

function getArrayU8FromWasm(ptr, len) {
    return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
}

let cachedGlobalArgumentPtr = null;
function globalArgumentPtr() {
    if (cachedGlobalArgumentPtr === null) {
        cachedGlobalArgumentPtr = wasm.__wbindgen_global_argument_ptr();
    }
    return cachedGlobalArgumentPtr;
}

let cachegetUint32Memory = null;
function getUint32Memory() {
    if (cachegetUint32Memory === null || cachegetUint32Memory.buffer !== wasm.memory.buffer) {
        cachegetUint32Memory = new Uint32Array(wasm.memory.buffer);
    }
    return cachegetUint32Memory;
}

const __wbg_values_a85d527de860eeb6_target = Array.prototype.values || function() {
    throw new Error(`wasm-bindgen: Array.prototype.values does not exist`);
};

export function __wbg_values_a85d527de860eeb6(arg0) {
    return addHeapObject(__wbg_values_a85d527de860eeb6_target.call(getObject(arg0)));
}

const __wbg_call_eeece939fc9a62db_target = Function.prototype.call || function() {
    throw new Error(`wasm-bindgen: Function.prototype.call does not exist`);
};

export function __wbg_call_eeece939fc9a62db(arg0, arg1, arg2, exnptr) {
    try {
        return addHeapObject(__wbg_call_eeece939fc9a62db_target.call(getObject(arg0), getObject(arg1), getObject(arg2)));
    } catch (e) {
        const view = getUint32Memory();
        view[exnptr / 4] = 1;
        view[exnptr / 4 + 1] = addHeapObject(e);

    }
}

const __wbg_next_da9f5778cde00c4e_target = function() {
    return this.next();
};

export function __wbg_next_da9f5778cde00c4e(arg0, exnptr) {
    try {
        return addHeapObject(__wbg_next_da9f5778cde00c4e_target.call(getObject(arg0)));
    } catch (e) {
        const view = getUint32Memory();
        view[exnptr / 4] = 1;
        view[exnptr / 4 + 1] = addHeapObject(e);

    }
}

const __wbg_done_da646dabefc0c0ce_target = function() {
    return this.done;
};

export function __wbg_done_da646dabefc0c0ce(arg0) {
    return __wbg_done_da646dabefc0c0ce_target.call(getObject(arg0)) ? 1 : 0;
}

const __wbg_value_0d5b146abcfc1b1e_target = function() {
    return this.value;
};

export function __wbg_value_0d5b146abcfc1b1e(arg0) {
    return addHeapObject(__wbg_value_0d5b146abcfc1b1e_target.call(getObject(arg0)));
}

export function __wbg_new_6b7f4334f7b728fd() {
    return addHeapObject(new Object());
}

const __wbg_set_6f4fee47694f666d_target = Reflect.set.bind(Reflect) || function() {
    throw new Error(`wasm-bindgen: Reflect.set.bind(Reflect) does not exist`);
};

export function __wbg_set_6f4fee47694f666d(arg0, arg1, arg2, exnptr) {
    try {
        return __wbg_set_6f4fee47694f666d_target(getObject(arg0), getObject(arg1), getObject(arg2)) ? 1 : 0;
    } catch (e) {
        const view = getUint32Memory();
        view[exnptr / 4] = 1;
        view[exnptr / 4 + 1] = addHeapObject(e);

    }
}

export function __wbg_instanceof_Uint8Array_5e844eb7e252dfda(idx) {
    return getObject(idx) instanceof Uint8Array ? 1 : 0;
}

const __wbg_forEach_d292802bb5b926c3_target = Uint8Array.prototype.forEach || function() {
    throw new Error(`wasm-bindgen: Uint8Array.prototype.forEach does not exist`);
};

function getGlobalArgument(arg) {
    const idx = globalArgumentPtr() / 4 + arg;
    return getUint32Memory()[idx];
}

export function __wbg_forEach_d292802bb5b926c3(arg0, arg1) {
    let cbarg1 = function(arg0, arg1, arg2) {
        let a = this.a;
        this.a = 0;
        try {
            return this.f(a, this.b, arg0, arg1, addHeapObject(arg2));

        } finally {
            this.a = a;

        }

    };
    cbarg1.f = wasm.__wbg_function_table.get(arg1);
    cbarg1.a = getGlobalArgument(0);
    cbarg1.b = getGlobalArgument(0 + 1);
    try {
        __wbg_forEach_d292802bb5b926c3_target.call(getObject(arg0), cbarg1.bind(cbarg1));
    } finally {
        cbarg1.a = cbarg1.b = 0;

    }
}

function GetOwnOrInheritedPropertyDescriptor(obj, id) {
    while (obj) {
        let desc = Object.getOwnPropertyDescriptor(obj, id);
        if (desc) return desc;
        obj = Object.getPrototypeOf(obj);
    }
    throw new Error(`descriptor for id='${id}' not found`);
}

const __wbg_byteLength_cb67287a305c26e9_target = GetOwnOrInheritedPropertyDescriptor(Uint8Array.prototype, 'byteLength').get || function() {
    throw new Error(`wasm-bindgen: GetOwnOrInheritedPropertyDescriptor(Uint8Array.prototype, 'byteLength').get does not exist`);
};

export function __wbg_byteLength_cb67287a305c26e9(arg0) {
    return __wbg_byteLength_cb67287a305c26e9_target.call(getObject(arg0));
}

export function __wbg_new_ea60e716adf807fe(arg0) {
    let cbarg0 = function(arg0, arg1) {
        let a = this.a;
        this.a = 0;
        try {
            return this.f(a, this.b, addHeapObject(arg0), addHeapObject(arg1));

        } finally {
            this.a = a;

        }

    };
    cbarg0.f = wasm.__wbg_function_table.get(arg0);
    cbarg0.a = getGlobalArgument(0);
    cbarg0.b = getGlobalArgument(0 + 1);
    try {
        return addHeapObject(new Promise(cbarg0.bind(cbarg0)));
    } finally {
        cbarg0.a = cbarg0.b = 0;

    }
}

const __wbg_then_074a42f44879a676_target = Promise.prototype.then || function() {
    throw new Error(`wasm-bindgen: Promise.prototype.then does not exist`);
};

export function __wbg_then_074a42f44879a676(arg0, arg1, arg2) {
    return addHeapObject(__wbg_then_074a42f44879a676_target.call(getObject(arg0), getObject(arg1), getObject(arg2)));
}

function freeStreamToAsyncIterator(ptr) {

    wasm.__wbg_streamtoasynciterator_free(ptr);
}
/**
*/
export class StreamToAsyncIterator {

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

        return obj;
    }

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

    /**
    * @returns {any}
    */
    next() {
        return takeObject(wasm.streamtoasynciterator_next(this.ptr));
    }
}

export function __wbg_operationenvelope_new(ptr) {
    return addHeapObject(OperationEnvelope.__wrap(ptr));
}

function freeOperationEnvelope(ptr) {

    wasm.__wbg_operationenvelope_free(ptr);
}
/**
*/
export class OperationEnvelope {

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

        return obj;
    }

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

    /**
    * @returns {any}
    */
    epochReplicaId() {
        return takeObject(wasm.operationenvelope_epochReplicaId(this.ptr));
    }
    /**
    * @returns {any}
    */
    epochTimestamp() {
        return takeObject(wasm.operationenvelope_epochTimestamp(this.ptr));
    }
    /**
    * @returns {Uint8Array}
    */
    operation() {
        const retptr = globalArgumentPtr();
        wasm.operationenvelope_operation(retptr, this.ptr);
        const mem = getUint32Memory();
        const rustptr = mem[retptr / 4];
        const rustlen = mem[retptr / 4 + 1];

        const realRet = getArrayU8FromWasm(rustptr, rustlen).slice();
        wasm.__wbindgen_free(rustptr, rustlen * 1);
        return realRet;

    }
}

function freeWorkTreeNewResult(ptr) {

    wasm.__wbg_worktreenewresult_free(ptr);
}
/**
*/
export class WorkTreeNewResult {

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

        return obj;
    }

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

    /**
    * @returns {WorkTree}
    */
    tree() {
        return WorkTree.__wrap(wasm.worktreenewresult_tree(this.ptr));
    }
    /**
    * @returns {StreamToAsyncIterator}
    */
    operations() {
        return StreamToAsyncIterator.__wrap(wasm.worktreenewresult_operations(this.ptr));
    }
}

function freeWorkTree(ptr) {

    wasm.__wbg_worktree_free(ptr);
}
/**
*/
export class WorkTree {

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

        return obj;
    }

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

    /**
    * @param {any} arg0
    * @param {any} arg1
    * @param {Uint8Array} arg2
    * @param {any} arg3
    * @param {any} arg4
    * @returns {WorkTreeNewResult}
    */
    static new(arg0, arg1, arg2, arg3, arg4) {
        const [ptr2, len2] = passArray8ToWasm(arg2);
        try {
            return WorkTreeNewResult.__wrap(wasm.worktree_new(addHeapObject(arg0), addHeapObject(arg1), ptr2, len2, addHeapObject(arg3), addHeapObject(arg4)));

        } finally {
            wasm.__wbindgen_free(ptr2, len2 * 1);

        }

    }
    /**
    * @param {any} arg0
    * @returns {StreamToAsyncIterator}
    */
    reset(arg0) {
        return StreamToAsyncIterator.__wrap(wasm.worktree_reset(this.ptr, addHeapObject(arg0)));
    }
    /**
    * @param {any} arg0
    * @returns {StreamToAsyncIterator}
    */
    apply_ops(arg0) {
        return StreamToAsyncIterator.__wrap(wasm.worktree_apply_ops(this.ptr, addHeapObject(arg0)));
    }
    /**
    * @param {string} arg0
    * @param {any} arg1
    * @returns {OperationEnvelope}
    */
    create_file(arg0, arg1) {
        const [ptr0, len0] = passStringToWasm(arg0);
        return OperationEnvelope.__wrap(wasm.worktree_create_file(this.ptr, ptr0, len0, addHeapObject(arg1)));
    }
    /**
    * @param {string} arg0
    * @param {string} arg1
    * @returns {OperationEnvelope}
    */
    rename(arg0, arg1) {
        const [ptr0, len0] = passStringToWasm(arg0);
        const [ptr1, len1] = passStringToWasm(arg1);
        return OperationEnvelope.__wrap(wasm.worktree_rename(this.ptr, ptr0, len0, ptr1, len1));
    }
    /**
    * @param {string} arg0
    * @returns {OperationEnvelope}
    */
    remove(arg0) {
        const [ptr0, len0] = passStringToWasm(arg0);
        return OperationEnvelope.__wrap(wasm.worktree_remove(this.ptr, ptr0, len0));
    }
    /**
    * @param {string} arg0
    * @returns {any}
    */
    open_text_file(arg0) {
        const [ptr0, len0] = passStringToWasm(arg0);
        return takeObject(wasm.worktree_open_text_file(this.ptr, ptr0, len0));
    }
    /**
    * @param {any} arg0
    * @returns {any}
    */
    text(arg0) {
        return takeObject(wasm.worktree_text(this.ptr, addHeapObject(arg0)));
    }
    /**
    * @param {any} arg0
    * @param {any} arg1
    * @param {string} arg2
    * @returns {OperationEnvelope}
    */
    edit(arg0, arg1, arg2) {
        const [ptr2, len2] = passStringToWasm(arg2);
        try {
            return OperationEnvelope.__wrap(wasm.worktree_edit(this.ptr, addHeapObject(arg0), addHeapObject(arg1), ptr2, len2));

        } finally {
            wasm.__wbindgen_free(ptr2, len2 * 1);

        }

    }
    /**
    * @param {any} arg0
    * @param {boolean} arg1
    * @returns {any}
    */
    entries(arg0, arg1) {
        return takeObject(wasm.worktree_entries(this.ptr, addHeapObject(arg0), arg1 ? 1 : 0));
    }
}

export function __wbindgen_object_drop_ref(i) {
    dropRef(i);
}

export function __wbindgen_string_new(p, l) {
    return addHeapObject(getStringFromWasm(p, l));
}

export function __wbindgen_number_get(n, invalid) {
    let obj = getObject(n);
    if (typeof(obj) === 'number') return obj;
    getUint8Memory()[invalid] = 1;
    return 0;
}

export function __wbindgen_is_null(idx) {
    return getObject(idx) === null ? 1 : 0;
}

export function __wbindgen_is_undefined(idx) {
    return getObject(idx) === undefined ? 1 : 0;
}

export function __wbindgen_boolean_get(i) {
    let v = getObject(i);
    if (typeof(v) === 'boolean') {
        return v ? 1 : 0;
    } else {
        return 2;
    }
}

export function __wbindgen_is_symbol(i) {
    return typeof(getObject(i)) === 'symbol' ? 1 : 0;
}

export function __wbindgen_string_get(i, len_ptr) {
    let obj = getObject(i);
    if (typeof(obj) !== 'string') return 0;
    const [ptr, len] = passStringToWasm(obj);
    getUint32Memory()[len_ptr / 4] = len;
    return ptr;
}

export function __wbindgen_cb_drop(i) {
    const obj = getObject(i).original;
    dropRef(i);
    if (obj.cnt-- == 1) {
        obj.a = 0;
        return 1;
    }
    return 0;
}

export function __wbindgen_json_parse(ptr, len) {
    return addHeapObject(JSON.parse(getStringFromWasm(ptr, len)));
}

export function __wbindgen_json_serialize(idx, ptrptr) {
    const [ptr, len] = passStringToWasm(JSON.stringify(getObject(idx)));
    getUint32Memory()[ptrptr / 4] = ptr;
    return len;
}

export function __wbindgen_rethrow(idx) { throw takeObject(idx); }

export function __wbindgen_closure_wrapper270(a, b, fi, di, _ignored) {
    const f = wasm.__wbg_function_table.get(fi);
    const d = wasm.__wbg_function_table.get(di);
    const cb = function(arg0) {
        this.cnt++;
        let a = this.a;
        this.a = 0;
        try {
            return f(a, b, addHeapObject(arg0));

        } finally {
            this.a = a;
            if (this.cnt-- == 1) d(this.a, b);

        }

    };
    cb.a = a;
    cb.cnt = 1;
    let real = cb.bind(cb);
    real.original = cb;
    return addHeapObject(real);
}

export function __wbindgen_throw(ptr, len) {
    throw new Error(getStringFromWasm(ptr, len));
}

