// @ts-nocheck
const allowedOperation = "catalog-maker-refresh-manufacturer";

export function assertAllowedWriteSql(sql, { operation } = {}) {
  if (operation !== allowedOperation) {
    throw new Error(`Write SQL blocked. Unsupported operation: ${operation || "none"}`);
  }

  const compact = compactSql(sql);
  const allowed = [
    /^updateps_product_catalogpcsetpc\.skladovka=0,pc\.catalog_9b=0wherepc\.id_manufacturer=\d+$/,
    /^updateps_product_catalogpcjoinps_import_suppliers_dumpisdonisd\.ean=pc\.ean_13setpc\.skladovka=1wherepc\.id_manufacturer=\d+$/,
    /^updateps_product_catalogpcsetpc\.catalog_9b=1wherepc\.id_manufacturer=\d+andpc\.ean_13in\(selectpa\.ean13fromps_product_attributepajoinps_productponp\.id_product=pa\.id_productwherep\.id_manufacturer=\d+\)$/,
  ];

  if (!allowed.some((pattern) => pattern.test(compact))) {
    throw new Error("Write SQL blocked. Statement is not on the catalog-maker allowlist.");
  }
}

function compactSql(sql) {
  return String(sql)
    .replace(/`/g, "")
    .replace(/\s+/g, " ")
    .trim()
    .toLowerCase()
    .replace(/\s/g, "");
}
