'use strict';

const products = require('./extractors/products.extractor');
const productPrices = require('./extractors/product-prices.extractor');
const supplies = require('./extractors/supplies.extractor');
const warehouses = require('./extractors/warehouses.extractor');
const inventoryMovements = require('./extractors/inventory-movements.extractor');
const suppliers = require('./extractors/suppliers.extractor');
const purchaseOrders = require('./extractors/purchase-orders.extractor');
const purchases = require('./extractors/purchases.extractor');
const transfers = require('./extractors/transfers.extractor');
const recipes = require('./extractors/recipes.extractor');
const users = require('./extractors/users.extractor');
const waiters = require('./extractors/waiters.extractor');
const permissions = require('./extractors/permissions.extractor');
const productNormalizer = require('./normalizers/product.normalizer');
const productPriceNormalizer = require('./normalizers/productPrice.normalizer');
const supplyNormalizer = require('./normalizers/supply.normalizer');
const warehouseNormalizer = require('./normalizers/warehouse.normalizer');
const inventoryMovementNormalizer = require('./normalizers/inventoryMovement.normalizer');
const supplierNormalizer = require('./normalizers/supplier.normalizer');
const purchaseOrderNormalizer = require('./normalizers/purchaseOrder.normalizer');
const purchaseNormalizer = require('./normalizers/purchase.normalizer');
const transferNormalizer = require('./normalizers/transfer.normalizer');
const recipeNormalizer = require('./normalizers/recipe.normalizer');
const operatorUserNormalizer = require('./normalizers/operatorUser.normalizer');
const operatorRoleNormalizer = require('./normalizers/operatorRole.normalizer');
const operatorPermissionNormalizer = require('./normalizers/operatorPermission.normalizer');
const eventBuilders = require('./event-builders');
const writers = require('./writers');

const pipelines = [
  { extractor: products, normalize: productNormalizer },
  { extractor: productPrices, normalize: productPriceNormalizer },
  { extractor: supplies, normalize: supplyNormalizer },
  { extractor: warehouses, normalize: warehouseNormalizer },
  { extractor: inventoryMovements, normalize: inventoryMovementNormalizer },
  { extractor: suppliers, normalize: supplierNormalizer },
  { extractor: purchaseOrders, normalize: purchaseOrderNormalizer },
  { extractor: purchases, normalize: purchaseNormalizer },
  { extractor: transfers, normalize: transferNormalizer },
  { extractor: recipes, normalize: recipeNormalizer },
  { extractor: users, normalize: operatorUserNormalizer },
  { extractor: waiters, normalize: operatorUserNormalizer },
  { extractor: permissions.roles, normalize: operatorRoleNormalizer },
  { extractor: permissions.permissions, normalize: operatorPermissionNormalizer },
  { extractor: permissions.catalogPermissions, normalize: operatorPermissionNormalizer },
];

async function extractCanonicalEvents(pool, context, cursors = {}, limit = 100) {
  const output = [];
  for (const pipeline of pipelines) {
    const pipelineCursors = Object.fromEntries(pipeline.extractor.sourceTables.map((table) => [table, cursors[table]]));
    const cursor = pipeline.extractor.sourceTables.length === 1 ? pipelineCursors[pipeline.extractor.sourceTables[0]] : pipelineCursors;
    const rows = await pipeline.extractor.extract(pool, cursor, limit);
    for (const row of rows) output.push(eventBuilders.buildForNormalized(pipeline.normalize(row), context));
  }
  return output;
}

module.exports = { pipelines, extractCanonicalEvents, eventBuilders, writers };
