'use client';
import { _ as _classPrivateFieldLooseBase, p as protobufsBySchema, b as _objectSpread2, a as _classPrivateFieldLooseKey } from './protobuf-OcbVtxg1.js';
import { r as recordToDataView } from './encoding-jXImCIb4.js';
import { L as Logger } from './logger-Bqy1nuLj.js';

const logger = Logger.getLogger();

// | { type: "custom"; data: any };
var _services = /*#__PURE__*/_classPrivateFieldLooseKey("services");
var _channels = /*#__PURE__*/_classPrivateFieldLooseKey("channels");
var _serviceCalls = /*#__PURE__*/_classPrivateFieldLooseKey("serviceCalls");
var _getPublicServices = /*#__PURE__*/_classPrivateFieldLooseKey("getPublicServices");
class FoxgloveCommunicator {
  constructor() {
    Object.defineProperty(this, _getPublicServices, {
      value: _getPublicServices2
    });
    Object.defineProperty(this, _services, {
      writable: true,
      value: new Map()
    });
    Object.defineProperty(this, _channels, {
      writable: true,
      value: new Map()
    });
    Object.defineProperty(this, _serviceCalls, {
      writable: true,
      value: new Map()
    });
  }
  getPublicChannels() {
    return [..._classPrivateFieldLooseBase(this, _channels)[_channels].values()];
  }
  handleReconnect() {
    _classPrivateFieldLooseBase(this, _services)[_services].clear();
    _classPrivateFieldLooseBase(this, _channels)[_channels].clear();
    this.send({
      type: "reconnect"
    });
  }
  handleChannelsAdvertisement(channels) {
    for (const channel of channels) {
      var _protobufsBySchema$ch, _protobufsBySchema$ch2;
      const kind = (_protobufsBySchema$ch = (_protobufsBySchema$ch2 = protobufsBySchema[channel.schemaName]) === null || _protobufsBySchema$ch2 === void 0 ? void 0 : _protobufsBySchema$ch2.kind) !== null && _protobufsBySchema$ch !== void 0 ? _protobufsBySchema$ch : 'annotations';
      _classPrivateFieldLooseBase(this, _channels)[_channels].set(channel.id, _objectSpread2(_objectSpread2({}, channel), {}, {
        kind
      }));
    }
    const data = this.getPublicChannels();
    this.send({
      type: "channels",
      data
    });
  }
  handleChannelsUnadvertisement(channels) {
    for (const channel of channels) {
      _classPrivateFieldLooseBase(this, _channels)[_channels].delete(channel);
    }
    const data = this.getPublicChannels();
    this.send({
      type: "channels",
      data
    });
  }
  handleServicesAdvertisement(services) {
    for (const service of services) {
      _classPrivateFieldLooseBase(this, _services)[_services].set(service.name, service);
    }
    const data = _classPrivateFieldLooseBase(this, _getPublicServices)[_getPublicServices]();
    this.send({
      type: "services",
      data
    });
  }
  handleServicesUnadvertisement(services) {
    for (const unadvertisedService of services) {
      for (const [name, service] of _classPrivateFieldLooseBase(this, _services)[_services]) {
        if (service.id === unadvertisedService) {
          _classPrivateFieldLooseBase(this, _services)[_services].delete(name);
        }
      }
    }
    const data = _classPrivateFieldLooseBase(this, _getPublicServices)[_getPublicServices]();
    this.send({
      type: "services",
      data
    });
  }
  handleServiceResponse(response) {
    var _classPrivateFieldLoo;
    const name = (_classPrivateFieldLoo = _classPrivateFieldLooseBase(this, _serviceCalls)[_serviceCalls].get(response.callId)) !== null && _classPrivateFieldLoo !== void 0 ? _classPrivateFieldLoo : 'unknown';
    _classPrivateFieldLooseBase(this, _serviceCalls)[_serviceCalls].delete(response.callId);
    this.sendWithTransfer({
      type: "service_response",
      data: {
        name,
        ok: true,
        serviceCallId: response.callId,
        buffer: response.data,
        error: null
      }
    }, [response.data.buffer]);
  }
  handleGenericMessage(topic, schema, buffer) {
    this.sendWithTransfer({
      type: "message",
      data: {
        topic,
        schema,
        buffer
      }
    }, [buffer.buffer]);
  }
  callService({
    name,
    serviceCallId,
    body = {}
  }) {
    const service = _classPrivateFieldLooseBase(this, _services)[_services].get(name);
    if (!service) {
      this.send({
        type: "service_response",
        data: {
          name,
          serviceCallId,
          ok: false,
          error: "Service was not advertised",
          buffer: null
        }
      });
      return;
    }
    if (this.isReady()) {
      _classPrivateFieldLooseBase(this, _serviceCalls)[_serviceCalls].set(serviceCallId, name);
      this.sendServiceCallRequest({
        serviceId: service.id,
        callId: serviceCallId,
        encoding: "json",
        data: recordToDataView(body)
      });
    } else {
      logger.warn('Foxglove Communicator Error: requested service call, but connection is not ready');
    }
  }
}
function _getPublicServices2() {
  return [..._classPrivateFieldLooseBase(this, _services)[_services].values()].map(service => service.name);
}

export { FoxgloveCommunicator as F };
