import { Command } from "./command.js";
function deserialize(fields, result) {
    if (result.length === 0 || result.every((field) => field === null)) {
        return null;
    }
    const obj = {};
    for (let i = 0; i < fields.length; i++) {
        try {
            obj[fields[i]] = JSON.parse(result[i]);
        }
        catch {
            obj[fields[i]] = result[i];
        }
    }
    return obj;
}
/**
 * hmget returns an object of all requested fields from a hash
 * The field values are returned as an object like this:
 * ```ts
 * {[fieldName: string]: T | null}
 * ```
 *
 * In case the hash does not exist or all fields are empty `null` is returned
 *
 * @see https://redis.io/commands/hmget
 */
export class HMGetCommand extends Command {
    constructor([key, ...fields], opts) {
        super(["hmget", key, ...fields], {
            deserialize: (result) => deserialize(fields, result),
            ...opts,
        });
    }
}
