// @ts-nocheck
const writeStatementPattern =
  /^(?:alter|analyze|call|create|delete|drop|grant|insert|load|lock|optimize|replace|revoke|set|truncate|update)\b/i;

export function assertReadOnlySql(sql) {
  const normalized = stripLeadingSqlNoise(sql);

  if (!normalized) {
    throw new Error("SQL query is empty.");
  }

  if (writeStatementPattern.test(normalized)) {
    throw new Error(`Blocked non-read-only SQL statement: ${normalized.split(/\s+/, 1)[0].toUpperCase()}`);
  }
}

function stripLeadingSqlNoise(sql) {
  return String(sql)
    .replace(/^\s+/g, "")
    .replace(/^(?:--[^\n]*\n|\/\*[\s\S]*?\*\/\s*)+/g, "")
    .trim();
}
