import { QUEUES_INIT } from "../util/config";
import { CommonQueueManagement } from "./common";
import { Consumers } from "../types";

// ingest the global config and come up with a list of queue names to create
const INITS = Object.entries(QUEUES_INIT).reduce(
  (prev: string[], [name, config]) => {
    if (config.integrationAPI.init) {
      return [...prev, name];
    } else {
      return prev;
    }
  },
  [],
);

/**
 * This is the instance for most integation APIs. This instance has access to almost all queues so that the Lexamica API can assign, manage, and process jobs.
 */
export class IntegrationAPI extends CommonQueueManagement {
  /**
   * Creates an instance of the Integration API message queue manager
   * @param {string} integration_platform should be the lowercase id of the integration api. Example: litify, filevine, etc.
   * @param {string[]} initQueues a optional list of arrays to populate instead of the defaults. Do not specify this unless you know what you are doing.
   * @param {string} connection_url an optional remote Redis connection URL to use. If not provided, this instance will use a local Redis instance.
   */
  constructor(
    integration_platform: string,
    initQueues?: string[],
    connection_url?: string,
  ) {
    // add a sync queue for each integration
    const queues = [...(initQueues ?? INITS), `sync_${integration_platform}`];
    super(queues, Consumers.INTEGRATION_API, connection_url);
  }
}
