import { consola, createConsola } from 'consola';

declare const clarityLog: unknown;
declare function determineLogLevel(): number;
declare function formatMessage(...args: any[]): string;
declare function handleStyledOutput(message: string, logger: any, method: string, options?: LogOptions): void;
export declare interface Log {
  info: (...args: any[]) => void
  success: (msg: string) => void
  error: (err: string | Error | object | unknown, options?: ErrorOptions) => void
  warn: (arg: string) => void
  warning: (arg: string) => void
  debug: (...args: any[]) => void
  dump: (...args: any[]) => void
  dd: (...args: any[]) => void
  echo: (...args: any[]) => void
}
export declare interface LogOptions {
  styled?: boolean
}
export declare type ErrorMessage = string
export type ErrorOptions = {
  shouldExit: boolean
  silent?: boolean
  message?: ErrorMessage
} | any | Error

export const log: Log = {
  info: async (message: string, options?: LogOptions) => {
    handleStyledOutput(message, consolaLogger, 'info', options)
    clarityLog.info(message)
  },

  success: async (message: string, options?: LogOptions) => {
    handleStyledOutput(message, consolaLogger, 'success', options)
    clarityLog.info(`SUCCESS: ${message}`)
  },

  warn: async (message: string, options?: LogOptions) => {
    handleStyledOutput(message, consolaLogger, 'warn', options)
    clarityLog.warn(message)
  },

  warning: async (message: string, options?: LogOptions) => {
    handleStyledOutput(message, consolaLogger, 'warn', options)
    clarityLog.warn(message)
  },

  error: async (err: string | Error | object | unknown, options?: ErrorOptions) => {
    const errorMessage = typeof err === 'string'
      ? err
      : err instanceof Error
        ? err.message
        : JSON.stringify(err)

    handleStyledOutput(errorMessage, consolaLogger, 'error', options)
    clarityLog.error(errorMessage)
    handleError(err, options)
  },

  debug: async (...args: any[]) => {
    const message = `${formatMessage(...args)}`

    if (process.env.APP_ENV === 'production' || process.env.APP_ENV === 'prod') {
      clarityLog.debug(message)
      return writeToLogFile(message)
    }

    consolaLogger.debug(`DEBUG: ${message}`)
    clarityLog.debug(message)
  },

  dump: (...args: any[]) => {
    const message = formatMessage(...args)
    console.log(message)
    clarityLog.debug(`DUMP: ${message}`)
  },

  dd: (...args: any[]) => {
    const message = formatMessage(...args)
    console.log(message)
    consolaLogger.error(message)
    clarityLog.error(message)
    process.exit(ExitCode.FatalError)
  },

  echo: (...args: any[]) => {
    const message = formatMessage(...args)
    console.log(message)
    clarityLog.info(`ECHO: ${message}`)
  },
}
export declare function dump(...args: any[]): void;
export declare function dd(...args: any[]): void;
export declare function echo(...args: any[]): void;

export { consola, createConsola }