function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
/**
 * 日志追踪 ID 生成器
 * 用于生成唯一的请求追踪 ID，便于日志关联和问题排查
 */ /**
 * 追踪 ID 生成器
 */ export class TraceIdGenerator {
    /**
   * 获取单例实例
   */ static getInstance(prefix) {
        if (!TraceIdGenerator.instance) {
            TraceIdGenerator.instance = new TraceIdGenerator(prefix);
        }
        return TraceIdGenerator.instance;
    }
    /**
   * 生成追踪 ID
   */ generate() {
        const timestamp = Date.now();
        const random = Math.random().toString(36).substring(2, 9);
        const counter = (this.counter++).toString(36).padStart(4, '0');
        const parts = [
            timestamp.toString(36),
            random,
            counter
        ];
        if (this.prefix) {
            parts.unshift(this.prefix);
        }
        return parts.join('-');
    }
    /**
   * 从字符串解析追踪 ID（用于验证）
   */ static parse(traceId) {
        const parts = traceId.split('-');
        if (parts.length < 3) {
            return null;
        }
        try {
            const timestamp = parseInt(parts[parts.length - 3], 36);
            const random = parts[parts.length - 2];
            const counter = parseInt(parts[parts.length - 1], 36);
            return {
                prefix: parts.length > 3 ? parts.slice(0, -3).join('-') : undefined,
                timestamp,
                random,
                counter
            };
        } catch  {
            return null;
        }
    }
    constructor(prefix = ''){
        _define_property(this, "counter", 0);
        _define_property(this, "prefix", void 0);
        this.prefix = prefix;
    }
}
_define_property(TraceIdGenerator, "instance", null);
/**
 * 全局追踪 ID 生成器实例
 */ let globalTraceIdGenerator = null;
/**
 * 初始化追踪 ID 生成器
 */ export function initTraceIdGenerator(prefix) {
    globalTraceIdGenerator = TraceIdGenerator.getInstance(prefix);
    return globalTraceIdGenerator;
}
/**
 * 获取全局追踪 ID 生成器
 */ export function getTraceIdGenerator() {
    if (!globalTraceIdGenerator) {
        globalTraceIdGenerator = TraceIdGenerator.getInstance();
    }
    return globalTraceIdGenerator;
}
/**
 * 生成追踪 ID
 */ export function generateTraceId() {
    return getTraceIdGenerator().generate();
}
/**
 * 解析追踪 ID
 */ export function parseTraceId(traceId) {
    return TraceIdGenerator.parse(traceId);
}
/**
 * 当前请求的追踪 ID（使用 AsyncLocalStorage 或全局变量）
 */ let currentTraceId = null;
/**
 * 设置当前请求的追踪 ID
 */ export function setCurrentTraceId(traceId) {
    currentTraceId = traceId;
}
/**
 * 获取当前请求的追踪 ID
 */ export function getCurrentTraceId() {
    return currentTraceId;
}
/**
 * 清除当前请求的追踪 ID
 */ export function clearCurrentTraceId() {
    currentTraceId = null;
}

//# sourceMappingURL=traceId.js.map