import { log } from "./logging.js";

/* I can't believe I have to write this just so I can use the assert that should be standard in every javascript. */

export class AssertionError extends Error {
  /* This is from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
   * which claims you have to do this weird stuff to make your error actually work.
   */

  constructor(foo = 'bar', ...params) {
    // Pass remaining arguments (including vendor specific ones) to parent constructor
    super(...params);

    // Maintains proper stack trace for where our error was thrown (only available on V8)
    if (Error.captureStackTrace) {
      // this "only on V8" is managed by the Error.captureStackTrace test above
      Error.captureStackTrace(this, AssertionError);
    }

    this.name = 'AssertionError';
    // Custom debugging information
    this.foo = foo;
    this.date = new Date();
  }
}

const assert = (test, message) => {
  log.assert(test, message);

  if(!test) {
    throw new AssertionError(message);
  }
}

export default assert;
