import a1 from "./j";
import Event, { EventType } from "./c/Event";
import b1 from "./b/k";
import m from "./g/h";
import c1 from "./g/l";
import { d1 } from "./b/m";
import e1 from "./g/n";
import UserInfo from "./d/UserInfo";
import DeviceInfo from "./d/DeviceInfo";
import f1 from "./b/o";
import AppInfo from "./d/p";
class g1 {
    constructor() {
        this.properties = {};
    }
}
export default class AnalyticsInstanceImpl {
    constructor(config, context) {
        this.h1 = new c1();
        this.i1 = new g1();
        this.j1 = new e1();
        this.user = new UserInfo();
        this.appInfo = new AppInfo();
        this.l1 = true;
        this.m1 = new Map();
        this.n1 = new Map();
        this.o1 = new f1();
        this.app_id = config.app_id;
        this.sub_app_id = config.sub_app_id;
        if (this.sub_app_id.length == 0) {
            this.sub_app_id = this.app_id;
        }
        this.identifier = config.identifier;
        this.useTraditionalPageViewMode = config.useTraditionalPageViewMode;
        this.context = context;
        this.q1 = new a1();
        this.deviceInfo = new DeviceInfo(context);
        this.s1 = new b1(this.deviceInfo, this.appInfo);
        d1.t1(config.identifier, config.mainUrl, config.backupUrl);
        this.o1.on('networkStateChanged', (w3) => {
            m.i(`Network state changed to: ${w3}`);
        });
    }
    track(s3, t3, u3) {
        return this.trackDetailed(s3, EventType.Custom, t3, u3);
    }
    trackDetailed(l3, type = EventType.Custom, m3, n3, p3, q3, r3) {
        return this.u1(l3, type, m3, n3, p3, q3, r3);
    }
    u1(a3, type = EventType.Custom, b3, c3, d3, e3, f3, g3) {
        m.i(`tracking ${a3} with value: '${b3}' and properties: ${JSON.stringify(c3)}`);
        {
            if (!this.v1()) {
                m.i("网络错误，请检查网络设置或allowCellularFlush设置");
                return new Error("网络错误，请检查网络设置或allowCellularFlush设置");
            }
        }
        const event = new Event(a3, b3, c3, type);
        if (d3 !== undefined) {
            m.i("runtimeConfig: " + JSON.stringify(d3));
            event.setProps(d3.toRecord());
        }
        if (e3 !== undefined) {
            m.i("networkConfig: " + JSON.stringify(e3));
            event.setProps(e3.toRecord());
        }
        if (f3 !== undefined) {
            m.i(`locationConfig: ${f3}`);
            event.setProps(f3.toRecord());
        }
        this.w1(event);
        if (g3 !== undefined) {
            event.setProps(g3);
        }
        this.h1.z1(() => new Promise((j3) => {
            if (this.m1.has(a3)) {
                const k3 = this.m1.get(a3);
                this.a2(a3);
                if (k3 !== undefined) {
                    event.event_time = event.time_stamp - k3;
                    m.i("event_time: " + event.event_time);
                }
            }
            this.b2(event, "/wa-codcs/event-data-collect");
            j3();
        }));
        return null;
    }
    trackLog(z2) {
        m.i("tracking logModel: " + JSON.stringify(z2));
        if (!this.v1()) {
            m.i("网络错误，请检查网络设置或allowCellularFlush设置");
            return new Error("网络错误，请检查网络设置或allowCellularFlush设置");
        }
        this.w1(z2);
        this.b2(z2, "/wa-codcs/verify-data-collect");
        return null;
    }
    trackError(w2) {
        m.i("tracking errorModel: " + JSON.stringify(w2));
        if (!this.v1()) {
            m.i("网络错误，请检查网络设置或allowCellularFlush设置");
            return new Error("网络错误，请检查网络设置或allowCellularFlush设置");
        }
        this.w1(w2);
        this.b2(w2, "/wa-codcs/verify-data-collect");
        return null;
    }
    v1() {
        if (!this.o1.c2()) {
            return false;
        }
        if (!this.l1 && this.o1.d2()) {
            return false;
        }
        return true;
    }
    w1(model) {
        let u2 = {};
        this.j1.read(() => {
            u2 = this.i1.properties;
        });
        model.setProps(u2);
        model.setProps(this.user.toRecord());
        model.setProps(this.e2?.toRecord() ?? {});
        model.setProp("network_type", this.o1.f2);
    }
    b2(model, q2) {
        this.h1.z1(() => new Promise((t2) => {
            model.trace_id = this.q1.g2();
            model.growth_id = this.q1.h2();
            this.s1.i2(this.app_id, this.sub_app_id, this.identifier, [model], q2);
            t2();
        }));
    }
    time(event) {
        if (event.trim() === "") {
            m.e(`time key is empty`);
            return;
        }
        const startTime = new Date().getTime();
        m.i('time event "' + event + '" begin');
        this.h1.z1(() => new Promise((o2) => {
            this.m1.set(event, startTime);
            o2();
        }));
    }
    eventElapsedTime(event) {
        const l2 = this.m1.get(event);
        if (l2 !== undefined) {
            return new Date().getTime() - l2;
        }
        return undefined;
    }
    a2(event) {
        this.m1.delete(event);
    }
    clearTimedEvent(event) {
        this.h1.z1(() => new Promise((k2) => {
            this.a2(event);
            k2();
        }));
    }
    clearAllTimedEvent() {
        this.h1.z1(() => new Promise((h2) => {
            this.m1.clear();
            h2();
        }));
    }
    trackPageViewBegin(pageId, z1, a2) {
        if (pageId.trim() === "") {
            m.e(`pageId key is empty`);
            return;
        }
        const startTime = new Date().getTime();
        m.i('PageView "' + pageId + '" begin');
        if (this.useTraditionalPageViewMode) {
        }
        else {
            let e2 = {
                "page_id": pageId
            };
            if (z1 !== undefined) {
                e2.j2 = z1;
            }
            if (a2 !== undefined) {
                e2.l2 = a2;
            }
            this.u1(EventType.Expo, EventType.Expo, undefined, undefined, undefined, undefined, undefined, e2);
        }
        this.h1.z1(() => new Promise((d2) => {
            this.n1.set(pageId, startTime);
            d2();
        }));
    }
    trackPageViewEnd(pageId, o1, p1) {
        if (pageId.trim() === "") {
            m.e(`pageId key is empty`);
            return;
        }
        this.h1.z1(() => new Promise((t1) => {
            if (this.n1.has(pageId)) {
                const u1 = this.n1.get(pageId);
                this.n1.delete(pageId);
                let v1 = {
                    "page_id": pageId
                };
                if (o1 !== undefined) {
                    v1.j2 = o1;
                }
                if (p1 !== undefined) {
                    v1.l2 = p1;
                }
                if (u1 !== undefined) {
                    const w1 = new Date().getTime() - u1;
                    v1["event_time"] = w1;
                    m.i("event_time: " + w1);
                }
                else {
                    m.e("event_time undefined");
                }
                if (this.useTraditionalPageViewMode) {
                    this.u1(EventType.Expo, EventType.Expo, undefined, undefined, undefined, undefined, undefined, v1);
                }
                else {
                    this.u1(EventType.PageChg, EventType.PageChg, undefined, undefined, undefined, undefined, undefined, v1);
                }
            }
            else {
                m.e(`pageId:${pageId}并未开始计时，无法结束`);
            }
            t1();
        }));
    }
    setReservedField(j1) {
        const k1 = {};
        Object.keys(j1).forEach((key) => {
            if (key.startsWith("field_y_") || key.startsWith("sys_y_")) {
                k1[key] = j1[key];
            }
            else {
                m.e("仅field_y_*/sys_y_*有效，其他字段设置无效。无效 key: " + key);
            }
        });
        this.j1.write(() => {
            Object.keys(k1).forEach((key) => {
                this.i1.properties[key] = k1[key];
            });
        });
    }
    setBusinessInfo(h1) {
        this.j1.write(() => {
            this.e2 = h1;
        });
    }
}
