"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var NodeJsDirectory_exports = {};
__export(NodeJsDirectory_exports, {
  NodeJsDirectory: () => NodeJsDirectory
});
module.exports = __toCommonJS(NodeJsDirectory_exports);
var import_general = require("@matter/general");
var import_promises = require("node:fs/promises");
var import_node_path = require("node:path");
var import_fs_utils = require("./fs-utils.js");
var import_lock_utils = require("./lock-utils.js");
var import_NodeJsFile = require("./NodeJsFile.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class NodeJsDirectory extends import_general.Directory {
  #fs;
  #path;
  #name;
  #cachedStat;
  constructor(fs, path, name, cachedStat) {
    super();
    this.#fs = fs;
    this.#path = path;
    this.#name = name;
    this.#cachedStat = cachedStat;
  }
  get fs() {
    return this.#fs;
  }
  get name() {
    return this.#name;
  }
  get path() {
    return this.#path;
  }
  async exists() {
    try {
      const s = await (0, import_promises.stat)(this.#path);
      return s.isDirectory();
    } catch (e) {
      if (e.code === "ENOENT") {
        return false;
      }
      throw e;
    }
  }
  stat() {
    if (this.#cachedStat) {
      return this.#cachedStat;
    }
    return (0, import_fs_utils.nodeStat)(this.#path);
  }
  async rename(newName) {
    const newPath = (0, import_node_path.resolve)((0, import_node_path.dirname)(this.#path), newName);
    await (0, import_promises.rename)(this.#path, newPath);
    this.#path = newPath;
    this.#name = (0, import_node_path.basename)(newPath);
  }
  async delete() {
    await (0, import_promises.rm)(this.#path, { recursive: true, force: true });
  }
  async *entries() {
    yield* (0, import_fs_utils.nodeEntries)(this.#fs, this.#path, import_NodeJsFile.NodeJsFile, NodeJsDirectory);
  }
  file(name) {
    return new import_NodeJsFile.NodeJsFile(this.#fs, (0, import_node_path.resolve)(this.#path, name), name);
  }
  directory(name) {
    return new NodeJsDirectory(this.#fs, (0, import_node_path.resolve)(this.#path, name), name);
  }
  async mkdir() {
    await (0, import_promises.mkdir)(this.#path, { recursive: true });
  }
  async copy(source, target) {
    await (0, import_fs_utils.nodeCopy)(this.#path, source, target);
  }
  async lock() {
    await this.mkdir();
    return (0, import_lock_utils.acquireDirectoryLock)(this.#path, this.#name);
  }
}
//# sourceMappingURL=NodeJsDirectory.js.map
