"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.HRandFieldCommand = void 0;
const command_js_1 = require("./command.js");
function deserialize(result) {
    if (result.length === 0) {
        return null;
    }
    const obj = {};
    while (result.length >= 2) {
        const key = result.shift();
        const value = result.shift();
        try {
            obj[key] = JSON.parse(value);
        }
        catch {
            obj[key] = value;
        }
    }
    return obj;
}
/**
 * @see https://redis.io/commands/hrandfield
 */
class HRandFieldCommand extends command_js_1.Command {
    constructor(cmd, opts) {
        const command = ["hrandfield", cmd[0]];
        if (typeof cmd[1] === "number") {
            command.push(cmd[1]);
        }
        if (cmd[2]) {
            command.push("WITHVALUES");
        }
        super(command, {
            // @ts-ignore TODO:
            deserialize: cmd[2]
                ? (result) => deserialize(result)
                : opts?.deserialize,
            ...opts,
        });
    }
}
exports.HRandFieldCommand = HRandFieldCommand;
