/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module NativeApp
 */
import { BentleyError, Logger, LogLevel } from "@itwin/core-bentley";
import { IpcApp } from "./IpcApp";
/**
 * NativeAppLogger send log message from frontend to backend. It works on native app only.
 * @internal
 */
export class NativeAppLogger {
    static _messages = [];
    static _onFlushed;
    static flushToBackend() {
        if (!this._onFlushed && this._messages.length > 0) {
            this._onFlushed = new Promise(() => this._onFlushed = undefined);
            const messages = this._messages;
            this._messages = [];
            setTimeout(async () => this.flushBucket(messages));
        }
    }
    static async flushBucket(messages) {
        try {
            while (messages.length > 0) {
                // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
                const msg = messages.shift();
                await IpcApp.appFunctionIpc.log(msg.timestamp, msg.level, msg.category, msg.message, { ...msg.metaData });
            }
        }
        finally {
            // Put back unsent messages.
            this._messages.unshift(...messages);
            if (this._messages.length > 0) {
                this.flushToBackend();
            }
            else {
                // eslint-disable-next-line @typescript-eslint/no-floating-promises
                Promise.resolve(this._onFlushed);
            }
        }
    }
    static log(level, category, message, metaData) {
        this._messages.push({ timestamp: Date.now(), level, category, message, metaData: BentleyError.getMetaData(metaData) });
        this.flushToBackend();
    }
    static logError(category, message, metaData) {
        this.log(LogLevel.Error, category, message, metaData);
    }
    static logInfo(category, message, metaData) {
        this.log(LogLevel.Info, category, message, metaData);
    }
    static logTrace(category, message, metaData) {
        this.log(LogLevel.Trace, category, message, metaData);
    }
    static logWarning(category, message, metaData) {
        this.log(LogLevel.Warning, category, message, metaData);
    }
    static async flush() {
        this.flushToBackend();
        if (this._onFlushed) {
            return this._onFlushed;
        }
    }
    static initialize() {
        const errCb = (category, message, metaData) => this.logError(category, message, metaData);
        const warnCb = (category, message, metaData) => this.logWarning(category, message, metaData);
        const infoCb = (category, message, metaData) => this.logInfo(category, message, metaData);
        const traceCb = (category, message, metaData) => this.logTrace(category, message, metaData);
        Logger.initialize(errCb, warnCb, infoCb, traceCb);
    }
}
//# sourceMappingURL=NativeAppLogger.js.map