/* eslint-disable import/prefer-default-export */
import debug from 'debug';

export const createDebug = debug;

export const createLogger = (namespace: string) => {
  return new Proxy(window.console, {
    get(target, prop: keyof Console) {
      return (...args: any[]) => {
        try {
          if (['log', 'debug', 'info', 'warn', 'error'].includes(prop)) {
            // @ts-ignore
            return target[prop](namespace, ...args);
          }
          if (target[prop] instanceof Function) {
            // @ts-ignore
            return target[prop](...args);
          }
          return target[prop];
        } catch (err) {
          console.error(`Logger error in ${namespace}:`, err);
          return undefined;
        }
      };
    },
  });
};
