// Copyright 2017 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { ipcRenderer as ipc } from 'electron';
import * as path from 'node:path';

import {
  initLogger,
  LogLevel as SignalClientLogLevel,
} from '@signalapp/libsignal-client';

import { setPinoDestination, log } from './log.std.ts';
import * as Errors from '../types/errors.std.ts';
import { createRotatingPinoDest } from '../util/rotatingPinoDest.node.ts';
import { redactAll } from '../util/privacy.node.ts';

let isInitialized = false;
let shouldRestart = false;

export function beforeRestart(): void {
  shouldRestart = true;
}

export function initialize(): void {
  if (isInitialized) {
    throw new Error('Already initialized');
  }
  isInitialized = true;

  const basePath = ipc.sendSync('get-user-data-path');
  const logFile = path.join(basePath, 'logs', 'app.log');

  const onClose = () => {
    if (shouldRestart) {
      initialize();
    }
  };

  const stream = createRotatingPinoDest({
    logFile,
  });

  stream.on('close', onClose);
  stream.on('error', onClose);

  setPinoDestination(stream, redactAll);
}

window.onerror = (message, source, line, column, error) => {
  const errorInfo = Errors.toLogFormat(error);
  log.error(
    // oxlint-disable-next-line typescript/no-base-to-string, typescript/restrict-template-expressions
    `Top-level unhandled error: ${message}, ${errorInfo}`,
    Errors.toLocation(source, line, column)
  );
};

window.addEventListener('unhandledrejection', rejectionEvent => {
  const error = rejectionEvent.reason;
  const errorString = Errors.toLogFormat(error);
  log.error(`Top-level unhandled promise rejection: ${errorString}`);
});

const libSignalLog = log.child('@signalapp/libsignal-client');

initLogger(
  SignalClientLogLevel.Info,
  (
    level: unknown,
    target: string,
    file: string | null,
    line: number | null,
    message: string
  ) => {
    let fileString = '';
    if (file && line) {
      fileString = ` ${file}:${line}`;
    } else if (file) {
      fileString = ` ${file}`;
    }
    const logString = `${message} ${target}${fileString}`;

    if (level === SignalClientLogLevel.Trace) {
      libSignalLog.trace(logString);
    } else if (level === SignalClientLogLevel.Debug) {
      libSignalLog.debug(logString);
    } else if (level === SignalClientLogLevel.Info) {
      libSignalLog.info(logString);
    } else if (level === SignalClientLogLevel.Warn) {
      libSignalLog.warn(logString);
    } else if (level === SignalClientLogLevel.Error) {
      libSignalLog.error(logString);
    } else {
      // oxlint-disable-next-line typescript/restrict-template-expressions
      libSignalLog.error(`${logString} (unknown log level ${level})`);
    }
  }
);
