var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
  function adopt(value) {
    return value instanceof P ? value : new P(function (resolve) {
      resolve(value);
    });
  }

  return new (P || (P = Promise))(function (resolve, reject) {
    function fulfilled(value) {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    }

    function rejected(value) {
      try {
        step(generator["throw"](value));
      } catch (e) {
        reject(e);
      }
    }

    function step(result) {
      result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
    }

    step((generator = generator.apply(thisArg, _arguments || [])).next());
  });
};

import WebSocket from 'isomorphic-ws';
import { prepareData } from "../utils/data.js";
import { http } from "../utils/http.js";
import { extractUploadHeaders } from "../utils/headers.js";
const endpoint = 'pss';
/**
 * Send to recipient or target with Postal Service for Swarm
 *
 * @param ky Ky instance for given Bee class instance
 * @param topic Topic name
 * @param target Target message address prefix
 * @param data
 * @param postageBatchId Postage BatchId that will be assigned to sent message
 * @param recipient Recipient public key
 *
 */

export function send(ky, topic, target, data, postageBatchId, recipient) {
  return __awaiter(this, void 0, void 0, function* () {
    yield http(ky, {
      method: 'post',
      path: `${endpoint}/send/${topic}/${target}`,
      body: yield prepareData(data),
      responseType: 'json',
      searchParams: {
        recipient
      },
      headers: extractUploadHeaders(postageBatchId)
    });
  });
}
/**
 * Subscribe for messages on the given topic
 *
 * @param url Bee node URL
 * @param topic Topic name
 */

export function subscribe(url, topic) {
  const wsUrl = url.replace(/^http/i, 'ws');
  return new WebSocket(`${wsUrl}/${endpoint}/subscribe/${topic}`);
}