'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var configure = require('../lib/configure.js');
var resolve = require('../lib/resolve.js');
var first = require('it-first');
var last = require('it-last');
var errCode = require('err-code');
var get = require('../block/get.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var first__default = /*#__PURE__*/_interopDefaultLegacy(first);
var last__default = /*#__PURE__*/_interopDefaultLegacy(last);
var errCode__default = /*#__PURE__*/_interopDefaultLegacy(errCode);

const createGet = (codecs, options) => {
  const fn = configure.configure((api, opts) => {
    const getBlock = get.createGet(opts);
    const get$1 = async (cid, options = {}) => {
      if (options.path) {
        const entry = options.localResolve ? await first__default["default"](resolve.resolve(cid, options.path, codecs, getBlock, options)) : await last__default["default"](resolve.resolve(cid, options.path, codecs, getBlock, options));
        const result = entry;
        if (!result) {
          throw errCode__default["default"](new Error('Not found'), 'ERR_NOT_FOUND');
        }
        return result;
      }
      const codec = await codecs.getCodec(cid.code);
      const block = await getBlock(cid, options);
      const node = codec.decode(block);
      return {
        value: node,
        remainderPath: ''
      };
    };
    return get$1;
  });
  return fn(options);
};

exports.createGet = createGet;
