"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCompositeCacheKey = getCompositeCacheKey;
exports.getOrCacheValue = getOrCacheValue;
exports.getCachedValue = getCachedValue;
exports.getCachedValueOrThrow = getCachedValueOrThrow;
exports.createCachedValueResolver = createCachedValueResolver;
exports.createCachingParameterResolver = createCachingParameterResolver;
/*@ts-ignore*/
var { __ΩClassType } = require('@deepkit/core');
/*@ts-ignore*/
var { __ΩRouteParameterResolverContext, __ΩRouteParameterResolver } = require('@deepkit/http');
const http_1 = require("@deepkit/http");
const __ΩCacheKey = ['CacheKey', 'P&+%Jw!y'];
const ObjectRefSymbol = Symbol('ObjectRef');
/**
 * Key generator
 */
function getCompositeCacheKey(target, key) {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    if (target[key] === undefined) {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        target[key] = Symbol(`${target.name}_${key.description}`);
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return target[key];
}
getCompositeCacheKey.__type = [() => __ΩClassType, 'target', 'key', 'getCompositeCacheKey', 'Pn!2"+2#"/$'];
const __ΩValueResolver = ['T', () => http_1.HttpRequest, 'request', '', 'ValueResolver', 'b!PP7"2#e"!`/$w%y'];
async function getOrCacheValue(request, key, resolver) {
    if (typeof key === 'function' || typeof key === 'object') {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        if (request.store[ObjectRefSymbol] === undefined) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            request.store[ObjectRefSymbol] = (Map.Ω = [['%'], [() => resolver, () => __ΩValueResolver, 'Pdi!h!!o""qe!!!j']], new Map());
        }
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const map = request.store[ObjectRefSymbol];
        if (!map.has(key)) {
            const value = await resolver(request);
            map.set(key, value);
            return value;
        }
        return map.get(key);
    }
    else {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        if (request.store[key] === undefined) {
            // eslint-disable-next-line @typescript-eslint/no-explicit-any
            request.store[key] = await resolver(request);
        }
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        return request.store[key];
    }
}
getOrCacheValue.__type = [() => http_1.HttpRequest, 'request', () => __ΩCacheKey, 'key', () => __ΩValueResolver, 'resolver', 'getOrCacheValue', 'PP7!2"n#2$"o%"2&"`/\''];
function getCachedValue(request, key) {
    const isObjectRefKey = typeof key === 'function' || typeof key === 'object';
    const resolvedKey = isObjectRefKey ? ObjectRefSymbol : key;
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const rawValue = request.store[resolvedKey];
    return isObjectRefKey ? rawValue.get(key) : rawValue;
}
getCachedValue.__type = [() => http_1.HttpRequest, 'request', () => __ΩCacheKey, 'key', 'getCachedValue', 'PP7!2"n#2$P"-J/%'];
function getCachedValueOrThrow(request, key) {
    const value = (getCachedValue.Ω = [['!']], getCachedValue(request, key));
    if (value === undefined)
        throw new Error(`Request does not contain cached value [${String(key)}]`);
    return value;
}
getCachedValueOrThrow.__type = [() => http_1.HttpRequest, 'request', () => __ΩCacheKey, 'key', 'getCachedValueOrThrow', 'PP7!2"n#2$"/%'];
/**
 * Generic store value resolver
 * Requires a previous listener to have attached data to the store
 */
function createCachedValueResolver(key, isRequired = true) {
    var _a;
    const getter = isRequired ? getCachedValueOrThrow : getCachedValue;
    return _a = class {
            async resolve(context) {
                return getter(context.request, key) ?? undefined;
            }
        },
        _a.__type = [() => __ΩRouteParameterResolverContext, 'context', 'resolve', () => __ΩRouteParameterResolver, 'Pn!2"P!-J`0#5n$x"'],
        _a;
}
createCachedValueResolver.__type = ['key', 'isRequired', 'createCachedValueResolver', 'PP&+%J2!"2""/#'];
/**
 * Generic store value resolver with cache miss lookup
 */
function createCachingParameterResolver(key, lookupFn) {
    var _a;
    return _a = class {
            async resolve(context) {
                return getOrCacheValue(context.request, key, async () => lookupFn(context));
            }
        },
        _a.__type = [() => __ΩRouteParameterResolverContext, 'context', () => lookupFn, () => __ΩRouteParameterResolverContext, '', 'resolve', () => __ΩRouteParameterResolver, 'Pn!2"PPdi#Pn$2"h"!`/%qe!!!j-J`0&5n\'x"'],
        _a;
}
createCachingParameterResolver.__type = ['key', () => __ΩRouteParameterResolverContext, 'context', '', 'lookupFn', 'createCachingParameterResolver', 'PP&+%J2!Pn"2#"`/$2%"/&'];
//# sourceMappingURL=store.js.map