import { FilterableQueryBuilder } from './where-query-builder.js';
/**
 * A query builder for deleting records from the database.
 * @template DB - The database type.
 * @template TableName - The name of the table to delete from.
 */
export class DeleteQueryBuilder extends FilterableQueryBuilder {
    #node;
    constructor(node, executor) {
        super(node, executor);
        this.#node = node;
    }
    /**
     * Executes the delete query.
     * @returns A promise that resolves with the number of affected records.
     *
     * @example
     * ```typescript
     * const { affectedRecords } = await qb
     *   .deleteFrom('users')
     *   .where('id', '=', 1)
     *   .execute();
     * ```
     */
    async execute() {
        const response = await this._executor.execute(this);
        return response[0];
    }
    compile() {
        const query = 'mutation ($metadata: JSON) { execute(metadata: $metadata) }';
        const metadata = [this._prepareMetadata()];
        return {
            query,
            variables: {
                metadata,
            },
        };
    }
    _prepareMetadata() {
        return {
            type: 'delete',
            tableName: this.#node.tableName,
            ...(this.#node.filtersSet.filtersSet.length > 0
                ? { filtersSet: this.#node.filtersSet }
                : {}),
        };
    }
}
//# sourceMappingURL=delete-query-builder.js.map