"use strict";
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);

// src/builtins/sqlite-storage/filesystem-storage.addon.ts
var filesystem_storage_addon_exports = {};
__export(filesystem_storage_addon_exports, {
  FilesystemStorageAddon: () => FilesystemStorageAddon,
  default: () => filesystem_storage_addon_default
});
module.exports = __toCommonJS(filesystem_storage_addon_exports);

// src/builtins/sqlite-storage/filesystem-storage-provider.ts
var fs = __toESM(require("fs"));
var path = __toESM(require("path"));
var STORAGE_LOCATION_TYPES = [
  "recordings-high",
  "recordings-low",
  "recordings-clips",
  "event-images",
  "models",
  "addons-data",
  "cache",
  "logs"
];
var DEFAULT_LOCATION_SUBDIRS = {
  "recordings-high": "recordings-high",
  "recordings-low": "recordings-low",
  "recordings-clips": "recordings-clips",
  "event-images": "event-images",
  "models": "models",
  "addons-data": "addons-data",
  "cache": "/tmp/camstack-cache",
  "logs": "logs"
};
var FilesystemStorageProvider = class {
  id = "local";
  name = "Local Filesystem";
  supportedLocations = [...STORAGE_LOCATION_TYPES];
  rootPath;
  locationPaths;
  constructor(rootPath, overrides) {
    this.rootPath = path.resolve(rootPath);
    this.locationPaths = /* @__PURE__ */ new Map();
    for (const loc of STORAGE_LOCATION_TYPES) {
      const override = overrides?.[loc];
      if (override) {
        this.locationPaths.set(loc, path.resolve(override));
      } else {
        const subdir = DEFAULT_LOCATION_SUBDIRS[loc];
        this.locationPaths.set(
          loc,
          path.isAbsolute(subdir) ? subdir : path.join(this.rootPath, subdir)
        );
      }
    }
  }
  resolve(location, relativePath) {
    const base = this.locationPaths.get(location);
    if (!base) throw new Error(`Unknown storage location: ${location}`);
    return path.join(base, relativePath);
  }
  async write(location, relativePath, data) {
    const filePath = this.resolve(location, relativePath);
    await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
    if (Buffer.isBuffer(data)) {
      await fs.promises.writeFile(filePath, data);
    } else {
      const writeStream = fs.createWriteStream(filePath);
      await new Promise((resolve2, reject) => {
        data.pipe(writeStream);
        writeStream.on("finish", resolve2);
        writeStream.on("error", reject);
      });
    }
  }
  async read(location, relativePath) {
    return fs.promises.readFile(this.resolve(location, relativePath));
  }
  async exists(location, relativePath) {
    try {
      await fs.promises.access(this.resolve(location, relativePath));
      return true;
    } catch {
      return false;
    }
  }
  async list(location, prefix) {
    const base = this.locationPaths.get(location);
    if (!base) return [];
    const dir = prefix ? path.join(base, prefix) : base;
    try {
      const entries = await fs.promises.readdir(dir, { withFileTypes: true });
      return entries.map((e) => prefix ? `${prefix}/${e.name}` : e.name);
    } catch {
      return [];
    }
  }
  async delete(location, relativePath) {
    const filePath = this.resolve(location, relativePath);
    await fs.promises.rm(filePath, { force: true });
  }
  async getAvailableSpace(location) {
    const base = this.locationPaths.get(location);
    if (!base) return null;
    try {
      const stats = await fs.promises.statfs(base);
      return stats.bavail * stats.bsize;
    } catch {
      return null;
    }
  }
  async initialize() {
    for (const [, dirPath] of this.locationPaths) {
      await fs.promises.mkdir(dirPath, { recursive: true });
    }
  }
  async shutdown() {
  }
  /** Get the resolved path for a location type */
  getLocationPath(location) {
    const p = this.locationPaths.get(location);
    if (!p) throw new Error(`Unknown storage location: ${location}`);
    return p;
  }
  /** Get the root path */
  getRootPath() {
    return this.rootPath;
  }
};

// src/builtins/sqlite-storage/filesystem-storage.addon.ts
var FilesystemStorageAddon = class {
  manifest = {
    id: "filesystem-storage",
    name: "Local Filesystem Storage",
    version: "1.0.0",
    capabilities: [{ name: "storage", mode: "collection" }]
  };
  provider = null;
  currentConfig = {
    rootPath: "camstack-data"
  };
  async initialize(context) {
    const rootPath = context.addonConfig.rootPath ?? this.currentConfig.rootPath;
    this.currentConfig.rootPath = rootPath;
    const overrides = {};
    for (const key of Object.keys(context.addonConfig)) {
      if (key.startsWith("override.")) {
        const loc = key.slice("override.".length);
        overrides[loc] = context.addonConfig[key];
      }
    }
    this.provider = new FilesystemStorageProvider(rootPath, overrides);
    await this.provider.initialize();
    context.logger.info(`Filesystem storage initialized at ${this.provider.getRootPath()}`);
  }
  async shutdown() {
    await this.provider?.shutdown();
  }
  getCapabilityProvider(name) {
    if (name === "storage" && this.provider) {
      return this.provider;
    }
    return null;
  }
  getProvider() {
    return this.provider;
  }
  getConfigSchema() {
    return { sections: [] };
  }
  getConfig() {
    return { ...this.currentConfig };
  }
  async onConfigChange(config) {
    this.currentConfig.rootPath = config.rootPath ?? this.currentConfig.rootPath;
  }
};
var filesystem_storage_addon_default = FilesystemStorageAddon;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  FilesystemStorageAddon
});
//# sourceMappingURL=filesystem-storage.addon.js.map