"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.download = exports.upload = void 0;
const headers_1 = require("../utils/headers");
const http_1 = require("../utils/http");
const bytes_1 = require("../utils/bytes");
const endpoint = 'chunks';
/**
 * Upload chunk to a Bee node
 *
 * The chunk data consists of 8 byte span and up to 4096 bytes of payload data.
 * The span stores the length of the payload in uint64 little endian encoding.
 * Upload expects the chuck data to be set accordingly.
 *
 * @param ky Ky instance
 * @param data    Chunk data to be uploaded
 * @param postageBatchId  Postage BatchId that will be assigned to uploaded data
 * @param options Additional options like tag, encryption, pinning
 */
function upload(ky, data, postageBatchId, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const response = yield (0, http_1.http)(ky, {
            method: 'post',
            path: `${endpoint}`,
            body: data,
            headers: Object.assign({ 'content-type': 'application/octet-stream' }, (0, headers_1.extractUploadHeaders)(postageBatchId, options)),
            responseType: 'json',
        });
        return response.data.reference;
    });
}
exports.upload = upload;
/**
 * Download chunk data as a byte array
 *
 * @param ky Ky instance for given Bee class instance
 * @param hash Bee content reference
 *
 */
function download(ky, hash) {
    return __awaiter(this, void 0, void 0, function* () {
        const response = yield (0, http_1.http)(ky, {
            responseType: 'arraybuffer',
            path: `${endpoint}/${hash}`,
        });
        return (0, bytes_1.wrapBytesWithHelpers)(new Uint8Array(response.data));
    });
}
exports.download = download;
