import fs from "node:fs";
import { promisify } from "node:util";
import { r as retryifySync, a as retryifyAsync, b as attemptifySync, c as attemptifyAsync } from "./stubborn-utils.mjs";
import process from "node:process";
const Handlers = {
  /* API */
  isChangeErrorOk: (error) => {
    if (!Handlers.isNodeError(error))
      return false;
    const { code } = error;
    if (code === "ENOSYS")
      return true;
    if (!IS_USER_ROOT && (code === "EINVAL" || code === "EPERM"))
      return true;
    return false;
  },
  isNodeError: (error) => {
    return error instanceof Error;
  },
  isRetriableError: (error) => {
    if (!Handlers.isNodeError(error))
      return false;
    const { code } = error;
    if (code === "EMFILE" || code === "ENFILE" || code === "EAGAIN" || code === "EBUSY" || code === "EACCESS" || code === "EACCES" || code === "EACCS" || code === "EPERM")
      return true;
    return false;
  },
  onChangeError: (error) => {
    if (!Handlers.isNodeError(error))
      throw error;
    if (Handlers.isChangeErrorOk(error))
      return;
    throw error;
  }
};
const ATTEMPTIFY_CHANGE_ERROR_OPTIONS = {
  onError: Handlers.onChangeError
};
const ATTEMPTIFY_NOOP_OPTIONS = {
  onError: () => void 0
};
const IS_USER_ROOT = process.getuid ? !process.getuid() : false;
const RETRYIFY_OPTIONS = {
  isRetriable: Handlers.isRetriableError
};
const FS = {
  attempt: {
    /* ASYNC */
    chmod: attemptifyAsync(promisify(fs.chmod), ATTEMPTIFY_CHANGE_ERROR_OPTIONS),
    chown: attemptifyAsync(promisify(fs.chown), ATTEMPTIFY_CHANGE_ERROR_OPTIONS),
    close: attemptifyAsync(promisify(fs.close), ATTEMPTIFY_NOOP_OPTIONS),
    fsync: attemptifyAsync(promisify(fs.fsync), ATTEMPTIFY_NOOP_OPTIONS),
    mkdir: attemptifyAsync(promisify(fs.mkdir), ATTEMPTIFY_NOOP_OPTIONS),
    realpath: attemptifyAsync(promisify(fs.realpath), ATTEMPTIFY_NOOP_OPTIONS),
    stat: attemptifyAsync(promisify(fs.stat), ATTEMPTIFY_NOOP_OPTIONS),
    unlink: attemptifyAsync(promisify(fs.unlink), ATTEMPTIFY_NOOP_OPTIONS),
    /* SYNC */
    chmodSync: attemptifySync(fs.chmodSync, ATTEMPTIFY_CHANGE_ERROR_OPTIONS),
    chownSync: attemptifySync(fs.chownSync, ATTEMPTIFY_CHANGE_ERROR_OPTIONS),
    closeSync: attemptifySync(fs.closeSync, ATTEMPTIFY_NOOP_OPTIONS),
    existsSync: attemptifySync(fs.existsSync, ATTEMPTIFY_NOOP_OPTIONS),
    fsyncSync: attemptifySync(fs.fsync, ATTEMPTIFY_NOOP_OPTIONS),
    mkdirSync: attemptifySync(fs.mkdirSync, ATTEMPTIFY_NOOP_OPTIONS),
    realpathSync: attemptifySync(fs.realpathSync, ATTEMPTIFY_NOOP_OPTIONS),
    statSync: attemptifySync(fs.statSync, ATTEMPTIFY_NOOP_OPTIONS),
    unlinkSync: attemptifySync(fs.unlinkSync, ATTEMPTIFY_NOOP_OPTIONS)
  },
  retry: {
    /* ASYNC */
    close: retryifyAsync(promisify(fs.close), RETRYIFY_OPTIONS),
    fsync: retryifyAsync(promisify(fs.fsync), RETRYIFY_OPTIONS),
    open: retryifyAsync(promisify(fs.open), RETRYIFY_OPTIONS),
    readFile: retryifyAsync(promisify(fs.readFile), RETRYIFY_OPTIONS),
    rename: retryifyAsync(promisify(fs.rename), RETRYIFY_OPTIONS),
    stat: retryifyAsync(promisify(fs.stat), RETRYIFY_OPTIONS),
    write: retryifyAsync(promisify(fs.write), RETRYIFY_OPTIONS),
    writeFile: retryifyAsync(promisify(fs.writeFile), RETRYIFY_OPTIONS),
    /* SYNC */
    closeSync: retryifySync(fs.closeSync, RETRYIFY_OPTIONS),
    fsyncSync: retryifySync(fs.fsyncSync, RETRYIFY_OPTIONS),
    openSync: retryifySync(fs.openSync, RETRYIFY_OPTIONS),
    readFileSync: retryifySync(fs.readFileSync, RETRYIFY_OPTIONS),
    renameSync: retryifySync(fs.renameSync, RETRYIFY_OPTIONS),
    statSync: retryifySync(fs.statSync, RETRYIFY_OPTIONS),
    writeSync: retryifySync(fs.writeSync, RETRYIFY_OPTIONS),
    writeFileSync: retryifySync(fs.writeFileSync, RETRYIFY_OPTIONS)
  }
};
export {
  FS as F
};
