import { resolveCurrentSpan } from './chunk-JK5J6E2A.js';
import { Transform } from 'stream';

// src/logger/multi-logger.ts
var MultiLogger = class {
  loggers;
  constructor(loggers) {
    this.loggers = loggers;
  }
  debug(message, ...args) {
    this.loggers.forEach((logger) => logger.debug(message, ...args));
  }
  info(message, ...args) {
    this.loggers.forEach((logger) => logger.info(message, ...args));
  }
  warn(message, ...args) {
    this.loggers.forEach((logger) => logger.warn(message, ...args));
  }
  error(message, ...args) {
    this.loggers.forEach((logger) => logger.error(message, ...args));
  }
  trackException(error, metadata) {
    this.loggers.forEach((logger) => logger.trackException(error, metadata));
  }
  getTransports() {
    const transports = [];
    this.loggers.forEach((logger) => transports.push(...logger.getTransports().entries()));
    return new Map(transports);
  }
  async listLogs(transportId, params) {
    for (const logger of this.loggers) {
      const logs = await logger.listLogs(transportId, params);
      if (logs.total > 0) {
        return logs;
      }
    }
    return { logs: [], total: 0, page: params?.page ?? 1, perPage: params?.perPage ?? 100, hasMore: false };
  }
  async listLogsByRunId(args) {
    for (const logger of this.loggers) {
      const logs = await logger.listLogsByRunId(args);
      if (logs.total > 0) {
        return logs;
      }
    }
    return { logs: [], total: 0, page: args.page ?? 1, perPage: args.perPage ?? 100, hasMore: false };
  }
};

// src/logger/dual-logger.ts
var DualLogger = class {
  #inner;
  #getLoggerVNext;
  constructor(inner, getLoggerVNext) {
    this.#inner = inner;
    this.#getLoggerVNext = getLoggerVNext;
  }
  /**
   * Set or update the loggerVNext getter.
   * Called after observability initializes (which may happen after logger creation).
   */
  setLoggerVNext(getLoggerVNext) {
    this.#getLoggerVNext = getLoggerVNext;
  }
  debug(message, ...args) {
    this.#inner.debug(message, ...args);
    this.#forwardToVNext("debug", message, args);
  }
  info(message, ...args) {
    this.#inner.info(message, ...args);
    this.#forwardToVNext("info", message, args);
  }
  warn(message, ...args) {
    this.#inner.warn(message, ...args);
    this.#forwardToVNext("warn", message, args);
  }
  error(message, ...args) {
    this.#inner.error(message, ...args);
    this.#forwardToVNext("error", message, args);
  }
  trackException(error, metadata) {
    this.#inner.trackException(error, metadata);
    try {
      const loggerVNext = this.#resolveLoggerVNext();
      loggerVNext?.error(error.message, {
        errorId: error.id,
        domain: error.domain,
        category: error.category,
        details: error.details,
        cause: error.cause?.message,
        ...metadata
      });
    } catch {
    }
  }
  getTransports() {
    return this.#inner.getTransports();
  }
  async listLogs(transportId, params) {
    return this.#inner.listLogs(transportId, params);
  }
  async listLogsByRunId(args) {
    return this.#inner.listLogsByRunId(args);
  }
  /**
   * Resolve the best available LoggerContext:
   * 1. Span-correlated loggerVNext from AsyncLocalStorage (has traceId/spanId)
   * 2. Global loggerVNext from the lazy getter (no correlation, still persisted)
   */
  #resolveLoggerVNext() {
    const span = resolveCurrentSpan();
    if (span) {
      const correlated = span.observabilityInstance?.getLoggerContext?.(span);
      if (correlated) return correlated;
    }
    return this.#getLoggerVNext?.();
  }
  /**
   * Adapt IMastraLogger's variadic args to LoggerContext's structured data param.
   * Extracts the first plain object as `data`, serializes Error args, and
   * collects any remaining primitives so the dual write preserves all context.
   */
  #forwardToVNext(level, message, args) {
    try {
      const loggerVNext = this.#resolveLoggerVNext();
      if (!loggerVNext) return;
      const objectData = args.find(
        (arg) => arg !== null && typeof arg === "object" && !Array.isArray(arg) && !(arg instanceof Error)
      );
      const errorArg = args.find((arg) => arg instanceof Error);
      const extraArgs = args.filter((arg) => arg !== objectData && arg !== errorArg);
      loggerVNext[level](message, {
        ...objectData ?? {},
        ...errorArg ? {
          error: {
            name: errorArg.name,
            message: errorArg.message,
            stack: errorArg.stack
          }
        } : {},
        ...extraArgs.length > 0 ? { args: extraArgs } : {}
      });
    } catch {
    }
  }
};

// src/logger/noop-logger.ts
var noopLogger = {
  debug: () => {
  },
  info: () => {
  },
  warn: () => {
  },
  error: () => {
  },
  cleanup: async () => {
  },
  getTransports: () => /* @__PURE__ */ new Map(),
  trackException: () => {
  },
  listLogs: async () => ({ logs: [], total: 0, page: 1, perPage: 100, hasMore: false }),
  listLogsByRunId: async () => ({ logs: [], total: 0, page: 1, perPage: 100, hasMore: false })
};
var LoggerTransport = class extends Transform {
  constructor(opts = {}) {
    super({ ...opts, objectMode: true });
  }
  async listLogsByRunId(_args) {
    return { logs: [], total: 0, page: _args?.page ?? 1, perPage: _args?.perPage ?? 100, hasMore: false };
  }
  async listLogs(_args) {
    return { logs: [], total: 0, page: _args?.page ?? 1, perPage: _args?.perPage ?? 100, hasMore: false };
  }
};
var createCustomTransport = (stream, listLogs, listLogsByRunId) => {
  let transport = stream;
  if (listLogs) {
    transport.listLogs = listLogs;
  }
  if (listLogsByRunId) {
    transport.listLogsByRunId = listLogsByRunId;
  }
  return transport;
};

export { DualLogger, LoggerTransport, MultiLogger, createCustomTransport, noopLogger };
//# sourceMappingURL=chunk-LCW65JSD.js.map
//# sourceMappingURL=chunk-LCW65JSD.js.map