"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get AnalyticsService () {
        return AnalyticsService;
    },
    get getAnalytics () {
        return getAnalytics;
    },
    get initAnalytics () {
        return initAnalytics;
    }
});
const _logger = require("./logger");
const _security = require("./security");
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;
}
let AnalyticsService = class AnalyticsService {
    /**
   * 启动批量上报定时器
   */ startBatchTimer() {
        if (this.batchTimer) {
            return;
        }
        this.batchTimer = setInterval(()=>{
            this.flush();
        }, this.config.batchInterval);
    }
    /**
   * 停止批量上报定时器
   */ stopBatchTimer() {
        if (this.batchTimer) {
            clearInterval(this.batchTimer);
            this.batchTimer = null;
        }
    }
    /**
   * 记录事件
   */ track(event) {
        if (!this.config.enabled) {
            return;
        }
        // 采样检查
        if (Math.random() > this.config.sampleRate) {
            return;
        }
        // 匿名化处理
        const anonymizedEvent = {
            ...event,
            timestamp: Date.now(),
            properties: this.anonymizeProperties(event.properties)
        };
        // 添加到队列
        this.eventQueue.push(anonymizedEvent);
        // 立即上报单个事件
        if (this.config.onEvent) {
            this.config.onEvent(anonymizedEvent);
        }
        // 检查是否需要批量上报
        if (this.eventQueue.length >= this.config.batchSize) {
            this.flush();
        }
    }
    /**
   * 记录页面浏览
   */ trackPageView(page, properties) {
        this.track({
            type: 'pageview',
            name: 'page_view',
            properties: {
                page,
                ...properties
            }
        });
    }
    /**
   * 记录点击事件
   */ trackClick(element, properties) {
        this.track({
            type: 'click',
            name: 'click',
            properties: {
                element,
                ...properties
            }
        });
    }
    /**
   * 记录自定义事件
   */ trackCustom(name, properties) {
        this.track({
            type: 'custom',
            name,
            properties
        });
    }
    /**
   * 记录错误事件
   */ trackError(error, properties) {
        this.track({
            type: 'error',
            name: 'error',
            properties: {
                errorName: error.name,
                errorMessage: error.message,
                errorStack: error.stack,
                ...properties
            }
        });
    }
    /**
   * 记录性能事件
   */ trackPerformance(metrics, properties) {
        this.track({
            type: 'performance',
            name: 'performance',
            properties: {
                ...metrics,
                ...properties
            }
        });
    }
    /**
   * 匿名化属性（使用深度脱敏）
   */ anonymizeProperties(properties) {
        if (!this.config.anonymize || !properties) {
            return properties;
        }
        // 使用深度脱敏处理
        return _security.SecurityUtils.deepSanitize(properties);
    }
    /**
   * 刷新队列（批量上报）
   */ flush() {
        if (this.eventQueue.length === 0) {
            return;
        }
        const events = [
            ...this.eventQueue
        ];
        this.eventQueue = [];
        if (this.config.onBatch) {
            this.config.onBatch(events);
        }
        _logger.logger.debug(`分析服务批量上报 ${events.length} 个事件`);
    }
    /**
   * 启用分析
   */ enable() {
        this.config.enabled = true;
        this.startBatchTimer();
    }
    /**
   * 禁用分析
   */ disable() {
        this.config.enabled = false;
        this.stopBatchTimer();
        this.flush();
    }
    /**
   * 销毁服务
   */ destroy() {
        this.stopBatchTimer();
        this.flush();
    }
    constructor(config = {}){
        _define_property(this, "config", void 0);
        _define_property(this, "eventQueue", []);
        _define_property(this, "batchTimer", null);
        this.config = {
            enabled: config.enabled ?? true,
            sampleRate: config.sampleRate ?? 1.0,
            anonymize: config.anonymize ?? true,
            onEvent: config.onEvent ?? (()=>{}),
            onBatch: config.onBatch ?? (()=>{}),
            batchSize: config.batchSize ?? 10,
            batchInterval: config.batchInterval ?? 5000
        };
        if (this.config.enabled) {
            this.startBatchTimer();
        }
    }
};
/**
 * 默认分析服务实例
 */ let defaultAnalyticsService = null;
function initAnalytics(config) {
    if (!defaultAnalyticsService) {
        defaultAnalyticsService = new AnalyticsService(config);
    }
    return defaultAnalyticsService;
}
function getAnalytics() {
    if (!defaultAnalyticsService) {
        return initAnalytics();
    }
    return defaultAnalyticsService;
}

//# sourceMappingURL=analytics.js.map