/**
Copyright: 2024 Contrast Security, Inc
  Contact: support@contrastsecurity.com
  License: Commercial

  NOTICE: This Software and the patented inventions embodied within may only be
  used as part of Contrast Security’s commercial offerings. Even though it is
  made available through public repositories, use of this Software is subject to
  the applicable End User Licensing Agreement found at
  https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
  between Contrast Security and the End User. The Software may not be reverse
  engineered, modified, repackaged, sold, redistributed or otherwise used in a
  way not consistent with the End User License Agreement.
*/
'use strict';

const labels = {
  language: require('./labels').language,
  state: require('./labels').stateTransition
};

/**
 * Returns a SQLScanner of the specified driver type. Supported drivers can be added as needed.
 * @param  {String}                      driver The SQL driver name.
 * @throws {DriverNotSupportedException}        When the scanner for the requested driver type
 *                                              is not supported/implemented.
 */
module.exports = new (class SqlScannerFactory {
  // Initializes with supported driver types.
  constructor() {
    // Update as needed to support more databases
    this.supportedDrivers = {
      MySQL: require('./mysql-scanner')
    };

    // Throw this when we don't recognize driver
    this.DriverNotSupportedException = function(driver) {
      return new Error(
        `There is no supported driver for ${driver} at this time.`
      );
    };
  }

  /**
   * Factory method that returns scanners by driver type.
   * @param   {String}     driver  The name of the driver
   * @param   {Object}     options Constructor options
   * @returns {SQLScanner}
   */
  create(driver, options) {
    options = options || {};

    options.labels = options.includeLabels ? labels : null;

    if (driver in this.supportedDrivers) {
      return new this.supportedDrivers[driver](options);
    }

    throw this.DriverNotSupportedException(driver);
  }
})();
