

import { InjectionSymbols } from "../../constants.mjs";
import { inject, injectable } from "../../plugins/index.mjs";
import { __decorateMetadata } from "../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateMetadata.mjs";
import { __decorate } from "../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.mjs";
import "../../agent/index.mjs";
import { JsonTransformer } from "../../utils/JsonTransformer.mjs";
import "../../utils/index.mjs";
import "../../storage/BaseRecord.mjs";
import { __decorateParam } from "../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.mjs";
import { CacheModuleConfig } from "./CacheModuleConfig.mjs";

//#region src/modules/cache/CachedStorageService.ts
let CachedStorageService = class CachedStorageService$1 {
	constructor(storageService) {
		this.storageService = storageService;
	}
	cache(agentContext) {
		return agentContext.resolve(CacheModuleConfig).cache;
	}
	getCacheKey(options) {
		return `${options.type}:${options.id}`;
	}
	async save(agentContext, record) {
		if (record.allowCache) await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON());
		return await this.storageService.save(agentContext, record);
	}
	async update(agentContext, record) {
		if (record.allowCache) await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON());
		return await this.storageService.update(agentContext, record);
	}
	async delete(agentContext, record) {
		if (record.allowCache) await this.cache(agentContext).remove(agentContext, this.getCacheKey(record));
		return await this.storageService.delete(agentContext, record);
	}
	async deleteById(agentContext, recordClass, id) {
		if (recordClass.allowCache) await this.cache(agentContext).remove(agentContext, this.getCacheKey({
			...recordClass,
			id
		}));
		return await this.storageService.deleteById(agentContext, recordClass, id);
	}
	async getById(agentContext, recordClass, id) {
		if (recordClass.allowCache) {
			const cachedValue = await this.cache(agentContext).get(agentContext, this.getCacheKey({
				type: recordClass.type,
				id
			}));
			if (cachedValue) return JsonTransformer.fromJSON(cachedValue, recordClass);
		}
		const record = await this.storageService.getById(agentContext, recordClass, id);
		if (recordClass.allowCache) await this.cache(agentContext).set(agentContext, this.getCacheKey({
			type: recordClass.type,
			id
		}), record.toJSON());
		return record;
	}
	async getAll(agentContext, recordClass) {
		return await this.storageService.getAll(agentContext, recordClass);
	}
	async findByQuery(agentContext, recordClass, query, queryOptions) {
		return await this.storageService.findByQuery(agentContext, recordClass, query, queryOptions);
	}
};
CachedStorageService = __decorate([
	injectable(),
	__decorateParam(0, inject(InjectionSymbols.StorageService)),
	__decorateMetadata("design:paramtypes", [Object])
], CachedStorageService);

//#endregion
export { CachedStorageService };
//# sourceMappingURL=CachedStorageService.mjs.map