import { TenantRecord } from "../repository/TenantRecord.mjs";
import { TenantRoutingRecord } from "../repository/TenantRoutingRecord.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 { TenantRepository } from "../repository/TenantRepository.mjs";
import { TenantRoutingRepository } from "../repository/TenantRoutingRepository.mjs";
import "../repository/index.mjs";
import { UpdateAssistant, injectable, utils } from "@credo-ts/core";

//#region ../tenants/src/services/TenantRecordService.ts
var _ref, _ref2;
let TenantRecordService = class TenantRecordService$1 {
	constructor(tenantRepository, tenantRoutingRepository) {
		this.tenantRepository = tenantRepository;
		this.tenantRoutingRepository = tenantRoutingRepository;
	}
	async createTenant(agentContext, config) {
		const tenantRecord = new TenantRecord({
			id: utils.uuid(),
			config,
			storageVersion: UpdateAssistant.frameworkStorageVersion
		});
		await this.tenantRepository.save(agentContext, tenantRecord);
		return tenantRecord;
	}
	async getTenantById(agentContext, tenantId) {
		return this.tenantRepository.getById(agentContext, tenantId);
	}
	async findTenantsByLabel(agentContext, label) {
		return this.tenantRepository.findByLabel(agentContext, label);
	}
	async getAllTenants(agentContext) {
		return this.tenantRepository.getAll(agentContext);
	}
	async deleteTenantById(agentContext, tenantId) {
		const tenantRecord = await this.getTenantById(agentContext, tenantId);
		const tenantRoutingRecords = await this.tenantRoutingRepository.findByQuery(agentContext, { tenantId: tenantRecord.id });
		await Promise.all(tenantRoutingRecords.map((tenantRoutingRecord) => this.tenantRoutingRepository.delete(agentContext, tenantRoutingRecord)));
		await this.tenantRepository.delete(agentContext, tenantRecord);
	}
	async updateTenant(agentContext, tenantRecord) {
		return this.tenantRepository.update(agentContext, tenantRecord);
	}
	async findTenantsByQuery(agentContext, query, queryOptions) {
		return this.tenantRepository.findByQuery(agentContext, query, queryOptions);
	}
	async findTenantRoutingRecordByRecipientKey(agentContext, recipientKey) {
		return this.tenantRoutingRepository.findByRecipientKey(agentContext, recipientKey);
	}
	async addTenantRoutingRecord(agentContext, tenantId, recipientKey) {
		const tenantRoutingRecord = new TenantRoutingRecord({
			tenantId,
			recipientKeyFingerprint: recipientKey.fingerprint
		});
		await this.tenantRoutingRepository.save(agentContext, tenantRoutingRecord);
		return tenantRoutingRecord;
	}
};
TenantRecordService = __decorate([injectable(), __decorateMetadata("design:paramtypes", [typeof (_ref = typeof TenantRepository !== "undefined" && TenantRepository) === "function" ? _ref : Object, typeof (_ref2 = typeof TenantRoutingRepository !== "undefined" && TenantRoutingRepository) === "function" ? _ref2 : Object])], TenantRecordService);

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