import { __exports as async } from '../../../../../../../../_virtual/async6.mjs';
import '../../../../../../@nodelib_fs.stat@2.0.5/node_modules/@nodelib/fs.stat/out/index.mjs';
import { runParallel_1 } from '../../../../../../run-parallel@1.2.0/node_modules/run-parallel/index.mjs';
import '../constants.mjs';
import '../utils/index.mjs';
import './common.mjs';
import { __exports as out } from '../../../../../../../../_virtual/index5.mjs';
import { __exports as constants } from '../../../../../../../../_virtual/constants.mjs';
import { __exports as utils$1 } from '../../../../../../../../_virtual/index7.mjs';
import { __exports as common$1 } from '../../../../../../../../_virtual/common2.mjs';

Object.defineProperty(async, "__esModule", {
  value: true
});
async.readdir = async.readdirWithFileTypes = async.read = void 0;
const fsStat = out;
const rpl = runParallel_1;
const constants_1 = constants;
const utils = utils$1;
const common = common$1;
function read(directory, settings, callback) {
  if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
    readdirWithFileTypes(directory, settings, callback);
    return;
  }
  readdir(directory, settings, callback);
}
async.read = read;
function readdirWithFileTypes(directory, settings, callback) {
  settings.fs.readdir(directory, {
    withFileTypes: true
  }, (readdirError, dirents) => {
    if (readdirError !== null) {
      callFailureCallback(callback, readdirError);
      return;
    }
    const entries = dirents.map(dirent => ({
      dirent,
      name: dirent.name,
      path: common.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
    }));
    if (!settings.followSymbolicLinks) {
      callSuccessCallback(callback, entries);
      return;
    }
    const tasks = entries.map(entry => makeRplTaskEntry(entry, settings));
    rpl(tasks, (rplError, rplEntries) => {
      if (rplError !== null) {
        callFailureCallback(callback, rplError);
        return;
      }
      callSuccessCallback(callback, rplEntries);
    });
  });
}
async.readdirWithFileTypes = readdirWithFileTypes;
function makeRplTaskEntry(entry, settings) {
  return done => {
    if (!entry.dirent.isSymbolicLink()) {
      done(null, entry);
      return;
    }
    settings.fs.stat(entry.path, (statError, stats) => {
      if (statError !== null) {
        if (settings.throwErrorOnBrokenSymbolicLink) {
          done(statError);
          return;
        }
        done(null, entry);
        return;
      }
      entry.dirent = utils.fs.createDirentFromStats(entry.name, stats);
      done(null, entry);
    });
  };
}
function readdir(directory, settings, callback) {
  settings.fs.readdir(directory, (readdirError, names) => {
    if (readdirError !== null) {
      callFailureCallback(callback, readdirError);
      return;
    }
    const tasks = names.map(name => {
      const path = common.joinPathSegments(directory, name, settings.pathSegmentSeparator);
      return done => {
        fsStat.stat(path, settings.fsStatSettings, (error, stats) => {
          if (error !== null) {
            done(error);
            return;
          }
          const entry = {
            name,
            path,
            dirent: utils.fs.createDirentFromStats(name, stats)
          };
          if (settings.stats) {
            entry.stats = stats;
          }
          done(null, entry);
        });
      };
    });
    rpl(tasks, (rplError, entries) => {
      if (rplError !== null) {
        callFailureCallback(callback, rplError);
        return;
      }
      callSuccessCallback(callback, entries);
    });
  });
}
async.readdir = readdir;
function callFailureCallback(callback, error) {
  callback(error);
}
function callSuccessCallback(callback, result) {
  callback(null, result);
}

export { async as default };
