"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenRepo = void 0;
const uuid = __importStar(require("uuid"));
const TOKEN_ID = "tokens:token-id";
const ID_TOKEN = "tokens:id-token";
class TokenRepo {
    constructor(redis) {
        this.redis = redis;
    }
    async create(id) {
        const token = uuid.v4();
        await this.redis.eval(`
      redis.call('HSET', KEYS[1], ARGV[1], ARGV[2])
      redis.call('HSET', KEYS[2], ARGV[2], ARGV[1])
      `, 2, TOKEN_ID, ID_TOKEN, token, id);
        return token;
    }
    async getById(id) {
        return await this.redis.hget(ID_TOKEN, id);
    }
    async check(token) {
        return await this.redis.hget(TOKEN_ID, token);
    }
    async revoke(id) {
        const result = await this.redis.eval(`
      local token = redis.call('HGET', KEYS[2], ARGV[1])
      if (not token) then
          return false
      end

      redis.call('HDEL', KEYS[2], ARGV[1])
      redis.call('HDEL', KEYS[1], token)
      return 1
      `, 2, TOKEN_ID, ID_TOKEN, id);
        return !!result;
    }
}
exports.TokenRepo = TokenRepo;
//# sourceMappingURL=token-repo.js.map