#import <atomic>
#import <optional>

#import <bare.h>
#import <js.h>
#import <jstl.h>

#import <CoreBluetooth/CoreBluetooth.h>
#import <Foundation/Foundation.h>

struct bare_bluetooth_apple_external_t {
  CFTypeRef ref;
  std::string uuid;
};

struct bare_bluetooth_apple_peripheral_name_change_t {
  std::optional<std::string> name;
};

struct bare_bluetooth_apple_peripheral_services_discover_t {
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_peripheral_characteristics_discover_t {
  CFTypeRef service;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_peripheral_read_t {
  CFTypeRef characteristic;
  std::string uuid;
  void *data;
  size_t data_len;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_peripheral_write_t {
  CFTypeRef characteristic;
  std::string uuid;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_peripheral_notify_t {
  CFTypeRef characteristic;
  std::string uuid;
  void *data;
  size_t data_len;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_peripheral_notify_state_t {
  CFTypeRef characteristic;
  std::string uuid;
  bool is_notifying;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_peripheral_channel_open_t {
  CFTypeRef channel;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_server_state_change_t {
  int32_t state;
};

struct bare_bluetooth_apple_server_add_service_t {
  CFTypeRef service;
  std::string uuid;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_server_read_request_t {
  CFTypeRef request;
};

struct bare_bluetooth_apple_server_write_requests_t {
  uint32_t count;
  CFTypeRef *requests;
};

struct bare_bluetooth_apple_server_subscribe_t {
  CFTypeRef central;
  std::string characteristic_uuid;
};

struct bare_bluetooth_apple_server_unsubscribe_t {
  CFTypeRef central;
  std::string characteristic_uuid;
};

struct bare_bluetooth_apple_server_channel_publish_t {
  uint16_t psm;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_server_channel_open_t {
  CFTypeRef channel;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_server_advertise_error_t {
  std::string error;
};

struct bare_bluetooth_apple_central_state_change_t {
  int32_t state;
};

struct bare_bluetooth_apple_central_discover_t {
  CFTypeRef peripheral;
  std::string id;
  std::optional<std::string> name;
  int32_t rssi;
  CFTypeRef service_data;
  std::vector<std::string> service_uuids;
};

struct bare_bluetooth_apple_central_connect_t {
  CFTypeRef peripheral;
  std::string id;
};

struct bare_bluetooth_apple_central_disconnect_t {
  std::string id;
  std::optional<std::string> error;
};

struct bare_bluetooth_apple_central_connect_fail_t {
  std::string id;
  std::string error;
};

struct bare_bluetooth_apple_l2cap_data_t {
  void *bytes;
  size_t len;
};

struct bare_bluetooth_apple_l2cap_error_t {
  std::string message;
};

@interface BareBluetoothAppleCentral : NSObject <CBCentralManagerDelegate> {
@public
  js_env_t *env;
  bool destroyed;
  js_ref_t *ctx;
  js_deferred_teardown_t *teardown;
  js_threadsafe_function_t *tsfn_state_change;
  js_threadsafe_function_t *tsfn_discover;
  js_threadsafe_function_t *tsfn_connect;
  js_threadsafe_function_t *tsfn_disconnect;
  js_threadsafe_function_t *tsfn_connect_fail;

  CBCentralManager *manager;
  dispatch_queue_t queue;
  uv_async_t *cleanup_async;
}

@end

@interface BareBluetoothApplePeripheral : NSObject <CBPeripheralDelegate> {
@public
  js_env_t *env;
  bool destroyed;
  js_ref_t *ctx;
  js_deferred_teardown_t *teardown;
  js_threadsafe_function_t *tsfn_services_discover;
  js_threadsafe_function_t *tsfn_characteristics_discover;
  js_threadsafe_function_t *tsfn_read;
  js_threadsafe_function_t *tsfn_write;
  js_threadsafe_function_t *tsfn_notify;
  js_threadsafe_function_t *tsfn_notify_state;
  js_threadsafe_function_t *tsfn_channel_open;
  js_threadsafe_function_t *tsfn_name_change;

  CBPeripheral *peripheral;
  dispatch_queue_t queue;
  uv_async_t *cleanup_async;
}

@end

@implementation BareBluetoothApplePeripheral

- (void)dealloc {
  [peripheral release];
  dispatch_release(queue);
  [super dealloc];
}

- (void)peripheralDidUpdateName:(CBPeripheral *)p {
  auto event = new bare_bluetooth_apple_peripheral_name_change_t;
  if (!event) abort();

  NSString *name = p.name;
  if (name) event->name = name.UTF8String;

  int err = js_call_threadsafe_function(tsfn_name_change, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheral:(CBPeripheral *)p didDiscoverServices:(NSError *)error {
  auto event = new bare_bluetooth_apple_peripheral_services_discover_t;
  if (!event) abort();
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_services_discover, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheral:(CBPeripheral *)p
  didDiscoverCharacteristicsForService:(CBService *)service
                                 error:(NSError *)error {
  auto event = new bare_bluetooth_apple_peripheral_characteristics_discover_t;
  if (!event) abort();

  event->service = CFBridgingRetain(service);
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_characteristics_discover, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheral:(CBPeripheral *)p
  didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
                            error:(NSError *)error {
  if (characteristic.isNotifying) {
    auto event = new bare_bluetooth_apple_peripheral_notify_t;
    if (!event) abort();

    event->characteristic = CFBridgingRetain(characteristic);
    event->uuid = characteristic.UUID.UUIDString.UTF8String;
    if (error) event->error = error.localizedDescription.UTF8String;

    NSData *value = characteristic.value;

    if (!error && value && value.length > 0) {
      event->data_len = value.length;
      event->data = new uint8_t[value.length];
      if (!event->data) abort();
      memcpy(event->data, value.bytes, value.length);
    } else {
      event->data = NULL;
      event->data_len = 0;
    }

    int err = js_call_threadsafe_function(tsfn_notify, event, js_threadsafe_function_nonblocking);
    assert(err == 0);
  } else {
    auto event = new bare_bluetooth_apple_peripheral_read_t;
    if (!event) abort();

    event->characteristic = CFBridgingRetain(characteristic);
    event->uuid = characteristic.UUID.UUIDString.UTF8String;

    if (error) {
      event->data = NULL;
      event->data_len = 0;
      event->error = error.localizedDescription.UTF8String;
    } else {
      NSData *value = characteristic.value;

      if (value && value.length > 0) {
        event->data_len = value.length;
        event->data = new uint8_t[value.length];
        if (!event->data) abort();
        memcpy(event->data, value.bytes, value.length);
      } else {
        event->data = NULL;
        event->data_len = 0;
      }
    }

    int err = js_call_threadsafe_function(tsfn_read, event, js_threadsafe_function_nonblocking);
    assert(err == 0);
  }
}

- (void)peripheral:(CBPeripheral *)p
  didWriteValueForCharacteristic:(CBCharacteristic *)characteristic
                           error:(NSError *)error {
  auto event = new bare_bluetooth_apple_peripheral_write_t;
  if (!event) abort();

  event->characteristic = CFBridgingRetain(characteristic);
  event->uuid = characteristic.UUID.UUIDString.UTF8String;
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_write, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheral:(CBPeripheral *)p
  didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic
                                        error:(NSError *)error {
  auto event = new bare_bluetooth_apple_peripheral_notify_state_t;
  if (!event) abort();

  event->characteristic = CFBridgingRetain(characteristic);
  event->uuid = characteristic.UUID.UUIDString.UTF8String;
  event->is_notifying = characteristic.isNotifying;
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_notify_state, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheral:(CBPeripheral *)p
  didOpenL2CAPChannel:(CBL2CAPChannel *)l2capChannel
                error:(NSError *)error {
  auto event = new bare_bluetooth_apple_peripheral_channel_open_t;
  if (!event) abort();

  event->channel = l2capChannel ? CFBridgingRetain(l2capChannel) : NULL;
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_channel_open, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

@end

static void
bare_bluetooth_apple__on_bridged_release(js_env_t *env, void *data, void *finalize_hint) {
  CFBridgingRelease(data);
}

template <typename T>
static void
bare_bluetooth_apple__release_bridged(js_env_t *, T *data) {
  CFBridgingRelease(data);
}

template <typename T>
static void
bare_bluetooth_apple__on_finalize(js_env_t *, T *ctx) {
  CFBridgingRelease((__bridge CFTypeRef) ctx->handle);
  delete ctx;
}

static void
bare_bluetooth_apple__on_cleanup_close(uv_handle_t *handle) {
  CFBridgingRelease(handle->data);
  delete handle;
}

static js_external_t<CBUUID>
bare_bluetooth_apple_create_cbuuid(
  js_env_t *env,
  js_receiver_t,
  std::string str
) {
  @autoreleasepool {
    int err;
    CBUUID *uuid = [CBUUID UUIDWithString:[NSString stringWithUTF8String:str.c_str()]];

    js_external_t<CBUUID> result;
    err = js_create_external<bare_bluetooth_apple__release_bridged<CBUUID>>(
      env,
      static_cast<CBUUID *>(CFBridgingRetain(uuid)),
      result
    );
    assert(err == 0);

    return result;
  }
}

struct bare_bluetooth_apple_peripheral_t {
  BareBluetoothApplePeripheral *handle;
};

using bare_bluetooth_apple_peripheral__on_services_discover_fn = js_function_t<void, js_receiver_t, js_array_t, std::optional<std::string>>;
using bare_bluetooth_apple_peripheral__on_characteristics_discover_fn = js_function_t<void, js_receiver_t, js_object_t, js_array_t, std::optional<std::string>>;
using bare_bluetooth_apple_peripheral__on_read_fn = js_function_t<void, js_receiver_t, js_object_t, std::string, js_object_t, std::optional<std::string>>;
using bare_bluetooth_apple_peripheral__on_write_fn = js_function_t<void, js_receiver_t, js_object_t, std::string, std::optional<std::string>>;
using bare_bluetooth_apple_peripheral__on_notify_fn = js_function_t<void, js_receiver_t, js_object_t, std::string, js_object_t, std::optional<std::string>>;
using bare_bluetooth_apple_peripheral__on_notify_state_fn = js_function_t<void, js_receiver_t, js_object_t, std::string, bool, std::optional<std::string>>;
using bare_bluetooth_apple_peripheral__on_channel_open_fn = js_function_t<void, js_receiver_t, js_object_t, std::optional<std::string>>;
using bare_bluetooth_apple_peripheral__on_name_change_fn = js_function_t<void, js_receiver_t, std::optional<std::string>>;

static js_value_t *
bare_bluetooth_apple__bridged_array(js_env_t *env, NSArray *items) {
  int err;

  js_value_t *array;
  err = js_create_array_with_length(env, static_cast<size_t>(items.count), &array);
  assert(err == 0);

  for (uint32_t i = 0; i < items.count; i++) {
    js_value_t *external;
    err = js_create_external(env, const_cast<void *>(CFBridgingRetain(items[i])), bare_bluetooth_apple__on_bridged_release, NULL, &external);
    assert(err == 0);

    err = js_set_element(env, array, i, external);
    assert(err == 0);
  }

  return array;
}

static void
bare_bluetooth_apple_peripheral__on_name_change(
  js_env_t *env,
  bare_bluetooth_apple_peripheral__on_name_change_fn function,
  bare_bluetooth_apple_peripheral_t *peripheral,
  bare_bluetooth_apple_peripheral_name_change_t *event
) {
  auto wrapper = peripheral->handle;
  int err;

  if (wrapper->destroyed) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, wrapper->ctx, &receiver);
  assert(err == 0);

  std::optional<std::string> name = std::move(event->name);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), name);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_peripheral__on_services_discover(
  js_env_t *env,
  bare_bluetooth_apple_peripheral__on_services_discover_fn function,
  bare_bluetooth_apple_peripheral_t *peripheral,
  bare_bluetooth_apple_peripheral_services_discover_t *event
) {
  auto wrapper = peripheral->handle;
  int err;

  if (wrapper->destroyed) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, wrapper->ctx, &receiver);
  assert(err == 0);

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_value_t *services = bare_bluetooth_apple__bridged_array(env, error ? nil : wrapper->peripheral.services);

  js_call_function(env, function, js_receiver_t(receiver), js_array_t(services), error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_peripheral__on_characteristics_discover(
  js_env_t *env,
  bare_bluetooth_apple_peripheral__on_characteristics_discover_fn function,
  bare_bluetooth_apple_peripheral_t *peripheral,
  bare_bluetooth_apple_peripheral_characteristics_discover_t *event
) {
  auto wrapper = peripheral->handle;
  int err;

  if (wrapper->destroyed) {
    CFBridgingRelease(event->service);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, wrapper->ctx, &receiver);
  assert(err == 0);

  js_value_t *service;
  err = js_create_external(env, const_cast<void *>(event->service), bare_bluetooth_apple__on_bridged_release, NULL, &service);
  assert(err == 0);

  std::optional<std::string> error = std::move(event->error);

  js_value_t *characteristics = bare_bluetooth_apple__bridged_array(env, error ? nil : ((__bridge CBService *) event->service).characteristics);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(service), js_array_t(characteristics), error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_peripheral__on_read(
  js_env_t *env,
  bare_bluetooth_apple_peripheral__on_read_fn function,
  bare_bluetooth_apple_peripheral_t *peripheral,
  bare_bluetooth_apple_peripheral_read_t *event
) {
  auto wrapper = peripheral->handle;
  int err;

  if (wrapper->destroyed) {
    if (event->data) delete[] reinterpret_cast<uint8_t *>(event->data);
    CFBridgingRelease(event->characteristic);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, wrapper->ctx, &receiver);
  assert(err == 0);

  js_value_t *characteristic;
  err = js_create_external(env, const_cast<void *>(event->characteristic), bare_bluetooth_apple__on_bridged_release, NULL, &characteristic);
  assert(err == 0);

  std::string uuid(event->uuid);

  js_value_t *data;
  if (event->data && event->data_len > 0) {
    void *buf;
    js_value_t *arraybuffer;
    err = js_create_arraybuffer(env, event->data_len, &buf, &arraybuffer);
    assert(err == 0);

    memcpy(buf, event->data, event->data_len);

    err = js_create_typedarray(env, js_uint8array, event->data_len, arraybuffer, 0, &data);
    assert(err == 0);

    delete[] reinterpret_cast<uint8_t *>(event->data);
  } else {
    err = js_get_null(env, &data);
    assert(err == 0);
  }

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(characteristic), uuid, js_object_t(data), error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_peripheral__on_write(
  js_env_t *env,
  bare_bluetooth_apple_peripheral__on_write_fn function,
  bare_bluetooth_apple_peripheral_t *peripheral,
  bare_bluetooth_apple_peripheral_write_t *event
) {
  auto wrapper = peripheral->handle;
  int err;

  if (wrapper->destroyed) {
    CFBridgingRelease(event->characteristic);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, wrapper->ctx, &receiver);
  assert(err == 0);

  js_value_t *characteristic;
  err = js_create_external(env, const_cast<void *>(event->characteristic), bare_bluetooth_apple__on_bridged_release, NULL, &characteristic);
  assert(err == 0);

  std::string uuid(event->uuid);

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(characteristic), uuid, error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_peripheral__on_notify(
  js_env_t *env,
  bare_bluetooth_apple_peripheral__on_notify_fn function,
  bare_bluetooth_apple_peripheral_t *peripheral,
  bare_bluetooth_apple_peripheral_notify_t *event
) {
  auto wrapper = peripheral->handle;
  int err;

  if (wrapper->destroyed) {
    if (event->data) delete[] reinterpret_cast<uint8_t *>(event->data);
    CFBridgingRelease(event->characteristic);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, wrapper->ctx, &receiver);
  assert(err == 0);

  js_value_t *characteristic;
  err = js_create_external(env, const_cast<void *>(event->characteristic), bare_bluetooth_apple__on_bridged_release, NULL, &characteristic);
  assert(err == 0);

  std::string uuid(event->uuid);

  js_value_t *data;
  if (event->data && event->data_len > 0) {
    void *buf;
    js_value_t *arraybuffer;
    err = js_create_arraybuffer(env, event->data_len, &buf, &arraybuffer);
    assert(err == 0);

    memcpy(buf, event->data, event->data_len);

    err = js_create_typedarray(env, js_uint8array, event->data_len, arraybuffer, 0, &data);
    assert(err == 0);

    delete[] reinterpret_cast<uint8_t *>(event->data);
  } else {
    err = js_get_null(env, &data);
    assert(err == 0);
  }

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(characteristic), uuid, js_object_t(data), error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_peripheral__on_notify_state(
  js_env_t *env,
  bare_bluetooth_apple_peripheral__on_notify_state_fn function,
  bare_bluetooth_apple_peripheral_t *peripheral,
  bare_bluetooth_apple_peripheral_notify_state_t *event
) {
  auto wrapper = peripheral->handle;
  int err;

  if (wrapper->destroyed) {
    CFBridgingRelease(event->characteristic);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, wrapper->ctx, &receiver);
  assert(err == 0);

  js_value_t *characteristic;
  err = js_create_external(env, const_cast<void *>(event->characteristic), bare_bluetooth_apple__on_bridged_release, NULL, &characteristic);
  assert(err == 0);

  std::string uuid(event->uuid);
  bool is_notifying = event->is_notifying;

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(characteristic), uuid, is_notifying, error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_peripheral__on_channel_open(
  js_env_t *env,
  bare_bluetooth_apple_peripheral__on_channel_open_fn function,
  bare_bluetooth_apple_peripheral_t *peripheral,
  bare_bluetooth_apple_peripheral_channel_open_t *event
) {
  auto wrapper = peripheral->handle;
  int err;

  if (wrapper->destroyed) {
    if (event->channel) CFBridgingRelease(event->channel);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, wrapper->ctx, &receiver);
  assert(err == 0);

  js_value_t *channel;
  if (event->channel) {
    err = js_create_external(env, const_cast<void *>(event->channel), bare_bluetooth_apple__on_bridged_release, NULL, &channel);
    assert(err == 0);
  } else {
    err = js_get_null(env, &channel);
    assert(err == 0);
  }

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(channel), error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_peripheral__on_cleanup(uv_async_t *async) {
  auto wrapper = static_cast<BareBluetoothApplePeripheral *>(async->data);

  int err;

  err = js_release_threadsafe_function(wrapper->tsfn_services_discover, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(wrapper->tsfn_characteristics_discover, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(wrapper->tsfn_read, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(wrapper->tsfn_write, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(wrapper->tsfn_notify, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(wrapper->tsfn_notify_state, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(wrapper->tsfn_channel_open, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(wrapper->tsfn_name_change, js_threadsafe_function_release);
  assert(err == 0);

  err = js_delete_reference(wrapper->env, wrapper->ctx);
  assert(err == 0);

  err = js_finish_deferred_teardown_callback(wrapper->teardown);
  assert(err == 0);

  uv_close(reinterpret_cast<uv_handle_t *>(async), bare_bluetooth_apple__on_cleanup_close);
}

static void
bare_bluetooth_apple_peripheral__teardown(BareBluetoothApplePeripheral *wrapper) {
  if (wrapper->destroyed) return;

  wrapper->destroyed = true;
  wrapper->peripheral.delegate = nil;

  dispatch_async(wrapper->queue, ^{
    uv_async_send(wrapper->cleanup_async);
  });
}

static void
bare_bluetooth_apple_peripheral__on_teardown(js_deferred_teardown_t *handle, void *data) {
  bare_bluetooth_apple_peripheral__teardown((__bridge BareBluetoothApplePeripheral *) data);
}

static js_value_t *
bare_bluetooth_apple_peripheral__wrap(js_env_t *env, dispatch_queue_t queue, const void *peripheral) {
  auto wrapper = [[BareBluetoothApplePeripheral alloc] init];

  wrapper->env = env;
  wrapper->destroyed = false;
  wrapper->peripheral = (__bridge CBPeripheral *) peripheral;
  wrapper->queue = queue;
  dispatch_retain(wrapper->queue);

  js_value_t *result;
  int err = js_create_external(env, (__bridge void *) wrapper, bare_bluetooth_apple__on_bridged_release, NULL, &result);
  assert(err == 0);

  return result;
}

static void
bare_bluetooth_apple_peripheral_init(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> peripheral_handle,
  js_object_t context,
  bare_bluetooth_apple_peripheral__on_services_discover_fn onServicesDiscover,
  bare_bluetooth_apple_peripheral__on_characteristics_discover_fn onCharacteristicsDiscover,
  bare_bluetooth_apple_peripheral__on_read_fn onRead,
  bare_bluetooth_apple_peripheral__on_write_fn onWrite,
  bare_bluetooth_apple_peripheral__on_notify_fn onNotify,
  bare_bluetooth_apple_peripheral__on_notify_state_fn onNotifyState,
  bare_bluetooth_apple_peripheral__on_channel_open_fn onChannelOpen,
  bare_bluetooth_apple_peripheral__on_name_change_fn onNameChange
) {
  @autoreleasepool {
    int err;

    BareBluetoothApplePeripheral *handle;
    err = js_get_value(env, peripheral_handle, handle);
    assert(err == 0);

    assert(handle->ctx == NULL);

    handle->env = env;

    err = js_create_reference(env, static_cast<js_value_t *>(context), 1, &handle->ctx);
    assert(err == 0);

    err = js_add_deferred_teardown_callback(env, bare_bluetooth_apple_peripheral__on_teardown, (__bridge void *) handle, &handle->teardown);
    assert(err == 0);

    auto *services_discover_ctx = new bare_bluetooth_apple_peripheral_t{(__bridge BareBluetoothApplePeripheral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_peripheral__on_services_discover,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_peripheral_t>,
      bare_bluetooth_apple_peripheral_t,
      bare_bluetooth_apple_peripheral_services_discover_t>(env, onServicesDiscover, 0, 1, services_discover_ctx, handle->tsfn_services_discover);
    assert(err == 0);

    auto *characteristics_discover_ctx = new bare_bluetooth_apple_peripheral_t{(__bridge BareBluetoothApplePeripheral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_peripheral__on_characteristics_discover,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_peripheral_t>,
      bare_bluetooth_apple_peripheral_t,
      bare_bluetooth_apple_peripheral_characteristics_discover_t>(env, onCharacteristicsDiscover, 0, 1, characteristics_discover_ctx, handle->tsfn_characteristics_discover);
    assert(err == 0);

    auto *read_ctx = new bare_bluetooth_apple_peripheral_t{(__bridge BareBluetoothApplePeripheral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_peripheral__on_read,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_peripheral_t>,
      bare_bluetooth_apple_peripheral_t,
      bare_bluetooth_apple_peripheral_read_t>(env, onRead, 0, 1, read_ctx, handle->tsfn_read);
    assert(err == 0);

    auto *write_ctx = new bare_bluetooth_apple_peripheral_t{(__bridge BareBluetoothApplePeripheral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_peripheral__on_write,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_peripheral_t>,
      bare_bluetooth_apple_peripheral_t,
      bare_bluetooth_apple_peripheral_write_t>(env, onWrite, 0, 1, write_ctx, handle->tsfn_write);
    assert(err == 0);

    auto *notify_ctx = new bare_bluetooth_apple_peripheral_t{(__bridge BareBluetoothApplePeripheral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_peripheral__on_notify,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_peripheral_t>,
      bare_bluetooth_apple_peripheral_t,
      bare_bluetooth_apple_peripheral_notify_t>(env, onNotify, 0, 1, notify_ctx, handle->tsfn_notify);
    assert(err == 0);

    auto *notify_state_ctx = new bare_bluetooth_apple_peripheral_t{(__bridge BareBluetoothApplePeripheral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_peripheral__on_notify_state,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_peripheral_t>,
      bare_bluetooth_apple_peripheral_t,
      bare_bluetooth_apple_peripheral_notify_state_t>(env, onNotifyState, 0, 1, notify_state_ctx, handle->tsfn_notify_state);
    assert(err == 0);

    auto *channel_open_ctx = new bare_bluetooth_apple_peripheral_t{(__bridge BareBluetoothApplePeripheral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_peripheral__on_channel_open,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_peripheral_t>,
      bare_bluetooth_apple_peripheral_t,
      bare_bluetooth_apple_peripheral_channel_open_t>(env, onChannelOpen, 0, 1, channel_open_ctx, handle->tsfn_channel_open);
    assert(err == 0);

    auto *name_change_ctx = new bare_bluetooth_apple_peripheral_t{(__bridge BareBluetoothApplePeripheral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_peripheral__on_name_change,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_peripheral_t>,
      bare_bluetooth_apple_peripheral_t,
      bare_bluetooth_apple_peripheral_name_change_t>(env, onNameChange, 0, 1, name_change_ctx, handle->tsfn_name_change);
    assert(err == 0);

    handle->peripheral.delegate = handle;

    uv_loop_t *loop;
    err = js_get_env_loop(env, &loop);
    assert(err == 0);

    handle->cleanup_async = new uv_async_t;
    err = uv_async_init(loop, handle->cleanup_async, bare_bluetooth_apple_peripheral__on_cleanup);
    assert(err == 0);

    handle->cleanup_async->data = const_cast<void *>(CFBridgingRetain(handle));
  }
}

static void
bare_bluetooth_apple_peripheral_destroy(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle
) {
  @autoreleasepool {
    BareBluetoothApplePeripheral *wrapper;
    int err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    assert(wrapper->cleanup_async != NULL);

    bare_bluetooth_apple_peripheral__teardown(wrapper);
  }
}

static std::string
bare_bluetooth_apple_peripheral_id(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle
) {
  @autoreleasepool {
    BareBluetoothApplePeripheral *wrapper;
    int err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    NSString *uuid = wrapper->peripheral.identifier.UUIDString;

    return uuid.UTF8String;
  }
}

static std::optional<std::string>
bare_bluetooth_apple_peripheral_name(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle
) {
  @autoreleasepool {
    BareBluetoothApplePeripheral *wrapper;
    int err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    NSString *name = wrapper->peripheral.name;

    if (name) {
      return name.UTF8String;
    } else {
      return std::nullopt;
    }
  }
}

static void
bare_bluetooth_apple_peripheral_discover_services(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle,
  std::optional<std::vector<js_external_t<CBUUID>>> uuids
) {
  @autoreleasepool {
    int err;
    BareBluetoothApplePeripheral *wrapper;
    err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    NSMutableArray<CBUUID *> *serviceUUIDs = nil;

    if (uuids.has_value()) {
      serviceUUIDs = [NSMutableArray arrayWithCapacity:uuids.value().size()];
      for (auto uuid : *uuids) {
        CBUUID *cbuuid;
        err = js_get_value(env, uuid, cbuuid);
        assert(err == 0);
        [serviceUUIDs addObject:cbuuid];
      }
    }

    if (wrapper->destroyed) return;

    assert(wrapper->peripheral.delegate == wrapper);

    [wrapper->peripheral discoverServices:serviceUUIDs];
  }
}

static void
bare_bluetooth_apple_peripheral_discover_characteristics(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle,
  js_external_t<CBService> service_handle,
  std::optional<std::vector<js_external_t<CBUUID>>> uuids
) {
  @autoreleasepool {
    int err;

    BareBluetoothApplePeripheral *wrapper;
    err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    CBService *service;
    err = js_get_value(env, service_handle, service);
    assert(err == 0);

    NSMutableArray<CBUUID *> *characteristicUUIDs = nil;

    if (uuids.has_value()) {
      characteristicUUIDs = [NSMutableArray arrayWithCapacity:uuids.value().size()];

      for (auto uuid : uuids.value()) {
        CBUUID *cbuuid;
        err = js_get_value(env, uuid, cbuuid);
        assert(err == 0);
        [characteristicUUIDs addObject:cbuuid];
      }
    }

    if (wrapper->destroyed) return;

    assert(wrapper->peripheral.delegate == wrapper);

    [wrapper->peripheral discoverCharacteristics:characteristicUUIDs forService:service];
  }
}

static void
bare_bluetooth_apple_peripheral_read(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle,
  js_external_t<CBCharacteristic> char_handle
) {
  @autoreleasepool {
    int err;

    BareBluetoothApplePeripheral *wrapper;
    err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    CBCharacteristic *characteristic;
    err = js_get_value(env, char_handle, characteristic);
    assert(err == 0);

    if (wrapper->destroyed) return;

    assert(wrapper->peripheral.delegate == wrapper);

    [wrapper->peripheral readValueForCharacteristic:characteristic];
  }
}

static void
bare_bluetooth_apple_peripheral_write(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle,
  js_external_t<CBCharacteristic> char_handle,
  js_arraybuffer_span_t data,
  uint64_t size,
  uint64_t offset,
  bool with_response
) {
  @autoreleasepool {
    int err;

    BareBluetoothApplePeripheral *wrapper;
    err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    CBCharacteristic *characteristic;
    err = js_get_value(env, char_handle, characteristic);
    assert(err == 0);

    if (offset > data.size() || size > data.size() - offset) {
      err = js_throw_range_errorf(env, nullptr, "Write range [%llu, %llu) is out of bounds for buffer of length %zu", offset, offset + size, data.size());
      assert(err == 0);
      return;
    }

    NSData *nsdata = [NSData dataWithBytes:&data[offset] length:size];

    CBCharacteristicWriteType type = with_response
                                       ? CBCharacteristicWriteWithResponse
                                       : CBCharacteristicWriteWithoutResponse;

    if (wrapper->destroyed) return;

    assert(wrapper->peripheral.delegate == wrapper);

    [wrapper->peripheral writeValue:nsdata forCharacteristic:characteristic type:type];
  }
}

static void
bare_bluetooth_apple_peripheral_subscribe(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle,
  js_external_t<CBCharacteristic> char_handle
) {

  @autoreleasepool {
    int err;

    BareBluetoothApplePeripheral *wrapper;
    err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    CBCharacteristic *characteristic;
    err = js_get_value(env, char_handle, characteristic);
    assert(err == 0);

    if (wrapper->destroyed) return;

    assert(wrapper->peripheral.delegate == wrapper);

    [wrapper->peripheral setNotifyValue:YES forCharacteristic:characteristic];
  }
}

static void
bare_bluetooth_apple_peripheral_unsubscribe(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle,
  js_external_t<CBCharacteristic> char_handle
) {
  @autoreleasepool {
    int err;

    BareBluetoothApplePeripheral *wrapper;
    err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    CBCharacteristic *characteristic;
    err = js_get_value(env, char_handle, characteristic);
    assert(err == 0);

    if (wrapper->destroyed) return;

    assert(wrapper->peripheral.delegate == wrapper);

    // TODO: pass notify as parameter ?
    [wrapper->peripheral setNotifyValue:NO forCharacteristic:characteristic];
  }
}

static void
bare_bluetooth_apple_peripheral_open_l2cap_channel(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle,
  uint32_t psm
) {
  @autoreleasepool {
    BareBluetoothApplePeripheral *wrapper;
    int err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    if (wrapper->destroyed) return;

    assert(wrapper->peripheral.delegate == wrapper);

    [wrapper->peripheral openL2CAPChannel:static_cast<CBL2CAPPSM>(psm)];
  }
}

static uint32_t
bare_bluetooth_apple_peripheral_service_count(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothApplePeripheral> handle
) {
  @autoreleasepool {
    BareBluetoothApplePeripheral *wrapper;
    int err = js_get_value(env, handle, wrapper);
    assert(err == 0);

    return wrapper->peripheral.services.count;
  }
}

static std::string
bare_bluetooth_apple_service_key(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBService> handle
) {
  @autoreleasepool {
    CBService *service;
    int err = js_get_value(env, handle, service);
    assert(err == 0);

    char key[32];
    int len = snprintf(key, sizeof(key), "%p", service);
    assert(len > 0);

    return std::string(key, len);
  }
}

static std::string
bare_bluetooth_apple_service_uuid(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBService> handle
) {
  @autoreleasepool {
    CBService *service;
    int err = js_get_value(env, handle, service);
    assert(err == 0);

    return service.UUID.UUIDString.UTF8String;
  }
}

static std::string
bare_bluetooth_apple_characteristic_key(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBCharacteristic> handle
) {
  @autoreleasepool {
    CBCharacteristic *characteristic;
    int err = js_get_value(env, handle, characteristic);
    assert(err == 0);

    char key[32];
    int len = snprintf(key, sizeof(key), "%p", characteristic);
    assert(len > 0);

    return std::string(key, len);
  }
}

static std::string
bare_bluetooth_apple_characteristic_uuid(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBCharacteristic> handle
) {
  @autoreleasepool {
    CBCharacteristic *characteristic;
    int err = js_get_value(env, handle, characteristic);
    assert(err == 0);

    return characteristic.UUID.UUIDString.UTF8String;
  }
}

static int32_t
bare_bluetooth_apple_characteristic_properties(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBCharacteristic> handle
) {
  @autoreleasepool {
    CBCharacteristic *characteristic;
    int err = js_get_value(env, handle, characteristic);
    assert(err == 0);

    return static_cast<int32_t>(characteristic.properties);
  }
}

static uint32_t
bare_bluetooth_apple_service_characteristic_count(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBService> handle
) {
  @autoreleasepool {
    CBService *service;
    int err = js_get_value(env, handle, service);
    assert(err == 0);

    return service.characteristics.count;
  }
}

@interface BareBluetoothAppleServer : NSObject <CBPeripheralManagerDelegate> {
@public
  js_env_t *env;
  bool destroyed;
  js_ref_t *ctx;
  js_deferred_teardown_t *teardown;
  js_threadsafe_function_t *tsfn_state_change;
  js_threadsafe_function_t *tsfn_add_service;
  js_threadsafe_function_t *tsfn_read_request;
  js_threadsafe_function_t *tsfn_write_requests;
  js_threadsafe_function_t *tsfn_subscribe;
  js_threadsafe_function_t *tsfn_unsubscribe;
  js_threadsafe_function_t *tsfn_ready_to_update;
  js_threadsafe_function_t *tsfn_channel_publish;
  js_threadsafe_function_t *tsfn_channel_open;
  js_threadsafe_function_t *tsfn_advertise_error;

  CBPeripheralManager *manager;
  dispatch_queue_t queue;
  uv_async_t *cleanup_async;
}

@end

@implementation BareBluetoothAppleServer

- (void)dealloc {
  [manager release];
  dispatch_release(queue);
  [super dealloc];
}

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
  auto event = new bare_bluetooth_apple_server_state_change_t;
  if (!event) abort();
  event->state = static_cast<int32_t>(peripheral.state);

  int err = js_call_threadsafe_function(tsfn_state_change, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral
                                       error:(NSError *)error {
  if (!error) return;

  auto event = new bare_bluetooth_apple_server_advertise_error_t;
  if (!event) abort();

  event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_advertise_error, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
            didAddService:(CBService *)service
                    error:(NSError *)error {
  auto event = new bare_bluetooth_apple_server_add_service_t;
  if (!event) abort();

  event->service = CFBridgingRetain(service);
  event->uuid = service.UUID.UUIDString.UTF8String;
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_add_service, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
    didReceiveReadRequest:(CBATTRequest *)request {
  auto event = new bare_bluetooth_apple_server_read_request_t;
  if (!event) abort();

  event->request = CFBridgingRetain(request);

  int err = js_call_threadsafe_function(tsfn_read_request, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
  didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests {
  auto event = new bare_bluetooth_apple_server_write_requests_t;
  if (!event) abort();

  uint32_t count = static_cast<uint32_t>(requests.count);
  event->count = count;
  event->requests = new CFTypeRef[count];
  if (!event->requests) abort();

  for (uint32_t i = 0; i < count; i++) {
    event->requests[i] = CFBridgingRetain(requests[i]);
  }

  int err = js_call_threadsafe_function(tsfn_write_requests, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
                       central:(CBCentral *)central
  didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
  auto event = new bare_bluetooth_apple_server_subscribe_t;
  if (!event) abort();

  event->central = CFBridgingRetain(central);
  event->characteristic_uuid = characteristic.UUID.UUIDString.UTF8String;

  int err = js_call_threadsafe_function(tsfn_subscribe, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
                           central:(CBCentral *)central
  didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic {
  auto event = new bare_bluetooth_apple_server_unsubscribe_t;
  if (!event) abort();

  event->central = CFBridgingRetain(central);
  event->characteristic_uuid = characteristic.UUID.UUIDString.UTF8String;

  int err = js_call_threadsafe_function(tsfn_unsubscribe, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral {
  js_call_threadsafe_function(tsfn_ready_to_update, NULL, js_threadsafe_function_nonblocking);
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
   didPublishL2CAPChannel:(CBL2CAPPSM)PSM
                    error:(NSError *)error {
  auto event = new bare_bluetooth_apple_server_channel_publish_t;
  if (!event) abort();

  event->psm = static_cast<uint16_t>(PSM);
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_channel_publish, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
      didOpenL2CAPChannel:(CBL2CAPChannel *)l2capChannel
                    error:(NSError *)error {
  auto event = new bare_bluetooth_apple_server_channel_open_t;
  if (!event) abort();

  event->channel = l2capChannel ? CFBridgingRetain(l2capChannel) : NULL;
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_channel_open, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

@end

struct bare_bluetooth_apple_server_t {
  BareBluetoothAppleServer *handle;
};

using bare_bluetooth_apple_server__on_state_change_fn = js_function_t<void, js_receiver_t, int32_t>;
using bare_bluetooth_apple_server__on_add_service_fn = js_function_t<void, js_receiver_t, js_object_t, std::string, std::optional<std::string>>;
using bare_bluetooth_apple_server__on_read_request_fn = js_function_t<void, js_receiver_t, js_object_t>;
using bare_bluetooth_apple_server__on_write_requests_fn = js_function_t<void, js_receiver_t, js_array_t>;
using bare_bluetooth_apple_server__on_subscribe_fn = js_function_t<void, js_receiver_t, js_object_t, std::string>;
using bare_bluetooth_apple_server__on_unsubscribe_fn = js_function_t<void, js_receiver_t, js_object_t, std::string>;
using bare_bluetooth_apple_server__on_ready_to_update_fn = js_function_t<void, js_receiver_t>;
using bare_bluetooth_apple_server__on_channel_publish_fn = js_function_t<void, js_receiver_t, uint32_t, std::optional<std::string>>;
using bare_bluetooth_apple_server__on_channel_open_fn = js_function_t<void, js_receiver_t, js_object_t, std::optional<std::string>>;
using bare_bluetooth_apple_server__on_advertise_error_fn = js_function_t<void, js_receiver_t, std::string>;

static void
bare_bluetooth_apple_server__on_state_change(
  js_env_t *env,
  bare_bluetooth_apple_server__on_state_change_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_state_change_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  int32_t state = event->state;
  delete event;

  js_call_function(env, function, js_receiver_t(receiver), state);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_add_service(
  js_env_t *env,
  bare_bluetooth_apple_server__on_add_service_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_add_service_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    CFBridgingRelease(event->service);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  js_value_t *service;
  err = js_create_external(env, const_cast<void *>(event->service), bare_bluetooth_apple__on_bridged_release, NULL, &service);
  assert(err == 0);

  std::string uuid(event->uuid);

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(service), uuid, error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_read_request(
  js_env_t *env,
  bare_bluetooth_apple_server__on_read_request_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_read_request_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    CFBridgingRelease(event->request);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  js_value_t *request;
  err = js_create_external(env, const_cast<void *>(event->request), bare_bluetooth_apple__on_bridged_release, NULL, &request);
  assert(err == 0);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(request));

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_write_requests(
  js_env_t *env,
  bare_bluetooth_apple_server__on_write_requests_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_write_requests_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    for (uint32_t i = 0; i < event->count; i++) {
      CFBridgingRelease(event->requests[i]);
    }
    delete[] event->requests;
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  uint32_t count = event->count;

  js_value_t *array;
  err = js_create_array_with_length(env, count, &array);
  assert(err == 0);

  for (uint32_t i = 0; i < count; i++) {
    js_value_t *request_external;
    err = js_create_external(env, const_cast<void *>(event->requests[i]), bare_bluetooth_apple__on_bridged_release, NULL, &request_external);
    assert(err == 0);

    err = js_set_element(env, array, i, request_external);
    assert(err == 0);
  }

  delete[] event->requests;
  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_array_t(array));

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_subscribe(
  js_env_t *env,
  bare_bluetooth_apple_server__on_subscribe_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_subscribe_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    CFBridgingRelease(event->central);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  js_value_t *central;
  err = js_create_external(env, const_cast<void *>(event->central), bare_bluetooth_apple__on_bridged_release, NULL, &central);
  assert(err == 0);

  std::string characteristic_uuid(event->characteristic_uuid);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(central), characteristic_uuid);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_unsubscribe(
  js_env_t *env,
  bare_bluetooth_apple_server__on_unsubscribe_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_unsubscribe_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    CFBridgingRelease(event->central);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  js_value_t *central;
  err = js_create_external(env, const_cast<void *>(event->central), bare_bluetooth_apple__on_bridged_release, NULL, &central);
  assert(err == 0);

  std::string characteristic_uuid(event->characteristic_uuid);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(central), characteristic_uuid);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_ready_to_update(
  js_env_t *env,
  bare_bluetooth_apple_server__on_ready_to_update_fn function,
  bare_bluetooth_apple_server_t *srv,
  void *data
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) return;

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  js_call_function(env, function, js_receiver_t(receiver));

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_channel_publish(
  js_env_t *env,
  bare_bluetooth_apple_server__on_channel_publish_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_channel_publish_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  uint32_t psm = event->psm;

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), psm, error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_advertise_error(
  js_env_t *env,
  bare_bluetooth_apple_server__on_advertise_error_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_advertise_error_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  std::string error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_channel_open(
  js_env_t *env,
  bare_bluetooth_apple_server__on_channel_open_fn function,
  bare_bluetooth_apple_server_t *srv,
  bare_bluetooth_apple_server_channel_open_t *event
) {
  auto server = srv->handle;
  int err;

  if (server->destroyed) {
    if (event->channel) CFBridgingRelease(event->channel);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, server->ctx, &receiver);
  assert(err == 0);

  js_value_t *channel;
  if (event->channel) {
    err = js_create_external(env, const_cast<void *>(event->channel), bare_bluetooth_apple__on_bridged_release, NULL, &channel);
    assert(err == 0);
  } else {
    err = js_get_null(env, &channel);
    assert(err == 0);
  }

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(channel), error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_server__on_cleanup(uv_async_t *async) {
  auto server = static_cast<BareBluetoothAppleServer *>(async->data);

  int err;

  err = js_release_threadsafe_function(server->tsfn_advertise_error, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_channel_open, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_channel_publish, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_ready_to_update, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_unsubscribe, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_subscribe, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_write_requests, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_read_request, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_add_service, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(server->tsfn_state_change, js_threadsafe_function_release);
  assert(err == 0);

  err = js_delete_reference(server->env, server->ctx);
  assert(err == 0);

  err = js_finish_deferred_teardown_callback(server->teardown);
  assert(err == 0);

  uv_close(reinterpret_cast<uv_handle_t *>(async), bare_bluetooth_apple__on_cleanup_close);
}

static void
bare_bluetooth_apple_server__teardown(BareBluetoothAppleServer *server) {
  if (server->destroyed) return;

  server->destroyed = true;
  server->manager.delegate = nil;

  dispatch_async(server->queue, ^{
    uv_async_send(server->cleanup_async);
  });
}

static void
bare_bluetooth_apple_server__on_teardown(js_deferred_teardown_t *handle, void *data) {
  bare_bluetooth_apple_server__teardown((__bridge BareBluetoothAppleServer *) data);
}

static js_external_t<BareBluetoothAppleServer>
bare_bluetooth_apple_server_init(
  js_env_t *env,
  js_receiver_t,
  js_object_t context,
  bare_bluetooth_apple_server__on_state_change_fn on_state_change,
  bare_bluetooth_apple_server__on_add_service_fn on_add_service,
  bare_bluetooth_apple_server__on_read_request_fn on_read_request,
  bare_bluetooth_apple_server__on_write_requests_fn on_write_requests,
  bare_bluetooth_apple_server__on_subscribe_fn on_subscribe,
  bare_bluetooth_apple_server__on_unsubscribe_fn on_unsubscribe,
  bare_bluetooth_apple_server__on_ready_to_update_fn on_ready_to_update,
  bare_bluetooth_apple_server__on_channel_publish_fn on_channel_publish,
  bare_bluetooth_apple_server__on_channel_open_fn on_channel_open,
  bare_bluetooth_apple_server__on_advertise_error_fn on_advertise_error,
  bool show_power_alert
) {
  @autoreleasepool {
    BareBluetoothAppleServer *handle = [[BareBluetoothAppleServer alloc] init];

    handle->env = env;
    handle->destroyed = false;

    int err = js_create_reference(env, static_cast<js_value_t *>(context), 1, &handle->ctx);
    assert(err == 0);

    err = js_add_deferred_teardown_callback(env, bare_bluetooth_apple_server__on_teardown, (__bridge void *) handle, &handle->teardown);
    assert(err == 0);

    auto *state_change_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_state_change,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_state_change_t>(env, on_state_change, 0, 1, state_change_ctx, handle->tsfn_state_change);
    assert(err == 0);

    auto *add_service_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_add_service,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_add_service_t>(env, on_add_service, 0, 1, add_service_ctx, handle->tsfn_add_service);
    assert(err == 0);

    auto *read_request_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_read_request,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_read_request_t>(env, on_read_request, 0, 1, read_request_ctx, handle->tsfn_read_request);
    assert(err == 0);

    auto *write_requests_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_write_requests,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_write_requests_t>(env, on_write_requests, 0, 1, write_requests_ctx, handle->tsfn_write_requests);
    assert(err == 0);

    auto *subscribe_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_subscribe,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_subscribe_t>(env, on_subscribe, 0, 1, subscribe_ctx, handle->tsfn_subscribe);
    assert(err == 0);

    auto *unsubscribe_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_unsubscribe,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_unsubscribe_t>(env, on_unsubscribe, 0, 1, unsubscribe_ctx, handle->tsfn_unsubscribe);
    assert(err == 0);

    auto *ready_to_update_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_ready_to_update,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      void>(env, on_ready_to_update, 0, 1, ready_to_update_ctx, handle->tsfn_ready_to_update);
    assert(err == 0);

    auto *channel_publish_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_channel_publish,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_channel_publish_t>(env, on_channel_publish, 0, 1, channel_publish_ctx, handle->tsfn_channel_publish);
    assert(err == 0);

    auto *channel_open_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_channel_open,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_channel_open_t>(env, on_channel_open, 0, 1, channel_open_ctx, handle->tsfn_channel_open);
    assert(err == 0);

    auto *advertise_error_ctx = new bare_bluetooth_apple_server_t{(__bridge BareBluetoothAppleServer *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_server__on_advertise_error,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_server_t>,
      bare_bluetooth_apple_server_t,
      bare_bluetooth_apple_server_advertise_error_t>(env, on_advertise_error, 0, 1, advertise_error_ctx, handle->tsfn_advertise_error);
    assert(err == 0);

    handle->queue = dispatch_queue_create("bare.bluetooth.server", DISPATCH_QUEUE_SERIAL);
    handle->manager = [[CBPeripheralManager alloc] initWithDelegate:handle queue:handle->queue options:@{CBPeripheralManagerOptionShowPowerAlertKey : @(show_power_alert)}];

    uv_loop_t *loop;
    err = js_get_env_loop(env, &loop);
    assert(err == 0);

    handle->cleanup_async = new uv_async_t;
    err = uv_async_init(loop, handle->cleanup_async, bare_bluetooth_apple_server__on_cleanup);
    assert(err == 0);

    handle->cleanup_async->data = const_cast<void *>(CFBridgingRetain(handle));

    js_external_t<BareBluetoothAppleServer> result;
    err = js_create_external<bare_bluetooth_apple__release_bridged<BareBluetoothAppleServer>>(env, handle, result);
    assert(err == 0);

    return result;
  }
}

static js_external_t<CBMutableCharacteristic>
bare_bluetooth_apple_create_mutable_characteristic(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBUUID> uuid_handle,
  int32_t properties,
  int32_t permissions,
  std::optional<js_uint8array_t> initial_value
) {
  @autoreleasepool {
    CBUUID *uuid;
    int err = js_get_value(env, uuid_handle, uuid);
    assert(err == 0);

    NSData *value_data = nil;

    if (initial_value) {
      uint8_t *data;
      size_t len;
      err = js_get_typedarray_info(env, *initial_value, data, len);
      assert(err == 0);

      value_data = [NSData dataWithBytes:data length:len];
    }

    CBMutableCharacteristic *characteristic = [[CBMutableCharacteristic alloc]
      initWithType:uuid
        properties:static_cast<CBCharacteristicProperties>(properties)
             value:value_data
       permissions:static_cast<CBAttributePermissions>(permissions)];

    js_external_t<CBMutableCharacteristic> result;
    err = js_create_external<bare_bluetooth_apple__release_bridged<CBMutableCharacteristic>>(
      env,
      static_cast<CBMutableCharacteristic *>(CFBridgingRetain(characteristic)),
      result
    );
    assert(err == 0);

    [characteristic release];

    return result;
  }
}

static js_external_t<CBMutableService>
bare_bluetooth_apple_create_mutable_service(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBUUID> uuid_handle,
  bool is_primary
) {
  @autoreleasepool {
    CBUUID *uuid;
    int err = js_get_value(env, uuid_handle, uuid);
    assert(err == 0);

    CBMutableService *service = [[CBMutableService alloc] initWithType:uuid primary:is_primary];

    js_external_t<CBMutableService> result;
    err = js_create_external<bare_bluetooth_apple__release_bridged<CBMutableService>>(
      env,
      static_cast<CBMutableService *>(CFBridgingRetain(service)),
      result
    );

    assert(err == 0);

    [service release];

    return result;
  }
}

static void
bare_bluetooth_apple_service_set_characteristics(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBMutableService> service_handle,
  js_array_t char_array
) {
  @autoreleasepool {
    CBMutableService *service;
    int err = js_get_value(env, service_handle, service);
    assert(err == 0);

    uint32_t len;
    err = js_get_array_length(env, static_cast<js_value_t *>(char_array), &len);
    assert(err == 0);

    NSMutableArray<CBMutableCharacteristic *> *characteristics = [NSMutableArray arrayWithCapacity:len];

    for (uint32_t i = 0; i < len; i++) {
      js_external_t<CBMutableCharacteristic> ext;
      err = js_get_element(env, char_array, i, ext);
      assert(err == 0);

      CBMutableCharacteristic *characteristic;
      err = js_get_value(env, ext, characteristic);
      assert(err == 0);

      [characteristics addObject:characteristic];
    }

    service.characteristics = characteristics;
  }
}

static void
bare_bluetooth_apple_server_add_service(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle,
  js_external_t<CBMutableService> service_handle
) {
  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    CBMutableService *service;
    err = js_get_value(env, service_handle, service);
    assert(err == 0);

    [server->manager addService:service];
  }
}

static void
bare_bluetooth_apple_server_start_advertising(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle,
  std::optional<std::string> name,
  std::optional<js_array_t> service_uuids, // TODO: use std::vector
  std::optional<js_object_t> service_data
) {
  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    NSMutableDictionary *advertisementData = [NSMutableDictionary dictionary];

    if (name) {
      advertisementData[CBAdvertisementDataLocalNameKey] = [NSString stringWithUTF8String:name->c_str()];
    }

    if (service_uuids) {
      uint32_t len;
      err = js_get_array_length(env, static_cast<js_value_t *>(*service_uuids), &len);
      assert(err == 0);

      NSMutableArray<CBUUID *> *uuids = [NSMutableArray arrayWithCapacity:len];

      for (uint32_t i = 0; i < len; i++) {
        js_external_t<CBUUID> ext;
        err = js_get_element(env, *service_uuids, i, ext);
        assert(err == 0);

        CBUUID *uuid;
        err = js_get_value(env, ext, uuid);
        assert(err == 0);

        [uuids addObject:uuid];
      }

      advertisementData[CBAdvertisementDataServiceUUIDsKey] = uuids;
    }

    if (service_data) {
      js_value_t *keys;
      err = js_get_property_names(env, static_cast<js_value_t *>(*service_data), &keys);
      assert(err == 0);

      uint32_t key_count;
      err = js_get_array_length(env, keys, &key_count);
      assert(err == 0);

      NSMutableDictionary<NSString *, NSData *> *sd = [NSMutableDictionary dictionaryWithCapacity:key_count];

      for (uint32_t i = 0; i < key_count; i++) {
        js_value_t *key;
        err = js_get_element(env, keys, i, &key);
        assert(err == 0);

        std::string uuid_str;
        err = js_get_value(env, js_string_t(key), uuid_str);
        assert(err == 0);

        js_typedarray_t<uint8_t> value;
        err = js_get_property(env, *service_data, uuid_str.c_str(), value);
        assert(err == 0);

        uint8_t *buf;
        size_t buf_len;
        err = js_get_typedarray_info(env, value, buf, buf_len);
        assert(err == 0);

        CBUUID *uuid = [CBUUID UUIDWithString:[NSString stringWithUTF8String:uuid_str.c_str()]];
        NSData *data = [NSData dataWithBytes:buf length:buf_len];

        sd[uuid.UUIDString] = data;
      }

      advertisementData[CBAdvertisementDataServiceDataKey] = sd;
    }

    [server->manager startAdvertising:advertisementData];
  }
}

static void
bare_bluetooth_apple_server_stop_advertising(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle
) {
  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    [server->manager stopAdvertising];
  }
}

static std::string
bare_bluetooth_apple_request_characteristic_uuid(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBATTRequest> handle
) {
  @autoreleasepool {
    CBATTRequest *request;
    int err = js_get_value(env, handle, request);
    assert(err == 0);

    return request.characteristic.UUID.UUIDString.UTF8String;
  }
}

static int32_t
bare_bluetooth_apple_request_offset(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBATTRequest> handle
) {
  @autoreleasepool {
    CBATTRequest *request;
    int err = js_get_value(env, handle, request);
    assert(err == 0);

    return static_cast<int32_t>(request.offset);
  }
}

static std::optional<js_uint8array_t>
bare_bluetooth_apple_request_data(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBATTRequest> handle
) {
  @autoreleasepool {
    CBATTRequest *request;
    int err = js_get_value(env, handle, request);
    assert(err == 0);

    NSData *value = request.value;

    if (value && value.length > 0) {
      js_uint8array_t result;
      err = js_create_typedarray(env, static_cast<const uint8_t *>(value.bytes), value.length, result);
      assert(err == 0);

      return result;
    }

    return std::nullopt;
  }
}

static void
bare_bluetooth_apple_server_respond_to_request(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle,
  js_external_t<CBATTRequest> request_handle,
  int32_t result_code,
  std::optional<js_uint8array_t> data
) {
  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    CBATTRequest *request;
    err = js_get_value(env, request_handle, request);
    assert(err == 0);

    if (data) {
      uint8_t *buf;
      size_t len;
      err = js_get_typedarray_info(env, *data, buf, len);
      assert(err == 0);

      request.value = [NSData dataWithBytes:buf length:len];
    }

    [server->manager respondToRequest:request withResult:static_cast<CBATTError>(result_code)];
  }
}

static bool
bare_bluetooth_apple_server_update_value(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle,
  js_external_t<CBMutableCharacteristic> char_handle,
  js_uint8array_t data
) {
  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    CBMutableCharacteristic *characteristic;
    err = js_get_value(env, char_handle, characteristic);
    assert(err == 0);

    uint8_t *buf;
    size_t len;
    err = js_get_typedarray_info(env, data, buf, len);
    assert(err == 0);

    NSData *nsdata = [NSData dataWithBytes:buf length:len];

    return [server->manager updateValue:nsdata forCharacteristic:characteristic onSubscribedCentrals:nil];
  }
}

static void
bare_bluetooth_apple_server_publish_channel(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle,
  bool encrypted
) {
  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    [server->manager publishL2CAPChannelWithEncryption:encrypted];
  }
}

static void
bare_bluetooth_apple_server_unpublish_channel(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle,
  uint32_t psm
) {
  assert(psm <= UINT16_MAX);

  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    [server->manager unpublishL2CAPChannel:static_cast<CBL2CAPPSM>(psm)];
  }
}

static void
bare_bluetooth_apple_server_remove_all_services(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle
) {
  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    [server->manager removeAllServices];
  }
}

static void
bare_bluetooth_apple_server_destroy(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleServer> handle
) {
  @autoreleasepool {
    BareBluetoothAppleServer *server;
    int err = js_get_value(env, handle, server);
    assert(err == 0);

    bare_bluetooth_apple_server__teardown(server);
  }
}

@implementation BareBluetoothAppleCentral

- (void)dealloc {
  [manager release];
  dispatch_release(queue);
  [super dealloc];
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
  auto event = new bare_bluetooth_apple_central_state_change_t;
  if (!event) abort();
  event->state = static_cast<int32_t>(central.state);

  int err = js_call_threadsafe_function(tsfn_state_change, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)centralManager:(CBCentralManager *)central
  didDiscoverPeripheral:(CBPeripheral *)peripheral
      advertisementData:(NSDictionary<NSString *, id> *)advertisementData
                   RSSI:(NSNumber *)RSSI {
  auto event = new bare_bluetooth_apple_central_discover_t;
  if (!event) abort();

  event->peripheral = CFBridgingRetain(peripheral);

  NSString *idString = peripheral.identifier.UUIDString;
  event->id = idString.UTF8String;

  NSString *peripheralName = peripheral.name;
  if (peripheralName) event->name = peripheralName.UTF8String;

  event->rssi = RSSI.intValue;

  NSDictionary *sd = advertisementData[CBAdvertisementDataServiceDataKey];
  event->service_data = (sd && sd.count > 0) ? CFBridgingRetain(sd) : NULL;

  NSArray<CBUUID *> *su = advertisementData[CBAdvertisementDataServiceUUIDsKey];
  for (CBUUID *uuid in su) {
    event->service_uuids.push_back(uuid.UUIDString.UTF8String);
  }

  int err = js_call_threadsafe_function(tsfn_discover, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)centralManager:(CBCentralManager *)central
  didConnectPeripheral:(CBPeripheral *)peripheral {
  auto event = new bare_bluetooth_apple_central_connect_t;
  if (!event) abort();

  event->peripheral = CFBridgingRetain(peripheral);
  event->id = peripheral.identifier.UUIDString.UTF8String;

  int err = js_call_threadsafe_function(tsfn_connect, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)centralManager:(CBCentralManager *)central
  didDisconnectPeripheral:(CBPeripheral *)peripheral
                    error:(NSError *)error {
  auto event = new bare_bluetooth_apple_central_disconnect_t;
  if (!event) abort();

  event->id = peripheral.identifier.UUIDString.UTF8String;
  if (error) event->error = error.localizedDescription.UTF8String;

  int err = js_call_threadsafe_function(tsfn_disconnect, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

- (void)centralManager:(CBCentralManager *)central
  didFailToConnectPeripheral:(CBPeripheral *)peripheral
                       error:(NSError *)error {
  auto event = new bare_bluetooth_apple_central_connect_fail_t;
  if (!event) abort();

  event->id = peripheral.identifier.UUIDString.UTF8String;
  event->error = error ? error.localizedDescription.UTF8String : "Unknown connection failure";

  int err = js_call_threadsafe_function(tsfn_connect_fail, event, js_threadsafe_function_nonblocking);
  assert(err == 0);
}

@end

struct bare_bluetooth_apple_central_t {
  BareBluetoothAppleCentral *handle;
};

using bare_bluetooth_apple_central__on_state_change_fn = js_function_t<void, js_receiver_t, int32_t>;
using bare_bluetooth_apple_central__on_discover_fn = js_function_t<void, js_receiver_t, js_object_t, std::string, std::optional<std::string>, int32_t, js_object_t, std::vector<std::string>>;
using bare_bluetooth_apple_central__on_connect_fn = js_function_t<void, js_receiver_t, js_object_t, std::string>;
using bare_bluetooth_apple_central__on_disconnect_fn = js_function_t<void, js_receiver_t, std::string, std::optional<std::string>>;
using bare_bluetooth_apple_central__on_connect_fail_fn = js_function_t<void, js_receiver_t, std::string, std::string>;

static void
bare_bluetooth_apple_central__on_state_change(
  js_env_t *env,
  bare_bluetooth_apple_central__on_state_change_fn function,
  bare_bluetooth_apple_central_t *cen,
  bare_bluetooth_apple_central_state_change_t *event
) {
  auto central = cen->handle;
  int err;

  if (central->destroyed) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, central->ctx, &receiver);
  assert(err == 0);

  int32_t state = event->state;
  delete event;

  js_call_function(env, function, js_receiver_t(receiver), state);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_central__on_discover(
  js_env_t *env,
  bare_bluetooth_apple_central__on_discover_fn function,
  bare_bluetooth_apple_central_t *cen,
  bare_bluetooth_apple_central_discover_t *event
) {
  auto central = cen->handle;
  int err;

  if (central->destroyed || !central->manager.isScanning) {
    CFBridgingRelease(event->peripheral);
    if (event->service_data) CFRelease(event->service_data);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, central->ctx, &receiver);
  assert(err == 0);

  js_value_t *peripheral_ext = bare_bluetooth_apple_peripheral__wrap(env, central->queue, event->peripheral);

  std::string id(event->id);

  std::optional<std::string> name = std::move(event->name);

  int32_t rssi = event->rssi;

  std::vector<std::string> service_uuids = std::move(event->service_uuids);

  js_value_t *service_data;
  if (event->service_data) {
    NSDictionary<CBUUID *, NSData *> *sd = (__bridge NSDictionary *) event->service_data;

    js_object_t obj;
    err = js_create_object(env, obj);
    assert(err == 0);

    for (CBUUID *key in sd) {
      NSData *value = sd[key];
      NSString *uuid_str = key.UUIDString;

      js_typedarray_t<uint8_t> u8;
      err = js_create_typedarray<uint8_t>(env, static_cast<const uint8_t *>(value.bytes), value.length, u8);
      assert(err == 0);

      err = js_set_property(env, obj, uuid_str.UTF8String, u8);
      assert(err == 0);
    }
    CFRelease(event->service_data);

    service_data = static_cast<js_value_t *>(obj);
  } else {
    err = js_get_null(env, &service_data);
    assert(err == 0);
  }

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(peripheral_ext), id, name, rssi, js_object_t(service_data), service_uuids);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_central__on_connect(
  js_env_t *env,
  bare_bluetooth_apple_central__on_connect_fn function,
  bare_bluetooth_apple_central_t *cen,
  bare_bluetooth_apple_central_connect_t *event
) {
  auto central = cen->handle;
  int err;

  if (central->destroyed) {
    CFBridgingRelease(event->peripheral);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, central->ctx, &receiver);
  assert(err == 0);

  js_value_t *peripheral_ext = bare_bluetooth_apple_peripheral__wrap(env, central->queue, event->peripheral);

  std::string id(event->id);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_object_t(peripheral_ext), id);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_central__on_disconnect(
  js_env_t *env,
  bare_bluetooth_apple_central__on_disconnect_fn function,
  bare_bluetooth_apple_central_t *cen,
  bare_bluetooth_apple_central_disconnect_t *event
) {
  auto central = cen->handle;
  int err;

  if (central->destroyed) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, central->ctx, &receiver);
  assert(err == 0);

  std::string id(event->id);

  std::optional<std::string> error = std::move(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), id, error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_central__on_connect_fail(
  js_env_t *env,
  bare_bluetooth_apple_central__on_connect_fail_fn function,
  bare_bluetooth_apple_central_t *cen,
  bare_bluetooth_apple_central_connect_fail_t *event
) {
  auto central = cen->handle;
  int err;

  if (central->destroyed) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, central->ctx, &receiver);
  assert(err == 0);

  std::string id(event->id);
  std::string error(event->error);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), id, error);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_central__on_cleanup(uv_async_t *async) {
  auto central = static_cast<BareBluetoothAppleCentral *>(async->data);

  int err;

  err = js_release_threadsafe_function(central->tsfn_connect_fail, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(central->tsfn_disconnect, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(central->tsfn_connect, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(central->tsfn_discover, js_threadsafe_function_release);
  assert(err == 0);

  err = js_release_threadsafe_function(central->tsfn_state_change, js_threadsafe_function_release);
  assert(err == 0);

  err = js_delete_reference(central->env, central->ctx);
  assert(err == 0);

  err = js_finish_deferred_teardown_callback(central->teardown);
  assert(err == 0);

  uv_close(reinterpret_cast<uv_handle_t *>(async), bare_bluetooth_apple__on_cleanup_close);
}

static void
bare_bluetooth_apple_central__teardown(BareBluetoothAppleCentral *central) {
  if (central->destroyed) return;

  central->destroyed = true;
  central->manager.delegate = nil;

  dispatch_async(central->queue, ^{
    uv_async_send(central->cleanup_async);
  });
}

static void
bare_bluetooth_apple_central__on_teardown(js_deferred_teardown_t *handle, void *data) {
  bare_bluetooth_apple_central__teardown((__bridge BareBluetoothAppleCentral *) data);
}

static js_external_t<BareBluetoothAppleCentral>
bare_bluetooth_apple_central_init(
  js_env_t *env,
  js_receiver_t,
  js_object_t context,
  bare_bluetooth_apple_central__on_state_change_fn on_state_change,
  bare_bluetooth_apple_central__on_discover_fn on_discover,
  bare_bluetooth_apple_central__on_connect_fn on_connect,
  bare_bluetooth_apple_central__on_disconnect_fn on_disconnect,
  bare_bluetooth_apple_central__on_connect_fail_fn on_connect_fail,
  bool show_power_alert
) {
  @autoreleasepool {
    BareBluetoothAppleCentral *handle = [[BareBluetoothAppleCentral alloc] init];

    handle->env = env;
    handle->destroyed = false;

    int err = js_create_reference(env, static_cast<js_value_t *>(context), 1, &handle->ctx);
    assert(err == 0);

    err = js_add_deferred_teardown_callback(env, bare_bluetooth_apple_central__on_teardown, (__bridge void *) handle, &handle->teardown);
    assert(err == 0);

    auto *state_ctx = new bare_bluetooth_apple_central_t{(__bridge BareBluetoothAppleCentral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_central__on_state_change,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_central_t>,
      bare_bluetooth_apple_central_t,
      bare_bluetooth_apple_central_state_change_t>(env, on_state_change, 0, 1, state_ctx, handle->tsfn_state_change);
    assert(err == 0);

    auto *discover_ctx = new bare_bluetooth_apple_central_t{(__bridge BareBluetoothAppleCentral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_central__on_discover,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_central_t>,
      bare_bluetooth_apple_central_t,
      bare_bluetooth_apple_central_discover_t>(env, on_discover, 0, 1, discover_ctx, handle->tsfn_discover);
    assert(err == 0);

    auto *connect_ctx = new bare_bluetooth_apple_central_t{(__bridge BareBluetoothAppleCentral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_central__on_connect,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_central_t>,
      bare_bluetooth_apple_central_t,
      bare_bluetooth_apple_central_connect_t>(env, on_connect, 0, 1, connect_ctx, handle->tsfn_connect);
    assert(err == 0);

    auto *disconnect_ctx = new bare_bluetooth_apple_central_t{(__bridge BareBluetoothAppleCentral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_central__on_disconnect,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_central_t>,
      bare_bluetooth_apple_central_t,
      bare_bluetooth_apple_central_disconnect_t>(env, on_disconnect, 0, 1, disconnect_ctx, handle->tsfn_disconnect);
    assert(err == 0);

    auto *connect_fail_ctx = new bare_bluetooth_apple_central_t{(__bridge BareBluetoothAppleCentral *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_central__on_connect_fail,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_central_t>,
      bare_bluetooth_apple_central_t,
      bare_bluetooth_apple_central_connect_fail_t>(env, on_connect_fail, 0, 1, connect_fail_ctx, handle->tsfn_connect_fail);
    assert(err == 0);

    handle->queue = dispatch_queue_create("bare.bluetooth.central", DISPATCH_QUEUE_SERIAL);
    handle->manager = [[CBCentralManager alloc] initWithDelegate:handle queue:handle->queue options:@{CBCentralManagerOptionShowPowerAlertKey : @(show_power_alert)}];

    uv_loop_t *loop;
    err = js_get_env_loop(env, &loop);
    assert(err == 0);

    handle->cleanup_async = new uv_async_t;
    err = uv_async_init(loop, handle->cleanup_async, bare_bluetooth_apple_central__on_cleanup);
    assert(err == 0);

    handle->cleanup_async->data = const_cast<void *>(CFBridgingRetain(handle));

    js_external_t<BareBluetoothAppleCentral> result;
    err = js_create_external<bare_bluetooth_apple__release_bridged<BareBluetoothAppleCentral>>(env, handle, result);
    assert(err == 0);

    return result;
  }
}

static void
bare_bluetooth_apple_central_start_scan(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleCentral> handle,
  std::optional<js_array_t> uuids_array,
  std::optional<bool> allow_duplicates
) {
  @autoreleasepool {
    BareBluetoothAppleCentral *central;
    int err = js_get_value(env, handle, central);
    assert(err == 0);

    NSArray<CBUUID *> *serviceUUIDs = nil;

    if (uuids_array) {
      uint32_t len;
      err = js_get_array_length(env, static_cast<js_value_t *>(*uuids_array), &len);
      assert(err == 0);

      NSMutableArray<CBUUID *> *uuids = [NSMutableArray arrayWithCapacity:len];

      for (uint32_t i = 0; i < len; i++) {
        js_external_t<CBUUID> ext;
        err = js_get_element(env, *uuids_array, i, ext);
        assert(err == 0);

        CBUUID *uuid;
        err = js_get_value(env, ext, uuid);
        assert(err == 0);

        [uuids addObject:uuid];
      }

      serviceUUIDs = uuids;
    }

    NSDictionary *options = @{
      CBCentralManagerScanOptionAllowDuplicatesKey : @(allow_duplicates.value_or(false))
    };

    [central->manager scanForPeripheralsWithServices:serviceUUIDs options:options];
  }
}

static void
bare_bluetooth_apple_central_stop_scan(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleCentral> handle
) {
  @autoreleasepool {
    BareBluetoothAppleCentral *central;
    int err = js_get_value(env, handle, central);
    assert(err == 0);

    [central->manager stopScan];
  }
}

static void
bare_bluetooth_apple_central__require_powered_on(js_env_t *env, BareBluetoothAppleCentral *central) {
  if (central->manager.state == CBManagerStatePoweredOn) return;

  int err = js_throw_error(env, nullptr, "Bluetooth is not powered on");
  assert(err == 0);

  throw js_pending_exception;
}

static std::vector<js_object_t>
bare_bluetooth_apple_central__peripherals(
  js_env_t *env,
  BareBluetoothAppleCentral *central,
  NSArray<CBPeripheral *> *peripherals
) {
  int err;

  std::vector<js_object_t> result;
  result.reserve(peripherals.count);

  for (CBPeripheral *peripheral in peripherals) {
    js_object_t entry;
    err = js_create_object(env, entry);
    assert(err == 0);

    js_value_t *handle = bare_bluetooth_apple_peripheral__wrap(env, central->queue, CFBridgingRetain(peripheral));

    err = js_set_property(env, entry, "handle", js_object_t(handle));
    assert(err == 0);

    err = js_set_property(env, entry, "id", std::string(peripheral.identifier.UUIDString.UTF8String));
    assert(err == 0);

    if (peripheral.name) {
      err = js_set_property(env, entry, "name", std::string(peripheral.name.UTF8String));
      assert(err == 0);
    } else {
      js_value_t *null;
      err = js_get_null(env, &null);
      assert(err == 0);

      err = js_set_named_property(env, static_cast<js_value_t *>(entry), "name", null);
      assert(err == 0);
    }

    err = js_set_property(env, entry, "state", static_cast<int32_t>(peripheral.state));
    assert(err == 0);

    result.push_back(entry);
  }

  return result;
}

static std::vector<js_object_t>
bare_bluetooth_apple_central_retrieve_peripherals(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleCentral> handle,
  std::vector<std::string> ids
) {
  @autoreleasepool {
    BareBluetoothAppleCentral *central;
    int err = js_get_value(env, handle, central);
    assert(err == 0);

    bare_bluetooth_apple_central__require_powered_on(env, central);

    NSMutableArray<NSUUID *> *identifiers = [NSMutableArray arrayWithCapacity:ids.size()];

    for (const auto &id : ids) {
      NSUUID *uuid = [[[NSUUID alloc] initWithUUIDString:[NSString stringWithUTF8String:id.c_str()]] autorelease];

      if (!uuid) {
        err = js_throw_errorf(env, nullptr, "Invalid peripheral id '%s'", id.c_str());
        assert(err == 0);

        throw js_pending_exception;
      }

      [identifiers addObject:uuid];
    }

    return bare_bluetooth_apple_central__peripherals(env, central, [central->manager retrievePeripheralsWithIdentifiers:identifiers]);
  }
}

static std::vector<js_object_t>
bare_bluetooth_apple_central_retrieve_connected_peripherals(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleCentral> handle,
  std::vector<js_external_t<CBUUID>> service_uuids
) {
  @autoreleasepool {
    int err;

    BareBluetoothAppleCentral *central;
    err = js_get_value(env, handle, central);
    assert(err == 0);

    bare_bluetooth_apple_central__require_powered_on(env, central);

    NSMutableArray<CBUUID *> *uuids = [NSMutableArray arrayWithCapacity:service_uuids.size()];

    for (auto &ext : service_uuids) {
      CBUUID *uuid;
      err = js_get_value(env, ext, uuid);
      assert(err == 0);

      [uuids addObject:uuid];
    }

    return bare_bluetooth_apple_central__peripherals(env, central, [central->manager retrieveConnectedPeripheralsWithServices:uuids]);
  }
}

static void
bare_bluetooth_apple_central_connect(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleCentral> handle,
  js_external_t<BareBluetoothApplePeripheral> peripheral_handle
) {
  @autoreleasepool {
    BareBluetoothAppleCentral *central;
    int err = js_get_value(env, handle, central);
    assert(err == 0);

    BareBluetoothApplePeripheral *wrapper;
    err = js_get_value(env, peripheral_handle, wrapper);
    assert(err == 0);

    [central->manager connectPeripheral:wrapper->peripheral options:nil];
  }
}

static void
bare_bluetooth_apple_central_disconnect(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleCentral> handle,
  js_external_t<BareBluetoothApplePeripheral> peripheral_handle
) {
  @autoreleasepool {
    BareBluetoothAppleCentral *central;
    int err = js_get_value(env, handle, central);
    assert(err == 0);

    BareBluetoothApplePeripheral *wrapper;
    err = js_get_value(env, peripheral_handle, wrapper);
    assert(err == 0);

    [central->manager cancelPeripheralConnection:wrapper->peripheral];
  }
}

static void
bare_bluetooth_apple_central_destroy(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleCentral> handle
) {
  @autoreleasepool {
    BareBluetoothAppleCentral *central;
    int err = js_get_value(env, handle, central);
    assert(err == 0);

    bare_bluetooth_apple_central__teardown(central);
  }
}

@interface BareBluetoothAppleL2CAPChannel : NSObject <NSStreamDelegate> {
@public
  js_env_t *env;
  js_ref_t *ctx;
  js_threadsafe_function_t *tsfn_data;
  js_threadsafe_function_t *tsfn_drain;
  js_threadsafe_function_t *tsfn_end;
  js_threadsafe_function_t *tsfn_error;
  js_threadsafe_function_t *tsfn_close;
  js_threadsafe_function_t *tsfn_open;

  js_deferred_teardown_t *teardown;

  CBL2CAPChannel *channel;
  NSInputStream *inputStream;
  NSOutputStream *outputStream;

  NSThread *streamThread;
  std::atomic<bool> opened;
  std::atomic<bool> closing;
  std::atomic<bool> closed;
  std::atomic<bool> destroyed;
  std::atomic<bool> finalized;
  NSMutableArray *writeQueue;
}

- (void)open;
- (void)destroy;
- (void)enqueueWrite:(NSData *)data;
- (void)processWriteQueue;

@end

@implementation BareBluetoothAppleL2CAPChannel

- (void)dealloc {
  [channel release];
  [streamThread release];
  [writeQueue release];
  [super dealloc];
}

- (void)open {
  if (opened.load()) return;
  opened.store(true);

  inputStream = channel.inputStream;
  outputStream = channel.outputStream;
  writeQueue = [[NSMutableArray alloc] init];

  streamThread = [[NSThread alloc] initWithTarget:self selector:@selector(streamThreadEntry) object:nil];
  streamThread.name = @"bare.bluetooth.l2cap";
  [streamThread start];
}

- (void)streamThreadEntry {
  @autoreleasepool {
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

    inputStream.delegate = self;
    outputStream.delegate = self;

    [inputStream scheduleInRunLoop:runLoop forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:runLoop forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];

    while (!destroyed.load() && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
    }
  }
}

- (void)destroy {
  if (destroyed.load() || closing.load()) return;
  closing.store(true);
  destroyed.store(true);

  if (!opened.load()) {
    js_call_threadsafe_function(tsfn_close, NULL, js_threadsafe_function_nonblocking);
    return;
  }

  [self performSelector:@selector(closeOnStreamThread) onThread:streamThread withObject:nil waitUntilDone:NO];
}

- (void)enqueueWrite:(NSData *)data {
  [writeQueue addObject:data];
  [self processWriteQueue];
}

- (void)processWriteQueue {
  while (writeQueue.count > 0 && outputStream.hasSpaceAvailable) {
    NSData *data = writeQueue[0];
    const uint8_t *bytes = static_cast<const uint8_t *>(data.bytes);
    NSInteger written = [outputStream write:bytes maxLength:data.length];

    if (written > 0) {
      if (static_cast<NSUInteger>(written) < data.length) {
        writeQueue[0] = [data subdataWithRange:NSMakeRange(written, data.length - written)];
      } else {
        [writeQueue removeObjectAtIndex:0];
      }
    } else {
      break;
    }
  }
}

- (void)closeOnStreamThread {
  if (closed.load()) return;

  [inputStream close];
  [outputStream close];

  NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
  [inputStream removeFromRunLoop:runLoop forMode:NSDefaultRunLoopMode];
  [outputStream removeFromRunLoop:runLoop forMode:NSDefaultRunLoopMode];

  inputStream.delegate = nil;
  outputStream.delegate = nil;

  [writeQueue removeAllObjects];

  closed.store(true);

  js_call_threadsafe_function(tsfn_close, NULL, js_threadsafe_function_nonblocking);
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
  if (closing.load()) return;

  switch (eventCode) {
  case NSStreamEventHasBytesAvailable: {
    if (stream != inputStream) break;

    std::vector<uint8_t> buffer;
    buffer.reserve(4096);
    size_t total = 0;

    do {
      if (total == buffer.size()) {
        buffer.resize(buffer.size() == 0 ? 4096 : buffer.size() * 2);
      }

      NSInteger bytesRead = [inputStream read:buffer.data() + total maxLength:buffer.size() - total];

      if (bytesRead <= 0) break;

      total += static_cast<size_t>(bytesRead);
    } while (inputStream.hasBytesAvailable);

    if (total > 0) {
      auto event = new bare_bluetooth_apple_l2cap_data_t;
      if (!event) abort();
      event->len = total;
      event->bytes = new uint8_t[total];
      std::memcpy(event->bytes, buffer.data(), total);

      int err = js_call_threadsafe_function(tsfn_data, event, js_threadsafe_function_nonblocking);
      assert(err == 0);
    }

    break;
  }

  case NSStreamEventHasSpaceAvailable: {
    if (stream != outputStream) break;

    [self processWriteQueue];

    js_call_threadsafe_function(tsfn_drain, NULL, js_threadsafe_function_nonblocking);

    break;
  }

  case NSStreamEventEndEncountered: {
    js_call_threadsafe_function(tsfn_end, NULL, js_threadsafe_function_nonblocking);

    break;
  }

  case NSStreamEventErrorOccurred: {
    NSError *error = stream.streamError;

    auto event = new bare_bluetooth_apple_l2cap_error_t;
    if (!event) abort();
    event->message = error ? error.localizedDescription.UTF8String : "Unknown stream error";

    int err = js_call_threadsafe_function(tsfn_error, event, js_threadsafe_function_nonblocking);
    assert(err == 0);

    break;
  }

  case NSStreamEventOpenCompleted: {
    if (stream != outputStream) break;

    js_call_threadsafe_function(tsfn_open, NULL, js_threadsafe_function_nonblocking);

    break;
  }

  default:
    break;
  }
}

@end

struct bare_bluetooth_apple_l2cap_t {
  BareBluetoothAppleL2CAPChannel *handle;
};

using bare_bluetooth_apple_l2cap__on_data_fn = js_function_t<void, js_receiver_t, js_uint8array_t>;
using bare_bluetooth_apple_l2cap__on_drain_fn = js_function_t<void, js_receiver_t>;
using bare_bluetooth_apple_l2cap__on_end_fn = js_function_t<void, js_receiver_t>;
using bare_bluetooth_apple_l2cap__on_error_fn = js_function_t<void, js_receiver_t, std::string>;
using bare_bluetooth_apple_l2cap__on_close_fn = js_function_t<void, js_receiver_t>;
using bare_bluetooth_apple_l2cap__on_open_fn = js_function_t<void, js_receiver_t>;

static void
bare_bluetooth_apple_l2cap__on_data(
  js_env_t *env,
  bare_bluetooth_apple_l2cap__on_data_fn function,
  bare_bluetooth_apple_l2cap_t *l2cap,
  bare_bluetooth_apple_l2cap_data_t *event
) {
  auto channel = l2cap->handle;
  int err;

  if (channel->finalized.load()) {
    delete[] reinterpret_cast<uint8_t *>(event->bytes);
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, channel->ctx, &receiver);
  assert(err == 0);

  void *buf;
  js_value_t *arraybuffer;
  err = js_create_arraybuffer(env, event->len, &buf, &arraybuffer);
  assert(err == 0);

  memcpy(buf, event->bytes, event->len);

  js_value_t *typedarray;
  err = js_create_typedarray(env, js_uint8array, event->len, arraybuffer, 0, &typedarray);
  assert(err == 0);

  delete[] reinterpret_cast<uint8_t *>(event->bytes);
  delete event;

  js_call_function(env, function, js_receiver_t(receiver), js_uint8array_t(typedarray));

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_l2cap__on_drain(
  js_env_t *env,
  bare_bluetooth_apple_l2cap__on_drain_fn function,
  bare_bluetooth_apple_l2cap_t *l2cap,
  void *data
) {
  auto channel = l2cap->handle;
  int err;

  if (channel->finalized.load()) return;

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, channel->ctx, &receiver);
  assert(err == 0);

  js_call_function(env, function, js_receiver_t(receiver));

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_l2cap__on_end(
  js_env_t *env,
  bare_bluetooth_apple_l2cap__on_end_fn function,
  bare_bluetooth_apple_l2cap_t *l2cap,
  void *data
) {
  auto channel = l2cap->handle;
  int err;

  if (channel->finalized.load()) return;

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, channel->ctx, &receiver);
  assert(err == 0);

  js_call_function(env, function, js_receiver_t(receiver));

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_l2cap__on_error(
  js_env_t *env,
  bare_bluetooth_apple_l2cap__on_error_fn function,
  bare_bluetooth_apple_l2cap_t *l2cap,
  bare_bluetooth_apple_l2cap_error_t *event
) {
  auto channel = l2cap->handle;
  int err;

  if (channel->finalized.load()) {
    delete event;
    return;
  }

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, channel->ctx, &receiver);
  assert(err == 0);

  std::string message(event->message);

  delete event;

  js_call_function(env, function, js_receiver_t(receiver), message);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_l2cap__teardown(BareBluetoothAppleL2CAPChannel *l2cap, js_threadsafe_function_release_mode_t mode) {
  if (l2cap->finalized.exchange(true)) return;

  int err;

  err = js_release_threadsafe_function(l2cap->tsfn_open, mode);
  assert(err == 0);

  err = js_release_threadsafe_function(l2cap->tsfn_close, mode);
  assert(err == 0);

  err = js_release_threadsafe_function(l2cap->tsfn_error, mode);
  assert(err == 0);

  err = js_release_threadsafe_function(l2cap->tsfn_end, mode);
  assert(err == 0);

  err = js_release_threadsafe_function(l2cap->tsfn_drain, mode);
  assert(err == 0);

  err = js_release_threadsafe_function(l2cap->tsfn_data, mode);
  assert(err == 0);

  err = js_delete_reference(l2cap->env, l2cap->ctx);
  assert(err == 0);

  err = js_finish_deferred_teardown_callback(l2cap->teardown);
  assert(err == 0);
}

static void
bare_bluetooth_apple_l2cap__on_close(
  js_env_t *env,
  bare_bluetooth_apple_l2cap__on_close_fn function,
  bare_bluetooth_apple_l2cap_t *l2cap,
  void *data
) {
  auto channel = l2cap->handle;
  int err;

  if (channel->finalized.load()) return;

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, channel->ctx, &receiver);
  assert(err == 0);

  js_call_function(env, function, js_receiver_t(receiver));

  bare_bluetooth_apple_l2cap__teardown(channel, js_threadsafe_function_release);

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_l2cap__on_open(
  js_env_t *env,
  bare_bluetooth_apple_l2cap__on_open_fn function,
  bare_bluetooth_apple_l2cap_t *l2cap,
  void *data
) {
  auto channel = l2cap->handle;
  int err;

  if (channel->finalized.load()) return;

  js_handle_scope_t *scope;
  err = js_open_handle_scope(env, &scope);
  assert(err == 0);

  js_value_t *receiver;
  err = js_get_reference_value(env, channel->ctx, &receiver);
  assert(err == 0);

  js_call_function(env, function, js_receiver_t(receiver));

  err = js_close_handle_scope(env, scope);
  assert(err == 0);
}

static void
bare_bluetooth_apple_l2cap__on_teardown(js_deferred_teardown_t *handle, void *data) {
  auto l2cap = (__bridge BareBluetoothAppleL2CAPChannel *) data;

  if (!l2cap->destroyed.load()) {
    [l2cap destroy];
  }

  bare_bluetooth_apple_l2cap__teardown(l2cap, js_threadsafe_function_abort);
}

static js_external_t<BareBluetoothAppleL2CAPChannel>
bare_bluetooth_apple_l2cap_init(
  js_env_t *env,
  js_receiver_t,
  js_external_t<CBL2CAPChannel> channel_handle,
  js_object_t context,
  bare_bluetooth_apple_l2cap__on_data_fn on_data,
  bare_bluetooth_apple_l2cap__on_drain_fn on_drain,
  bare_bluetooth_apple_l2cap__on_end_fn on_end,
  bare_bluetooth_apple_l2cap__on_error_fn on_error,
  bare_bluetooth_apple_l2cap__on_close_fn on_close,
  bare_bluetooth_apple_l2cap__on_open_fn on_open
) {
  @autoreleasepool {
    CBL2CAPChannel *channel;
    int err = js_get_value(env, channel_handle, channel);
    assert(err == 0);

    BareBluetoothAppleL2CAPChannel *handle = [[BareBluetoothAppleL2CAPChannel alloc] init];

    handle->env = env;
    handle->channel = [channel retain];
    handle->opened.store(false);
    handle->closing.store(false);
    handle->closed.store(false);
    handle->destroyed.store(false);
    handle->finalized.store(false);

    err = js_create_reference(env, static_cast<js_value_t *>(context), 1, &handle->ctx);
    assert(err == 0);

    auto *data_ctx = new bare_bluetooth_apple_l2cap_t{(__bridge BareBluetoothAppleL2CAPChannel *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_l2cap__on_data,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_l2cap_t>,
      bare_bluetooth_apple_l2cap_t,
      bare_bluetooth_apple_l2cap_data_t>(env, on_data, 0, 1, data_ctx, handle->tsfn_data);
    assert(err == 0);

    auto *drain_ctx = new bare_bluetooth_apple_l2cap_t{(__bridge BareBluetoothAppleL2CAPChannel *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_l2cap__on_drain,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_l2cap_t>,
      bare_bluetooth_apple_l2cap_t,
      void>(env, on_drain, 0, 1, drain_ctx, handle->tsfn_drain);
    assert(err == 0);

    auto *end_ctx = new bare_bluetooth_apple_l2cap_t{(__bridge BareBluetoothAppleL2CAPChannel *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_l2cap__on_end,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_l2cap_t>,
      bare_bluetooth_apple_l2cap_t,
      void>(env, on_end, 0, 1, end_ctx, handle->tsfn_end);
    assert(err == 0);

    auto *error_ctx = new bare_bluetooth_apple_l2cap_t{(__bridge BareBluetoothAppleL2CAPChannel *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_l2cap__on_error,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_l2cap_t>,
      bare_bluetooth_apple_l2cap_t,
      bare_bluetooth_apple_l2cap_error_t>(env, on_error, 0, 1, error_ctx, handle->tsfn_error);
    assert(err == 0);

    auto *close_ctx = new bare_bluetooth_apple_l2cap_t{(__bridge BareBluetoothAppleL2CAPChannel *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_l2cap__on_close,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_l2cap_t>,
      bare_bluetooth_apple_l2cap_t,
      void>(env, on_close, 0, 1, close_ctx, handle->tsfn_close);
    assert(err == 0);

    auto *open_ctx = new bare_bluetooth_apple_l2cap_t{(__bridge BareBluetoothAppleL2CAPChannel *) CFBridgingRetain(handle)};
    err = js_create_threadsafe_function<
      bare_bluetooth_apple_l2cap__on_open,
      bare_bluetooth_apple__on_finalize<bare_bluetooth_apple_l2cap_t>,
      bare_bluetooth_apple_l2cap_t,
      void>(env, on_open, 0, 1, open_ctx, handle->tsfn_open);
    assert(err == 0);

    err = js_add_deferred_teardown_callback(env, bare_bluetooth_apple_l2cap__on_teardown, (__bridge void *) handle, &handle->teardown);
    assert(err == 0);

    js_external_t<BareBluetoothAppleL2CAPChannel> result;
    err = js_create_external<bare_bluetooth_apple__release_bridged<BareBluetoothAppleL2CAPChannel>>(env, handle, result);
    assert(err == 0);

    return result;
  }
}

static void
bare_bluetooth_apple_l2cap_open(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleL2CAPChannel> handle
) {
  @autoreleasepool {
    BareBluetoothAppleL2CAPChannel *l2cap;
    int err = js_get_value(env, handle, l2cap);
    assert(err == 0);

    [l2cap open];
  }
}

static int32_t
bare_bluetooth_apple_l2cap_write(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleL2CAPChannel> handle,
  js_uint8array_t buf
) {
  @autoreleasepool {
    BareBluetoothAppleL2CAPChannel *l2cap;
    int err = js_get_value(env, handle, l2cap);
    assert(err == 0);

    if (atomic_load(&l2cap->destroyed) || !atomic_load(&l2cap->opened)) {
      return 0;
    }

    uint8_t *data;
    size_t data_len;
    err = js_get_typedarray_info(env, buf, data, data_len);
    assert(err == 0);

    NSData *nsdata = [NSData dataWithBytes:data length:data_len];
    [l2cap performSelector:@selector(enqueueWrite:) onThread:l2cap->streamThread withObject:nsdata waitUntilDone:NO];

    return static_cast<int32_t>(data_len);
  }
}

static void
bare_bluetooth_apple_l2cap_end(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleL2CAPChannel> handle
) {
  @autoreleasepool {
    BareBluetoothAppleL2CAPChannel *l2cap;
    int err = js_get_value(env, handle, l2cap);
    assert(err == 0);

    [l2cap destroy];
  }
}

static uint32_t
bare_bluetooth_apple_l2cap_psm(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleL2CAPChannel> handle
) {
  @autoreleasepool {
    BareBluetoothAppleL2CAPChannel *l2cap;
    int err = js_get_value(env, handle, l2cap);
    assert(err == 0);

    return static_cast<uint32_t>(l2cap->channel.PSM);
  }
}

static std::optional<std::string>
bare_bluetooth_apple_l2cap_peer(
  js_env_t *env,
  js_receiver_t,
  js_external_t<BareBluetoothAppleL2CAPChannel> handle
) {
  @autoreleasepool {
    BareBluetoothAppleL2CAPChannel *l2cap;
    int err = js_get_value(env, handle, l2cap);
    assert(err == 0);

    CBPeer *peer = l2cap->channel.peer;

    if (peer) {
      return std::string(peer.identifier.UUIDString.UTF8String);
    }

    return std::nullopt;
  }
}

static js_value_t *
bare_bluetooth_apple_exports(js_env_t *env, js_value_t *exports) {
  int err;

#define V(name, fn) \
  err = js_set_property<fn>(env, exports, name); \
  assert(err == 0);

  // CBUUID
  V("createCBUUID", bare_bluetooth_apple_create_cbuuid)

  // Peripheral
  V("peripheralInit", bare_bluetooth_apple_peripheral_init)
  V("peripheralDestroy", bare_bluetooth_apple_peripheral_destroy)
  V("peripheralId", bare_bluetooth_apple_peripheral_id)
  V("peripheralName", bare_bluetooth_apple_peripheral_name)
  V("peripheralDiscoverServices", bare_bluetooth_apple_peripheral_discover_services)
  V("peripheralDiscoverCharacteristics", bare_bluetooth_apple_peripheral_discover_characteristics)
  V("peripheralRead", bare_bluetooth_apple_peripheral_read)
  V("peripheralWrite", bare_bluetooth_apple_peripheral_write)
  V("peripheralSubscribe", bare_bluetooth_apple_peripheral_subscribe)
  V("peripheralUnsubscribe", bare_bluetooth_apple_peripheral_unsubscribe)
  V("peripheralOpenL2CAPChannel", bare_bluetooth_apple_peripheral_open_l2cap_channel)
  V("peripheralServiceCount", bare_bluetooth_apple_peripheral_service_count)

  // Service/Characteristic
  V("serviceKey", bare_bluetooth_apple_service_key)
  V("serviceUuid", bare_bluetooth_apple_service_uuid)
  V("characteristicKey", bare_bluetooth_apple_characteristic_key)
  V("characteristicUuid", bare_bluetooth_apple_characteristic_uuid)
  V("characteristicProperties", bare_bluetooth_apple_characteristic_properties)
  V("serviceCharacteristicCount", bare_bluetooth_apple_service_characteristic_count)

  // Request
  V("requestCharacteristicUuid", bare_bluetooth_apple_request_characteristic_uuid)
  V("requestOffset", bare_bluetooth_apple_request_offset)
  V("requestData", bare_bluetooth_apple_request_data)

  // Mutable Service/Characteristic
  V("createMutableCharacteristic", bare_bluetooth_apple_create_mutable_characteristic)
  V("createMutableService", bare_bluetooth_apple_create_mutable_service)
  V("serviceSetCharacteristics", bare_bluetooth_apple_service_set_characteristics)

  // Central
  V("centralInit", bare_bluetooth_apple_central_init)
  V("centralStartScan", bare_bluetooth_apple_central_start_scan)
  V("centralStopScan", bare_bluetooth_apple_central_stop_scan)
  V("centralRetrievePeripherals", bare_bluetooth_apple_central_retrieve_peripherals)
  V("centralRetrieveConnectedPeripherals", bare_bluetooth_apple_central_retrieve_connected_peripherals)
  V("centralConnect", bare_bluetooth_apple_central_connect)
  V("centralDisconnect", bare_bluetooth_apple_central_disconnect)
  V("centralDestroy", bare_bluetooth_apple_central_destroy)

  // Server
  V("serverInit", bare_bluetooth_apple_server_init)
  V("serverAddService", bare_bluetooth_apple_server_add_service)
  V("serverStartAdvertising", bare_bluetooth_apple_server_start_advertising)
  V("serverStopAdvertising", bare_bluetooth_apple_server_stop_advertising)
  V("serverRespondToRequest", bare_bluetooth_apple_server_respond_to_request)
  V("serverUpdateValue", bare_bluetooth_apple_server_update_value)
  V("serverRemoveAllServices", bare_bluetooth_apple_server_remove_all_services)
  V("serverDestroy", bare_bluetooth_apple_server_destroy)
  V("serverPublishChannel", bare_bluetooth_apple_server_publish_channel)
  V("serverUnpublishChannel", bare_bluetooth_apple_server_unpublish_channel)

  // L2CAP
  V("l2capInit", bare_bluetooth_apple_l2cap_init)
  V("l2capOpen", bare_bluetooth_apple_l2cap_open)
  V("l2capWrite", bare_bluetooth_apple_l2cap_write)
  V("l2capEnd", bare_bluetooth_apple_l2cap_end)
  V("l2capPsm", bare_bluetooth_apple_l2cap_psm)
  V("l2capPeer", bare_bluetooth_apple_l2cap_peer)

#undef V

#define V(name, n) \
  { \
    js_value_t *val; \
    err = js_create_int32(env, n, &val); \
    assert(err == 0); \
    err = js_set_named_property(env, exports, name, val); \
    assert(err == 0); \
  }

  V("STATE_UNKNOWN", CBManagerStateUnknown)
  V("STATE_POWERED_ON", CBManagerStatePoweredOn)
  V("STATE_POWERED_OFF", CBManagerStatePoweredOff)
  V("STATE_RESETTING", CBManagerStateResetting)
  V("STATE_UNAUTHORIZED", CBManagerStateUnauthorized)
  V("STATE_UNSUPPORTED", CBManagerStateUnsupported)

  V("PROPERTY_READ", CBCharacteristicPropertyRead)
  V("PROPERTY_WRITE_WITHOUT_RESPONSE", CBCharacteristicPropertyWriteWithoutResponse)
  V("PROPERTY_WRITE", CBCharacteristicPropertyWrite)
  V("PROPERTY_NOTIFY", CBCharacteristicPropertyNotify)
  V("PROPERTY_INDICATE", CBCharacteristicPropertyIndicate)

  V("PERMISSION_READABLE", CBAttributePermissionsReadable)
  V("PERMISSION_WRITEABLE", CBAttributePermissionsWriteable)
  V("PERMISSION_READ_ENCRYPTED", CBAttributePermissionsReadEncryptionRequired)
  V("PERMISSION_WRITE_ENCRYPTED", CBAttributePermissionsWriteEncryptionRequired)

  V("ATT_SUCCESS", CBATTErrorSuccess)
  V("ATT_INVALID_HANDLE", CBATTErrorInvalidHandle)
  V("ATT_READ_NOT_PERMITTED", CBATTErrorReadNotPermitted)
  V("ATT_WRITE_NOT_PERMITTED", CBATTErrorWriteNotPermitted)
  V("ATT_INSUFFICIENT_RESOURCES", CBATTErrorInsufficientResources)
  V("ATT_UNLIKELY_ERROR", CBATTErrorUnlikelyError)
#undef V

  return exports;
}

BARE_MODULE(bare_bluetooth_apple, bare_bluetooth_apple_exports)
