// src/builtins/winston-logging/winston-destination.ts
import * as winston from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
import * as path from "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 DailyRotateFile({
          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
    };
  }
};

export {
  WinstonDestination,
  WinstonLoggingAddon
};
//# sourceMappingURL=chunk-LQFPAEQF.mjs.map