'use strict';

var path2 = require('path');
var fs = require('fs');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var path2__default = /*#__PURE__*/_interopDefault(path2);
var fs__default = /*#__PURE__*/_interopDefault(fs);

function readFile(filePath) {
  return new Promise((resolve, reject) => {
    fs__default.default.readFile(filePath, "utf8", (error, result) => {
      if (error) {
        reject(error);
      } else {
        resolve(result);
      }
    });
  });
}

// src/getDataPath.ts
async function getDataPath(directoryPath, fileName) {
  const filePath = path2__default.default.join(directoryPath, fileName);
  const fileContent = await readFile(filePath);
  const data = JSON.parse(fileContent);
  return {
    path: filePath,
    data
  };
}

// src/findNearestFile.ts
async function findNearestFile(fileName, directoryPath = path2__default.default.resolve()) {
  try {
    const data = await getDataPath(directoryPath, fileName);
    return data;
  } catch (error) {
    const parentDirectoryPath = path2__default.default.dirname(directoryPath);
    if (parentDirectoryPath === directoryPath) {
      throw new Error(`No ${fileName} files found`);
    }
    return findNearestFile(parentDirectoryPath, fileName);
  }
}

exports.findNearestFile = findNearestFile;
