import { E as Env } from './index-BNpVu1WU.js';

// logger.ts
//
// PROJECT STANDARD — console.warn / console.* in shipped code
// ─────────────────────────────────────────────────────────────
// Do NOT use bare console.warn/console.debug/console.info in utility or component
// code that ships to production.  Use the Logger class instead:
//
//   import { Logger, LogLevel } from "../logger"
//   const logger = new Logger("MyModule", { logLevel: LogLevel.Error })
//   logger.warn("something unexpected:", detail)
//
// Default logLevel is LogLevel.Error, so debug/info/warn output is suppressed in
// production bundles.  Set Env.LOG_LEVEL="warn" (or "debug"/"info") to surface
// lower-severity messages during development or investigation.
//
// Exceptions (bare console.* is acceptable):
//   • Inside Logger itself (to avoid circular calls)
//   • One-time startup errors that must always be visible (e.g. misconfigured Env)
//
var LogLevel;
(function (LogLevel) {
    LogLevel[LogLevel["Debug"] = 1] = "Debug";
    LogLevel[LogLevel["Info"] = 2] = "Info";
    LogLevel[LogLevel["Warn"] = 3] = "Warn";
    LogLevel[LogLevel["Error"] = 4] = "Error";
})(LogLevel || (LogLevel = {}));
function getLogLevelFromString(level) {
    switch (level.toLowerCase()) {
        case "info":
            return LogLevel.Info;
        case "warn":
            return LogLevel.Warn;
        case "error":
            return LogLevel.Error;
        default:
            console.warn(`Invalid log level string: ${level}, defaulting to Error`);
            return LogLevel.Error;
    }
}
class Logger {
    constructor(componentName, options = {}) {
        this.componentName = componentName || "UnknownComponent";
        this.logLevel = options.logLevel || getLogLevelFromString(Env.LOG_LEVEL) || LogLevel.Error;
        this.remoteLoggingUrl = options.remoteLoggingUrl;
    }
    setLogLevel(level) {
        if (level in LogLevel) {
            this.logLevel = level;
        }
        else {
            console.warn(`Invalid log level: ${level}`);
        }
    }
    log(level, message, ...args) {
        if (level >= this.logLevel) {
            const timestamp = new Date().toISOString();
            const levelName = LogLevel[level].toUpperCase();
            const formattedMessage = `[${timestamp}] [${this.componentName}] [${levelName}] ${message}`;
            switch (level) {
                case LogLevel.Debug:
                    console.debug(formattedMessage, ...args);
                    break;
                case LogLevel.Info:
                    console.info(formattedMessage, ...args);
                    break;
                case LogLevel.Warn:
                    console.warn(formattedMessage, ...args);
                    break;
                case LogLevel.Error:
                    console.error(formattedMessage, ...args);
                    break;
            }
            if (this.remoteLoggingUrl) {
                this.sendToServer(levelName, message, args);
            }
        }
    }
    sendToServer(level, message, args) {
        const logEntry = {
            timestamp: new Date().toISOString(),
            componentName: this.componentName,
            level: level,
            message: message,
            args: args,
        };
        fetch(this.remoteLoggingUrl, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(logEntry),
        }).catch(error => {
            console.warn("Failed to send log to server:", error);
        });
    }
    debug(message, ...args) {
        this.log(LogLevel.Debug, message, ...args);
    }
    info(message, ...args) {
        this.log(LogLevel.Info, message, ...args);
    }
    warn(message, ...args) {
        this.log(LogLevel.Warn, message, ...args);
    }
    error(message, errorObj) {
        if (errorObj instanceof Error) {
            this.log(LogLevel.Error, `${message}: ${errorObj.message}`, errorObj.stack);
        }
        else {
            this.log(LogLevel.Error, message, errorObj);
        }
    }
}

export { Logger as L, LogLevel as a };
