// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
// DO NOT EDIT.
//
// To update this file, just rebuild your project or run
// `swift package bridge-js`.

export const PropertyEnumValues = {
    Value1: 0,
    Value2: 1,
};

export async function createInstantiator(options, swift) {
    let instance;
    let memory;
    let setException;
    let decodeString;
    const textDecoder = new TextDecoder("utf-8");
    const textEncoder = new TextEncoder("utf-8");
    let tmpRetString;
    let tmpRetBytes;
    let tmpRetException;
    let tmpRetOptionalBool;
    let tmpRetOptionalInt;
    let tmpRetOptionalFloat;
    let tmpRetOptionalDouble;
    let tmpRetOptionalHeapObject;
    let strStack = [];
    let i32Stack = [];
    let i64Stack = [];
    let f32Stack = [];
    let f64Stack = [];
    let ptrStack = [];
    const enumHelpers = {};
    const structHelpers = {};

    let _exports = null;
    let bjs = null;

    return {
        /**
         * @param {WebAssembly.Imports} importObject
         */
        addImports: (importObject, importsContext) => {
            bjs = {};
            importObject["bjs"] = bjs;
            bjs["swift_js_return_string"] = function(ptr, len) {
                tmpRetString = decodeString(ptr, len);
            }
            bjs["swift_js_init_memory"] = function(sourceId, bytesPtr) {
                const source = swift.memory.getObject(sourceId);
                swift.memory.release(sourceId);
                const bytes = new Uint8Array(memory.buffer, bytesPtr);
                bytes.set(source);
            }
            bjs["swift_js_make_js_string"] = function(ptr, len) {
                return swift.memory.retain(decodeString(ptr, len));
            }
            bjs["swift_js_init_memory_with_result"] = function(ptr, len) {
                const target = new Uint8Array(memory.buffer, ptr, len);
                target.set(tmpRetBytes);
                tmpRetBytes = undefined;
            }
            bjs["swift_js_throw"] = function(id) {
                tmpRetException = swift.memory.retainByRef(id);
            }
            bjs["swift_js_retain"] = function(id) {
                return swift.memory.retainByRef(id);
            }
            bjs["swift_js_release"] = function(id) {
                swift.memory.release(id);
            }
            bjs["swift_js_push_i32"] = function(v) {
                i32Stack.push(v | 0);
            }
            bjs["swift_js_push_f32"] = function(v) {
                f32Stack.push(Math.fround(v));
            }
            bjs["swift_js_push_f64"] = function(v) {
                f64Stack.push(v);
            }
            bjs["swift_js_push_string"] = function(ptr, len) {
                const value = decodeString(ptr, len);
                strStack.push(value);
            }
            bjs["swift_js_pop_i32"] = function() {
                return i32Stack.pop();
            }
            bjs["swift_js_pop_f32"] = function() {
                return f32Stack.pop();
            }
            bjs["swift_js_pop_f64"] = function() {
                return f64Stack.pop();
            }
            bjs["swift_js_push_pointer"] = function(pointer) {
                ptrStack.push(pointer);
            }
            bjs["swift_js_pop_pointer"] = function() {
                return ptrStack.pop();
            }
            bjs["swift_js_push_i64"] = function(v) {
                i64Stack.push(v);
            }
            bjs["swift_js_pop_i64"] = function() {
                return i64Stack.pop();
            }
            bjs["swift_js_return_optional_bool"] = function(isSome, value) {
                if (isSome === 0) {
                    tmpRetOptionalBool = null;
                } else {
                    tmpRetOptionalBool = value !== 0;
                }
            }
            bjs["swift_js_return_optional_int"] = function(isSome, value) {
                if (isSome === 0) {
                    tmpRetOptionalInt = null;
                } else {
                    tmpRetOptionalInt = value | 0;
                }
            }
            bjs["swift_js_return_optional_float"] = function(isSome, value) {
                if (isSome === 0) {
                    tmpRetOptionalFloat = null;
                } else {
                    tmpRetOptionalFloat = Math.fround(value);
                }
            }
            bjs["swift_js_return_optional_double"] = function(isSome, value) {
                if (isSome === 0) {
                    tmpRetOptionalDouble = null;
                } else {
                    tmpRetOptionalDouble = value;
                }
            }
            bjs["swift_js_return_optional_string"] = function(isSome, ptr, len) {
                if (isSome === 0) {
                    tmpRetString = null;
                } else {
                    tmpRetString = decodeString(ptr, len);
                }
            }
            bjs["swift_js_return_optional_object"] = function(isSome, objectId) {
                if (isSome === 0) {
                    tmpRetString = null;
                } else {
                    tmpRetString = swift.memory.getObject(objectId);
                }
            }
            bjs["swift_js_return_optional_heap_object"] = function(isSome, pointer) {
                if (isSome === 0) {
                    tmpRetOptionalHeapObject = null;
                } else {
                    tmpRetOptionalHeapObject = pointer;
                }
            }
            bjs["swift_js_get_optional_int_presence"] = function() {
                return tmpRetOptionalInt != null ? 1 : 0;
            }
            bjs["swift_js_get_optional_int_value"] = function() {
                const value = tmpRetOptionalInt;
                tmpRetOptionalInt = undefined;
                return value;
            }
            bjs["swift_js_get_optional_string"] = function() {
                const str = tmpRetString;
                tmpRetString = undefined;
                if (str == null) {
                    return -1;
                } else {
                    const bytes = textEncoder.encode(str);
                    tmpRetBytes = bytes;
                    return bytes.length;
                }
            }
            bjs["swift_js_get_optional_float_presence"] = function() {
                return tmpRetOptionalFloat != null ? 1 : 0;
            }
            bjs["swift_js_get_optional_float_value"] = function() {
                const value = tmpRetOptionalFloat;
                tmpRetOptionalFloat = undefined;
                return value;
            }
            bjs["swift_js_get_optional_double_presence"] = function() {
                return tmpRetOptionalDouble != null ? 1 : 0;
            }
            bjs["swift_js_get_optional_double_value"] = function() {
                const value = tmpRetOptionalDouble;
                tmpRetOptionalDouble = undefined;
                return value;
            }
            bjs["swift_js_get_optional_heap_object_pointer"] = function() {
                const pointer = tmpRetOptionalHeapObject;
                tmpRetOptionalHeapObject = undefined;
                return pointer || 0;
            }
            bjs["swift_js_closure_unregister"] = function(funcRef) {}
            // Wrapper functions for module: TestModule
            if (!importObject["TestModule"]) {
                importObject["TestModule"] = {};
            }
            importObject["TestModule"]["bjs_PropertyClass_wrap"] = function(pointer) {
                const obj = _exports['PropertyClass'].__construct(pointer);
                return swift.memory.retain(obj);
            };
        },
        setInstance: (i) => {
            instance = i;
            memory = instance.exports.memory;

            decodeString = (ptr, len) => { const bytes = new Uint8Array(memory.buffer, ptr >>> 0, len >>> 0); return textDecoder.decode(bytes); }

            setException = (error) => {
                instance.exports._swift_js_exception.value = swift.memory.retain(error)
            }
        },
        /** @param {WebAssembly.Instance} instance */
        createExports: (instance) => {
            const js = swift.memory.heap;
            const swiftHeapObjectFinalizationRegistry = (typeof FinalizationRegistry === "undefined") ? { register: () => {}, unregister: () => {} } : new FinalizationRegistry((state) => {
                if (state.hasReleased) {
                    return;
                }
                state.hasReleased = true;
                state.deinit(state.pointer);
            });

            /// Represents a Swift heap object like a class instance or an actor instance.
            class SwiftHeapObject {
                static __wrap(pointer, deinit, prototype) {
                    const obj = Object.create(prototype);
                    const state = { pointer, deinit, hasReleased: false };
                    obj.pointer = pointer;
                    obj.__swiftHeapObjectState = state;
                    swiftHeapObjectFinalizationRegistry.register(obj, state, state);
                    return obj;
                }

                release() {
                    const state = this.__swiftHeapObjectState;
                    if (state.hasReleased) {
                        return;
                    }
                    state.hasReleased = true;
                    swiftHeapObjectFinalizationRegistry.unregister(state);
                    state.deinit(state.pointer);
                }
            }
            class PropertyClass extends SwiftHeapObject {
                static __construct(ptr) {
                    return SwiftHeapObject.__wrap(ptr, instance.exports.bjs_PropertyClass_deinit, PropertyClass.prototype);
                }

                constructor() {
                    const ret = instance.exports.bjs_PropertyClass_init();
                    return PropertyClass.__construct(ret);
                }
                static get staticConstant() {
                    instance.exports.bjs_PropertyClass_static_staticConstant_get();
                    const ret = tmpRetString;
                    tmpRetString = undefined;
                    return ret;
                }
                static get staticVariable() {
                    const ret = instance.exports.bjs_PropertyClass_static_staticVariable_get();
                    return ret;
                }
                static set staticVariable(value) {
                    instance.exports.bjs_PropertyClass_static_staticVariable_set(value);
                }
                static get jsObjectProperty() {
                    const ret = instance.exports.bjs_PropertyClass_static_jsObjectProperty_get();
                    const ret1 = swift.memory.getObject(ret);
                    swift.memory.release(ret);
                    return ret1;
                }
                static set jsObjectProperty(value) {
                    instance.exports.bjs_PropertyClass_static_jsObjectProperty_set(swift.memory.retain(value));
                }
                static get classVariable() {
                    instance.exports.bjs_PropertyClass_static_classVariable_get();
                    const ret = tmpRetString;
                    tmpRetString = undefined;
                    return ret;
                }
                static set classVariable(value) {
                    const valueBytes = textEncoder.encode(value);
                    const valueId = swift.memory.retain(valueBytes);
                    instance.exports.bjs_PropertyClass_static_classVariable_set(valueId, valueBytes.length);
                }
                static get computedProperty() {
                    instance.exports.bjs_PropertyClass_static_computedProperty_get();
                    const ret = tmpRetString;
                    tmpRetString = undefined;
                    return ret;
                }
                static set computedProperty(value) {
                    const valueBytes = textEncoder.encode(value);
                    const valueId = swift.memory.retain(valueBytes);
                    instance.exports.bjs_PropertyClass_static_computedProperty_set(valueId, valueBytes.length);
                }
                static get readOnlyComputed() {
                    const ret = instance.exports.bjs_PropertyClass_static_readOnlyComputed_get();
                    return ret;
                }
                static get optionalProperty() {
                    instance.exports.bjs_PropertyClass_static_optionalProperty_get();
                    const optResult = tmpRetString;
                    tmpRetString = undefined;
                    return optResult;
                }
                static set optionalProperty(value) {
                    const isSome = value != null;
                    let result, result1;
                    if (isSome) {
                        const valueBytes = textEncoder.encode(value);
                        const valueId = swift.memory.retain(valueBytes);
                        result = valueId;
                        result1 = valueBytes.length;
                    } else {
                        result = 0;
                        result1 = 0;
                    }
                    instance.exports.bjs_PropertyClass_static_optionalProperty_set(+isSome, result, result1);
                }
            }
            if (typeof globalThis.PropertyNamespace === 'undefined') {
                globalThis.PropertyNamespace = {};
            }
            if (typeof globalThis.PropertyNamespace.Nested === 'undefined') {
                globalThis.PropertyNamespace.Nested = {};
            }
            const exports = {
                PropertyClass,
                PropertyEnum: {
                    ...PropertyEnumValues,
                    get enumProperty() {
                        instance.exports.bjs_PropertyEnum_static_enumProperty_get();
                        const ret = tmpRetString;
                        tmpRetString = undefined;
                        return ret;
                    },
                    set enumProperty(value) {
                        const valueBytes = textEncoder.encode(value);
                        const valueId = swift.memory.retain(valueBytes);
                        instance.exports.bjs_PropertyEnum_static_enumProperty_set(valueId, valueBytes.length);
                    },
                    get enumConstant() {
                        const ret = instance.exports.bjs_PropertyEnum_static_enumConstant_get();
                        return ret;
                    },
                    get computedEnum() {
                        instance.exports.bjs_PropertyEnum_static_computedEnum_get();
                        const ret = tmpRetString;
                        tmpRetString = undefined;
                        return ret;
                    },
                    set computedEnum(value) {
                        const valueBytes = textEncoder.encode(value);
                        const valueId = swift.memory.retain(valueBytes);
                        instance.exports.bjs_PropertyEnum_static_computedEnum_set(valueId, valueBytes.length);
                    }
                },
                PropertyNamespace: {
                    get namespaceProperty() {
                        instance.exports.bjs_PropertyNamespace_static_namespaceProperty_get();
                        const ret = tmpRetString;
                        tmpRetString = undefined;
                        return ret;
                    },
                    set namespaceProperty(value) {
                        const valueBytes = textEncoder.encode(value);
                        const valueId = swift.memory.retain(valueBytes);
                        instance.exports.bjs_PropertyNamespace_static_namespaceProperty_set(valueId, valueBytes.length);
                    },
                    get namespaceConstant() {
                        instance.exports.bjs_PropertyNamespace_static_namespaceConstant_get();
                        const ret = tmpRetString;
                        tmpRetString = undefined;
                        return ret;
                    },
                    Nested: {
                        get nestedProperty() {
                            const ret = instance.exports.bjs_PropertyNamespace_Nested_static_nestedProperty_get();
                            return ret;
                        },
                        set nestedProperty(value) {
                            instance.exports.bjs_PropertyNamespace_Nested_static_nestedProperty_set(value);
                        },
                        get nestedConstant() {
                            instance.exports.bjs_PropertyNamespace_Nested_static_nestedConstant_get();
                            const ret = tmpRetString;
                            tmpRetString = undefined;
                            return ret;
                        },
                        get nestedDouble() {
                            const ret = instance.exports.bjs_PropertyNamespace_Nested_static_nestedDouble_get();
                            return ret;
                        },
                        set nestedDouble(value) {
                            instance.exports.bjs_PropertyNamespace_Nested_static_nestedDouble_set(value);
                        },
                    },
                },
            };
            _exports = exports;
            Object.defineProperty(globalThis.PropertyNamespace, 'namespaceProperty', {
                get: () => exports.PropertyNamespace.namespaceProperty,
                set: (value) => { exports.PropertyNamespace.namespaceProperty = value; }
            });
            Object.defineProperty(globalThis.PropertyNamespace, 'namespaceConstant', {
                get: () => exports.PropertyNamespace.namespaceConstant,
            });
            Object.defineProperty(globalThis.PropertyNamespace.Nested, 'nestedProperty', {
                get: () => exports.PropertyNamespace.Nested.nestedProperty,
                set: (value) => { exports.PropertyNamespace.Nested.nestedProperty = value; }
            });
            Object.defineProperty(globalThis.PropertyNamespace.Nested, 'nestedConstant', {
                get: () => exports.PropertyNamespace.Nested.nestedConstant,
            });
            Object.defineProperty(globalThis.PropertyNamespace.Nested, 'nestedDouble', {
                get: () => exports.PropertyNamespace.Nested.nestedDouble,
                set: (value) => { exports.PropertyNamespace.Nested.nestedDouble = value; }
            });
            return exports;
        },
    }
}