import { CID } from 'multiformats/cid';
import { multipartRequest } from 'ipfs-core-utils/multipart-request';
import { configure } from '../../lib/configure.js';
import { toUrlSearchParams } from '../../lib/to-url-search-params.js';
import { abortSignal } from '../../lib/abort-signal.js';
export const createAppendData = configure(api => {
  async function appendData(cid, data, options = {}) {
    const controller = new AbortController();
    const signal = abortSignal(controller.signal, options.signal);
    const res = await api.post('object/patch/append-data', {
      signal,
      searchParams: toUrlSearchParams({
        arg: `${ cid }`,
        ...options
      }),
      ...await multipartRequest([data], controller, options.headers)
    });
    const {Hash} = await res.json();
    return CID.parse(Hash);
  }
  return appendData;
});