import { Service } from '../core/Service.js';
import services from './api_service.js';
export default class UserCacheService extends Service {
    static key = 'api-service/user-cache';
    organizationId;
    /**
     * Sets the organization ID for the user cache.
     * This must be called before fetching user data.
     * @param oid - The organization ID to set for the user cache.
     * This will clear the cache when the organization changes.
     */
    setOrganization(oid) {
        if (this.organizationId === oid) {
            return; // No change in organization, do nothing
        }
        this.organizationId = oid;
    }
    /**
     * Fetches a user by their unique identifier (UID).
     * If the user is already cached, it returns the cached user.
     * Otherwise, it fetches the user from the API and caches it.
     * @param uid - The unique identifier of the user to fetch.
     * @returns A promise that resolves to the IUser object or null if not found.
     */
    async getUser(uid) {
        const oid = this.organizationId;
        if (!oid) {
            throw new Error('Organization ID is not set. Call setOrganization() before fetching user data.');
        }
        const cache = await services.get('cache');
        const cacheKey = `user_${oid}_${uid}`;
        const cachedUser = await cache.get(cacheKey);
        if (cachedUser) {
            return cachedUser;
        }
        const sdk = await services.get('sdk');
        try {
            const user = await sdk.organizations.users.read(oid, uid);
            await cache.set(cacheKey, user);
            return user;
        }
        catch (error) {
            const log = await services.get('logger');
            log.error(`Failed to fetch user with ID ${uid}:`, error);
            return null;
        }
    }
}
//# sourceMappingURL=UserCacheService.js.map