var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var local_exports = {};
__export(local_exports, {
  default: () => local_default
});
module.exports = __toCommonJS(local_exports);
var import_node_fs = require("node:fs");
var import_promises = __toESM(require("node:fs/promises"));
var import_node_path = __toESM(require("node:path"));
var import_multer = __toESM(require("multer"));
var import_constants = require("../constants");
var import_utils = require("../utils");
function getDocumentRoot(storage) {
  const { documentRoot = process.env.LOCAL_STORAGE_DEST || "storage/uploads" } = storage.options || {};
  return import_node_path.default.resolve(
    import_node_path.default.isAbsolute(documentRoot) ? documentRoot : import_node_path.default.join(process.env.TEGO_RUNTIME_HOME, documentRoot)
  );
}
var local_default = {
  make(storage) {
    return import_multer.default.diskStorage({
      destination: function(req, file, cb) {
        const destPath = import_node_path.default.join(getDocumentRoot(storage), storage.path);
        (0, import_node_fs.mkdir)(destPath, { recursive: true }, (err) => cb(err, destPath));
      },
      filename: import_utils.getFilename
    });
  },
  defaults() {
    return {
      title: "Local storage",
      type: import_constants.STORAGE_TYPE_LOCAL,
      name: `local`,
      baseUrl: "/storage/uploads",
      options: {
        documentRoot: "storage/uploads"
      }
    };
  },
  async delete(storage, records) {
    const documentRoot = getDocumentRoot(storage);
    let count = 0;
    const undeleted = [];
    await records.reduce(
      (promise, record) => promise.then(async () => {
        try {
          await import_promises.default.unlink(import_node_path.default.join(documentRoot, record.path, record.filename));
          count += 1;
        } catch (ex) {
          if (ex.code === "ENOENT") {
            console.warn(ex.message);
            count += 1;
          } else {
            console.error(ex);
            undeleted.push(record);
          }
        }
      }),
      Promise.resolve()
    );
    return [count, undeleted];
  }
};
