"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/winston-logging/index.ts
var winston_logging_exports = {};
__export(winston_logging_exports, {
  WinstonDestination: () => WinstonDestination,
  WinstonLoggingAddon: () => WinstonLoggingAddon,
  default: () => WinstonLoggingAddon
});
module.exports = __toCommonJS(winston_logging_exports);

// src/builtins/winston-logging/winston-destination.ts
var winston = __toESM(require("winston"));
var import_winston_daily_rotate_file = __toESM(require("winston-daily-rotate-file"));
var path = __toESM(require("path"));
function formatScope(scope) {
  if (scope.length === 0) return "";
  const [first, ...rest] = scope;
  const restFormatted = rest.map((s) => `[${s}]`).join("");
  return `(${first})${restFormatted}`;
}
var WinstonDestination = class {
  logger = null;
  async initialize(config) {
    const {
      level = "info",
      retentionDays = 30,
      logsDir = path.join("camstack-data", "logs")
    } = config ?? {};
    const consoleFormat = winston.format.combine(
      winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
      winston.format.colorize(),
      winston.format.printf(({ timestamp, level: lvl, message, scope }) => {
        const scopeStr = scope ? ` ${scope}` : "";
        return `${timestamp} [${lvl}]${scopeStr} - ${message}`;
      })
    );
    const fileFormat = winston.format.combine(
      winston.format.timestamp(),
      winston.format.json()
    );
    this.logger = winston.createLogger({
      level,
      transports: [
        new winston.transports.Console({
          format: consoleFormat
        }),
        new import_winston_daily_rotate_file.default({
          dirname: logsDir,
          filename: "camstack-%DATE%.log",
          datePattern: "YYYY-MM-DD",
          maxFiles: `${retentionDays}d`,
          format: fileFormat
        })
      ]
    });
  }
  write(entry) {
    if (!this.logger) return;
    const scope = formatScope(entry.scope);
    const meta = entry.meta ?? {};
    this.logger.log({
      level: entry.level,
      message: entry.message,
      scope,
      timestamp: entry.timestamp.toISOString(),
      ...meta
    });
  }
  async query(_filter) {
    return [];
  }
  async shutdown() {
    if (!this.logger) return;
    await new Promise((resolve) => {
      this.logger.on("finish", resolve);
      this.logger.end();
    });
    this.logger = null;
  }
};

// src/builtins/winston-logging/winston-logging.addon.ts
var WinstonLoggingAddon = class {
  manifest = {
    id: "winston-logging",
    name: "Winston Logging",
    version: "1.0.0",
    capabilities: ["log-destination"]
  };
  destination = null;
  currentConfig = {
    level: "info",
    retentionDays: 30
  };
  async initialize(context) {
    this.currentConfig = {
      level: context.addonConfig.level ?? this.currentConfig.level,
      retentionDays: context.addonConfig.retentionDays ?? this.currentConfig.retentionDays
    };
    const logsDir = context.locationPaths.logs;
    this.destination = new WinstonDestination();
    await this.destination.initialize({ ...this.currentConfig, logsDir });
    context.logger.info("Winston logging initialized");
  }
  async shutdown() {
    await this.destination?.shutdown();
  }
  getDestination() {
    if (!this.destination) throw new Error("Winston not initialized");
    return this.destination;
  }
  getCapabilityProvider(name) {
    if (name === "log-destination" && this.destination) {
      return this.destination;
    }
    return null;
  }
  getConfigSchema() {
    return {
      sections: [
        {
          id: "winston-retention",
          title: "Log Retention",
          description: "How long Winston keeps rotated log files on disk.",
          columns: 1,
          fields: [
            {
              type: "number",
              key: "retentionDays",
              label: "Retention (days)",
              description: "Number of days to keep rotated log files before deletion",
              min: 1,
              max: 365,
              step: 1,
              unit: "days"
            }
          ]
        }
      ]
    };
  }
  getConfig() {
    return { ...this.currentConfig };
  }
  async onConfigChange(config) {
    this.currentConfig = {
      level: config.level ?? this.currentConfig.level,
      retentionDays: config.retentionDays ?? this.currentConfig.retentionDays
    };
  }
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  WinstonDestination,
  WinstonLoggingAddon
});
//# sourceMappingURL=index.js.map