import { log_levels } from "./config.js";
const disabled = () => {};

const bind_logging = () => {
  // We have to do this because rendered pages will include
  // client components but they don't have a window variable.
  // We try to use window to bind, and if it doesn't work then
  // just use the base function.  One question? Why bind window?
  try {
    return {
      debug: console.log.bind(window.console),
      warn: console.warn.bind(window.console),
      info: console.info.bind(window.console),
      error: console.error.bind(window.console),
      table: console.table.bind(window.console),
      assert: console.assert.bind(window.console),
    }
  } catch(error) {
    return {
      debug: console.log,
      warn: console.warn,
      info: console.info,
      error: console.error,
      table: console.table,
      assert: console.assert
    }
  }
}

const all_levels = bind_logging();

const as_entries = Object.entries(all_levels);

export const logger = (levels = log_levels) => {
  return Object.fromEntries(as_entries.map(([level, logf]) => {
    return levels.includes(level) ? [level, logf] : [level, disabled];
  }));
}

// this is a default logger that's configured with what's in config.js:log_levels
export const log = logger();
