"use strict";
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.subscribe = exports.send = void 0;
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
const data_1 = require("../utils/data");
const http_1 = require("../utils/http");
const headers_1 = require("../utils/headers");
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
 *
 */
function send(ky, topic, target, data, postageBatchId, recipient) {
    return __awaiter(this, void 0, void 0, function* () {
        yield (0, http_1.http)(ky, {
            method: 'post',
            path: `${endpoint}/send/${topic}/${target}`,
            body: yield (0, data_1.prepareData)(data),
            responseType: 'json',
            searchParams: { recipient },
            headers: (0, headers_1.extractUploadHeaders)(postageBatchId),
        });
    });
}
exports.send = send;
/**
 * Subscribe for messages on the given topic
 *
 * @param url Bee node URL
 * @param topic Topic name
 */
function subscribe(url, topic) {
    const wsUrl = url.replace(/^http/i, 'ws');
    return new isomorphic_ws_1.default(`${wsUrl}/${endpoint}/subscribe/${topic}`);
}
exports.subscribe = subscribe;
