import { CID } from 'multiformats/cid';
import { configure } from '../../lib/configure.js';
import { toUrlSearchParams } from '../../lib/to-url-search-params.js';
export const createAddLink = configure(api => {
  async function addLink(cid, dLink, options = {}) {
    const res = await api.post('object/patch/add-link', {
      signal: options.signal,
      searchParams: toUrlSearchParams({
        arg: [
          `${ cid }`,
          dLink.Name || dLink.name || '',
          (dLink.Hash || dLink.cid || '').toString() || null
        ],
        ...options
      }),
      headers: options.headers
    });
    const {Hash} = await res.json();
    return CID.parse(Hash);
  }
  return addLink;
});