"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.rawProductsQuery = void 0;
async function rawProductsQuery(tx, params, bizplace) {
    let product = params.filters.find(data => data.name === 'product');
    let productDesc = params.filters.find(data => data.name === 'productDescription');
    let productQuery = '';
    if (product) {
        let productValue = product.value
            .toLowerCase()
            .split(',')
            .map(prod => {
            return "'%" + prod.trim().replace(/'/g, "''") + "%'";
        })
            .join(',');
        productQuery = `AND (
      Lower(prd.name) LIKE ANY(ARRAY[${productValue}])
      OR Lower(prd.sku) LIKE ANY(ARRAY[${productValue}])
      OR Lower(prd.description) LIKE ANY(ARRAY[${productValue}])
      OR Lower(prd.brand) LIKE ANY(ARRAY[${productValue}])
      OR Lower(prd.brand_sku) LIKE ANY(ARRAY[${productValue}])
    )`;
    }
    let productDescQuery = '';
    if (productDesc) {
        productDescQuery = "AND Lower(description) LIKE '%" + productDesc.value.toLowerCase() + "%'";
    }
    await tx.query(`
    create temp table temp_products on commit drop AS
    (
      select prd.id, prd.sku, prd.name, prd.description, prd.type, prd.id::varchar as product_id, prd.brand, prd.brand_sku 
      from products prd
      inner join bizplaces b on b.id = prd.bizplace_id 
      inner join companies c on c.domain_id = b.domain_id 
      inner join bizplaces b2 on b2.company_id = c.id
      where b2.id = $1
      ${productQuery}
      GROUP BY prd.id, prd.sku, prd.name, prd.description, prd.brand, prd.brand_sku
    )`, [bizplace.id]);
}
exports.rawProductsQuery = rawProductsQuery;
//# sourceMappingURL=report-query-util.js.map