{"size": 2545, "ext": "js", "max_stars_repo_path": "src/api-extension/models/app-builder.js", "max_stars_repo_name": "talon-one/commercetools-talonone-connector", "max_stars_repo_stars_event_min_datetime": "2021-09-28T10:24:49.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-28T10:24:49.000Z", "max_issues_repo_path": "src/api-extension/models/app-builder.js", "max_issues_repo_name": "talon-one/commercetools-talonone-connector", "max_issues_repo_issues_event_min_datetime": "2021-08-25T10:24:21.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-17T16:17:57.000Z", "max_forks_repo_path": "src/api-extension/models/app-builder.js", "max_forks_repo_name": "talon-one/commercetools-talonone-connector", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 18.7132352941, "max_line_length": 88, "alphanum_fraction": 0.6220039293}
'use strict';
const middy = require('@middy/core');
const timePlugin = require('../plugins/time');
const { CloudProvider } = require('./cloud-provider');
const { googleMiddy } = require('./google-middy');

class AppBuilder {
  constructor(handler, logger, provider, username, password) {
    switch (provider) {
      case CloudProvider.AWS:
        this.handler = middy(handler, timePlugin({ logger: logger.info.bind(logger) }));
        break;
      case CloudProvider.GCP:
        this.handler = googleMiddy(handler, username, password);
        break;
      default:
        throw new Error('Invalid provider.');
    }

    this.provider = provider;
    this.config = {};
    this.setLogger(logger);
  }

  /**
   * @param logger
   * @returns {AppBuilder}
   */
  setLogger(logger) {
    this.config.logger = logger;

    return this;
  }

  /**
   * @param mappings
   * @returns {AppBuilder}
   */
  setAttributeMappings(mappings) {
    this.config.attributeMappings = mappings;

    return this;
  }

  /**
   * @param {string} spec
   * @returns {AppBuilder}
   */
  setCartAttributeMapping(spec) {
    this.config.cartAttributeMapping = spec;

    return this;
  }

  /**
   * @param {string} spec
   * @returns {AppBuilder}
   */
  setCartItemAttributeMapping(spec) {
    this.config.cartItemAttributeMapping = spec;

    return this;
  }

  /**
   * @param {number} value
   * @returns {AppBuilder}
   */
  setEventValidationMode(value) {
    this.config.eventValidationMode = value;

    return this;
  }

  /**
   * @param {MapperSettings} settings
   * @returns {AppBuilder}
   */
  setMapperSettings(settings) {
    this.config.mapperSettings = settings;

    return this;
  }

  /**
   * @param apiClient
   * @returns {AppBuilder}
   */
  setApiClient(apiClient) {
    this.config.apiClient = apiClient;

    return this;
  }

  /**
   * @param apiClient
   * @returns {AppBuilder}
   */
  setCtpApiClient(apiClient) {
    this.config.ctpApiClient = apiClient;

    return this;
  }

  /**
   * @param broadcastApiClient
   * @returns {AppBuilder}
   */
  setBroadcastApiClient(broadcastApiClient) {
    this.config.broadcastApiClient = broadcastApiClient;

    return this;
  }

  /**
   * @param middleware
   * @returns {AppBuilder}
   */
  use(middleware) {
    this.handler.use(middleware(this.config));

    return this;
  }

  build() {
    switch (this.provider) {
      case CloudProvider.GCP:
        return this.handler.main;
      default:
        return this.handler;
    }
  }
}

module.exports = { AppBuilder };
