

const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.js');
const require_constants = require('../../constants.js');
require('../../plugins/index.js');
const require_decorateMetadata = require('../../_virtual/_@oxc-project_runtime@0.95.0/helpers/decorateMetadata.js');
const require_decorate = require('../../_virtual/_@oxc-project_runtime@0.95.0/helpers/decorate.js');
require('../../agent/index.js');
const require_JsonTransformer = require('../../utils/JsonTransformer.js');
require('../../utils/index.js');
require('../../storage/BaseRecord.js');
const require_decorateParam = require('../../_virtual/_@oxc-project_runtime@0.95.0/helpers/decorateParam.js');
const require_CacheModuleConfig = require('./CacheModuleConfig.js');
let tsyringe = require("tsyringe");
tsyringe = require_rolldown_runtime.__toESM(tsyringe);

//#region src/modules/cache/CachedStorageService.ts
let CachedStorageService = class CachedStorageService$1 {
	constructor(storageService) {
		this.storageService = storageService;
	}
	cache(agentContext) {
		return agentContext.resolve(require_CacheModuleConfig.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 require_JsonTransformer.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 = require_decorate.__decorate([
	(0, tsyringe.injectable)(),
	require_decorateParam.__decorateParam(0, (0, tsyringe.inject)(require_constants.InjectionSymbols.StorageService)),
	require_decorateMetadata.__decorateMetadata("design:paramtypes", [Object])
], CachedStorageService);

//#endregion
Object.defineProperty(exports, 'CachedStorageService', {
  enumerable: true,
  get: function () {
    return CachedStorageService;
  }
});