(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "./column", "./expr", "./limit", "./tables", "./util", "./with"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("./column"), require("./expr"), require("./limit"), require("./tables"), require("./util"), require("./with"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.column, global.expr, global.limit, global.tables, global.util, global._with);
    global._delete = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _column, _expr, _limit, _tables, _util, _with) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.deleteToSQL = deleteToSQL;
  function deleteToSQL(stmt) {
    const {
      columns,
      from,
      table,
      where,
      orderby,
      with: withInfo,
      limit,
      returning
    } = stmt;
    const clauses = [(0, _with.withToSQL)(withInfo), 'DELETE'];
    const {
      database
    } = (0, _util.getParserOpt)();
    const isClickHouse = database && database.toLowerCase() === 'clickhouse';
    const isDuckDB = database && database.toLowerCase() === 'duckdb';

    // Handle table references between DELETE and FROM
    // ClickHouse and DuckDB always use DELETE FROM syntax without table refs
    if (!isClickHouse && !isDuckDB && columns && columns.length > 0) {
      const tableRefs = columns.map(col => {
        if (col.expr && col.expr.type === 'column_ref') {
          // Use table name if available, otherwise use column name
          if (col.expr.table) {
            return (0, _tables.tablesToSQL)([{
              db: col.expr.db,
              table: col.expr.table
            }]);
          }
          if (col.expr.column) {
            return `\`${col.expr.column}\``;
          }
          return '';
        }
        return (0, _column.columnsToSQL)([col], from);
      }).filter(_util.hasVal).join(', ');
      clauses.push(tableRefs);
    } else if (!isClickHouse && !isDuckDB && table && table.length > 0) {
      // Case 2: DELETE a FROM db.t ... (table contains what to delete)
      // Output table refs when:
      // 1. table has addition = false/undefined (meaning it should be shown)
      // 2. OR table is different from FROM clause (e.g., DELETE a FROM b)
      const shouldShowTable = table.filter(tbl => !tbl.addition);
      if (shouldShowTable.length > 0) {
        clauses.push((0, _tables.tablesToSQL)(shouldShowTable));
      }
    }
    clauses.push((0, _util.commonOptionConnector)('FROM', _tables.tablesToSQL, from || table));
    clauses.push((0, _util.commonOptionConnector)('WHERE', _expr.exprToSQL, where));
    clauses.push((0, _expr.orderOrPartitionByToSQL)(orderby, 'order by'));
    clauses.push((0, _limit.limitToSQL)(limit));
    clauses.push((0, _util.returningToSQL)(returning));
    return clauses.filter(_util.hasVal).join(' ');
  }
});