import process from 'node:process';
import path__default from 'node:path';
import { promises } from 'node:fs';
import { fileURLToPath } from 'node:url';
import pLocate from '../../../p-locate@6.0.0/node_modules/p-locate/index.mjs';

const typeMappings = {
  directory: 'isDirectory',
  file: 'isFile'
};
function checkType(type) {
  if (type in typeMappings) {
    return;
  }
  throw new Error(`Invalid type specified: ${type}`);
}
const matchType = (type, stat) => type === undefined || stat[typeMappings[type]]();
const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;
async function locatePath(paths, {
  cwd = process.cwd(),
  type = 'file',
  allowSymlinks = true,
  concurrency,
  preserveOrder
} = {}) {
  checkType(type);
  cwd = toPath(cwd);
  const statFunction = allowSymlinks ? promises.stat : promises.lstat;
  return pLocate(paths, async path_ => {
    try {
      const stat = await statFunction(path__default.resolve(cwd, path_));
      return matchType(type, stat);
    } catch {
      return false;
    }
  }, {
    concurrency,
    preserveOrder
  });
}

export { locatePath };
