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 { prepareData } from "../utils/data.js";
import { extractUploadHeaders } from "../utils/headers.js";
import { http } from "../utils/http.js";
import { wrapBytesWithHelpers } from "../utils/bytes.js";
import { makeTagUid } from "../utils/type.js";
const endpoint = 'bytes';
/**
 * Upload data to a Bee node
 *
 * @param ky              Ky instance
 * @param data            Data to be uploaded
 * @param postageBatchId  Postage BatchId that will be assigned to uploaded data
 * @param options         Additional options like tag, encryption, pinning
 */

export function upload(ky, data, postageBatchId, options) {
  return __awaiter(this, void 0, void 0, function* () {
    const response = yield http(ky, {
      path: endpoint,
      method: 'post',
      responseType: 'json',
      body: yield prepareData(data),
      headers: Object.assign({
        'content-type': 'application/octet-stream'
      }, extractUploadHeaders(postageBatchId, options))
    });
    return {
      reference: response.data.reference,
      tagUid: makeTagUid(response.headers.get('swarm-tag'))
    };
  });
}
/**
 * Download data as a byte array
 *
 * @param ky
 * @param hash Bee content reference
 */

export function download(ky, hash) {
  return __awaiter(this, void 0, void 0, function* () {
    const response = yield http(ky, {
      responseType: 'arraybuffer',
      path: `${endpoint}/${hash}`
    });
    return wrapBytesWithHelpers(new Uint8Array(response.data));
  });
}
/**
 * Download data as a readable stream
 *
 * @param ky
 * @param hash Bee content reference
 */

export function downloadReadable(ky, hash) {
  return __awaiter(this, void 0, void 0, function* () {
    const response = yield http(ky, {
      responseType: 'stream',
      path: `${endpoint}/${hash}`
    });
    return response.data;
  });
}