import { CID } from 'multiformats/cid';
import { toUrlSearchParams } from '../../lib/to-url-search-params.js';
export const decodePin = ({
  Name: name,
  Status: status,
  Cid: cid
}) => {
  return {
    cid: CID.parse(cid),
    name,
    status
  };
};
export const encodeService = service => {
  if (typeof service === 'string' && service !== '') {
    return service;
  } else {
    throw new TypeError('service name must be passed');
  }
};
export const encodeCID = cid => {
  if (CID.asCID(cid)) {
    return cid.toString();
  } else {
    throw new TypeError(`CID instance expected instead of ${ typeof cid }`);
  }
};
export const encodeQuery = ({service, cid, name, status, all}) => {
  const query = toUrlSearchParams({
    service: encodeService(service),
    name,
    force: all ? true : undefined
  });
  if (cid) {
    for (const value of cid) {
      query.append('cid', encodeCID(value));
    }
  }
  if (status) {
    for (const value of status) {
      query.append('status', value);
    }
  }
  return query;
};
export const encodeAddParams = ({cid, service, background, name, origins}) => {
  const params = toUrlSearchParams({
    arg: encodeCID(cid),
    service: encodeService(service),
    name,
    background: background ? true : undefined
  });
  if (origins) {
    for (const origin of origins) {
      params.append('origin', origin.toString());
    }
  }
  return params;
};