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 createSetData = configure(api => {
  async function setData(cid, data, options = {}) {
    const controller = new AbortController();
    const signal = abortSignal(controller.signal, options.signal);
    const res = await api.post('object/patch/set-data', {
      signal,
      searchParams: toUrlSearchParams({
        arg: [`${ cid }`],
        ...options
      }),
      ...await multipartRequest([data], controller, options.headers)
    });
    const {Hash} = await res.json();
    return CID.parse(Hash);
  }
  return setData;
});