const records = {};
let isEnabled = false;
const isTimingsEnabled = () => isEnabled;
export const forceEnable = () => {
  isEnabled = true;
};

// Debug: uncomment to enable
// forceEnable();

export const start = (key) => {
  // we want to strip all the code out for production builds
  // draw back: can only do timings in dev env (which seems to be fine for now)
  if (import.meta.env.DEV) {
    if (!isTimingsEnabled()) {
      return;
    }
    const now = performance.now();
    records[key] = now;
  }
};
export const finish = (key) => {
  if (import.meta.env.DEV) {
    if (!isTimingsEnabled()) {
      return;
    }
    const now = performance.now();
    const previous = records[key];
    if (!previous) {
       
      console.warn("cannot finish timing as no previous time found", key);
      return;
    }
    const result = now - previous;
    const rounded = result.toFixed(2);
    const style = (() => {
      if (result < 12) {
        return {
          textColor: "green",
          symbol: "✅"
        };
      }
      if (result < 40) {
        return {
          textColor: "orange",
          symbol: "⚠️"
        };
      }
      return {
        textColor: "red",
        symbol: "❌"
      };
    })();

    console.log(
      `${style.symbol} %cTiming %c${rounded} %cms %c${key}`,
      // title
      "color: blue; font-weight: bold;",
      // result
      `color: ${style.textColor}; font-size: 1.1em;`,
      // ms
      "color: grey;",
      // key
      "color: purple; font-weight: bold;"
    );
  }
};
