'use client';
import { _ as _classPrivateFieldLooseBase, a as _classPrivateFieldLooseKey } from './protobuf-OcbVtxg1.js';
import { r as recordToDataView } from './encoding-jXImCIb4.js';
import { F as FoxgloveCommunicator } from './communicator-Dh2aL-rj.js';
import { m as makeServiceCallRequest } from './foxglove-protocol-BtLMVx24.js';
import './logger-Bqy1nuLj.js';
import './FoxgloveServer-Da9kV1cr.js';
import 'ms';

var _subscriptionId = /*#__PURE__*/_classPrivateFieldLooseKey("subscriptionId");
var _messageReady = /*#__PURE__*/_classPrivateFieldLooseKey("messageReady");
var _systemReady = /*#__PURE__*/_classPrivateFieldLooseKey("systemReady");
var _subscriptions = /*#__PURE__*/_classPrivateFieldLooseKey("subscriptions");
var _rawSubscribe = /*#__PURE__*/_classPrivateFieldLooseKey("rawSubscribe");
var _rawUnsubscribe = /*#__PURE__*/_classPrivateFieldLooseKey("rawUnsubscribe");
class WebRtcHandler extends FoxgloveCommunicator {
  constructor(...args) {
    super(...args);
    Object.defineProperty(this, _rawUnsubscribe, {
      value: _rawUnsubscribe2
    });
    Object.defineProperty(this, _rawSubscribe, {
      value: _rawSubscribe2
    });
    Object.defineProperty(this, _subscriptionId, {
      writable: true,
      value: 0
    });
    Object.defineProperty(this, _messageReady, {
      writable: true,
      value: false
    });
    Object.defineProperty(this, _systemReady, {
      writable: true,
      value: false
    });
    Object.defineProperty(this, _subscriptions, {
      writable: true,
      value: new Map()
    });
  }
  isReady() {
    return _classPrivateFieldLooseBase(this, _messageReady)[_messageReady] && _classPrivateFieldLooseBase(this, _systemReady)[_systemReady];
  }
  send(message) {
    self.postMessage(message);
  }
  sendWithTransfer(message, transfer) {
    self.postMessage(message, transfer);
  }
  subscribe(topics) {
    const channels = this.getPublicChannels().filter(publicChannel => topics.includes(publicChannel.topic));
    for (const channel of channels) {
      const subscription = _classPrivateFieldLooseBase(this, _rawSubscribe)[_rawSubscribe](channel.id);
      _classPrivateFieldLooseBase(this, _subscriptions)[_subscriptions].set(subscription, {
        topic: channel.topic,
        schema: channel.schemaName,
        kind: channel.kind
      });
    }
  }
  unsubscribe(topics) {
    const subscriptionIds = [..._classPrivateFieldLooseBase(this, _subscriptions)[_subscriptions].entries()].filter(([_id, data]) => topics.includes(data.topic)).map(subscription => subscription[0]);
    for (const subscriptionId of subscriptionIds) {
      _classPrivateFieldLooseBase(this, _subscriptions)[_subscriptions].delete(subscriptionId);
      _classPrivateFieldLooseBase(this, _rawUnsubscribe)[_rawUnsubscribe](subscriptionId);
    }
  }
  sendServiceCallRequest(args) {
    const data = makeServiceCallRequest(args);
    self.postMessage({
      type: "send",
      data: {
        channel: "message",
        data
      }
    }, [data.buffer]);
  }
  handleRawSend(data) {
    self.postMessage({
      type: "send",
      data
    });
  }
  handleConnection(data) {
    if (!data.channel || data.channel === "message") {
      _classPrivateFieldLooseBase(this, _messageReady)[_messageReady] = data.ready;
    }
    if (!data.channel || data.channel === "system") {
      _classPrivateFieldLooseBase(this, _systemReady)[_systemReady] = data.ready;
    }
  }
  handleGeneric(message) {
    const subscription = _classPrivateFieldLooseBase(this, _subscriptions)[_subscriptions].get(message.subscriptionId);
    if (subscription) {
      this.handleGenericMessage(subscription.topic, subscription.schema, message.data);
    }
  }
  handleDisconnect(error) {
    this.send({
      type: "disconnect",
      error
    });
  }
  handleServiceCall(message) {
    this.callService(message);
  }
  handleSystem(message) {
    switch (message.op) {
      case "advertise":
        {
          this.handleChannelsAdvertisement(message.channels);
          break;
        }
      case "unadvertise":
        {
          this.handleChannelsUnadvertisement(message.channelIds);
          break;
        }
      case "advertiseServices":
        {
          this.handleServicesAdvertisement(message.services);
          break;
        }
      case "unadvertiseServices":
        {
          this.handleServicesUnadvertisement(message.serviceIds);
          break;
        }
      case "serviceResponse":
        {
          this.handleServiceResponse(message);
          break;
        }
    }
  }
}
function _rawSubscribe2(channelId) {
  const subscriptionId = ++_classPrivateFieldLooseBase(this, _subscriptionId)[_subscriptionId];
  const subscriptions = [{
    id: subscriptionId,
    channelId
  }];
  self.postMessage({
    type: "send",
    data: {
      channel: "system",
      data: recordToDataView({
        op: "subscribe",
        subscriptions
      })
    }
  });
  return subscriptionId;
}
function _rawUnsubscribe2(subscriptionId) {
  self.postMessage({
    type: "send",
    data: {
      channel: "system",
      data: recordToDataView({
        op: "unsubscribe",
        subscriptionIds: [subscriptionId]
      })
    }
  });
}

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/

const handler = new WebRtcHandler();
self.addEventListener("message", event => {
  if (event.data.kind === 'genericMessage') {
    handler.handleGeneric(event.data.data.message);
  } else if (event.data.kind === 'systemMessage') {
    handler.handleSystem(event.data.data.message);
  } else if (event.data.kind === 'serviceCall') {
    handler.handleServiceCall(event.data.data.message);
  } else if (event.data.kind === 'rawSend') {
    handler.handleRawSend({
      channel: event.data.data.message.channel,
      data: event.data.data.message.message
    });
  } else if (event.data.kind === 'subscribe') {
    handler.subscribe(event.data.data.message.topics);
  } else if (event.data.kind === 'unsubscribe') {
    handler.unsubscribe(event.data.data.message.topics);
  } else if (event.data.kind === 'disconnect') {
    handler.handleDisconnect(event.data.error);
  } else {
    handler.handleConnection(event.data.data);
  }
});
