"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.outboundOrderByStatusResolver = void 0;
const biz_base_1 = require("@things-factory/biz-base");
// Simple in-memory cache with 30 second TTL
const dashboardCache = new Map();
// Auto-cleanup expired cache entries every 5 minutes
setInterval(() => {
    const now = Date.now();
    for (const [key, value] of dashboardCache.entries()) {
        if (value.expires <= now) {
            dashboardCache.delete(key);
        }
    }
}, 5 * 60 * 1000);
exports.outboundOrderByStatusResolver = {
    async outboundOrderByStatus(_, params, context) {
        var _a;
        const { domain, user, tx } = context.state;
        const bizplaceId = ((_a = params.filters.find((filter) => filter.name === 'bizplace')) === null || _a === void 0 ? void 0 : _a.value) || 'ALL';
        const v4 = new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i);
        let bizplaceIds = [];
        if (bizplaceId && (bizplaceId == 'ALL' || bizplaceId.match(v4))) {
            bizplaceIds = bizplaceId && bizplaceId != 'ALL' ? [bizplaceId] : await (0, biz_base_1.getPermittedBizplaceIds)(domain, user);
        }
        else
            throw new Error('invalid bizplaceId');
        // Create cache key from domain and sorted bizplace IDs
        const cacheKey = `${domain.id}:${bizplaceIds.sort().join(',')}`;
        const now = Date.now();
        // Check cache first
        const cached = dashboardCache.get(cacheKey);
        if (cached && cached.expires > now) {
            return cached.data;
        }
        // Cache miss - query database
        let items = await tx.query(`Select * from dashboard($1,$2) `, [bizplaceIds, domain.id]);
        const orderStatus = items;
        // Store in cache for 30 seconds
        dashboardCache.set(cacheKey, {
            data: orderStatus,
            expires: now + 30000
        });
        return orderStatus;
    }
};
//# sourceMappingURL=outbound-order-by-status.js.map