import { TenantAgent } from "./TenantAgent.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 { __decorateParam } from "../../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.mjs";
import { TenantRecordService } from "./services/TenantRecordService.mjs";
import "./services/index.mjs";
import { TenantAgentContextProvider } from "./context/TenantAgentContextProvider.mjs";
import { AgentContext, InjectionSymbols, UpdateAssistant, inject, injectable, isStorageUpToDate } from "@credo-ts/core";

//#region ../tenants/src/TenantsApi.ts
var _ref, _ref2, _ref3;
let TenantsApi = class TenantsApi$1 {
	constructor(tenantRecordService, rootAgentContext, agentContextProvider, logger) {
		this.tenantRecordService = tenantRecordService;
		this.rootAgentContext = rootAgentContext;
		this.agentContextProvider = agentContextProvider;
		this.logger = logger;
	}
	async getTenantAgent({ tenantId }) {
		return this._getTenantAgent({ tenantId });
	}
	async withTenantAgent(options, withTenantAgentCallback) {
		this.logger.debug(`Getting tenant agent for tenant '${options.tenantId}' in with tenant agent callback`);
		const tenantAgent = await this.getTenantAgent(options);
		try {
			this.logger.debug(`Calling tenant agent callback for tenant '${options.tenantId}'`);
			return await withTenantAgentCallback(tenantAgent);
		} catch (error) {
			this.logger.error(`Error in tenant agent callback for tenant '${options.tenantId}'`, { error });
			throw error;
		} finally {
			this.logger.debug(`Ending tenant agent session for tenant '${options.tenantId}'`);
			await tenantAgent.endSession();
		}
	}
	async createTenant(options) {
		this.logger.debug("Creating tenant");
		const tenantRecord = await this.tenantRecordService.createTenant(this.rootAgentContext, options.config);
		await (await this._getTenantAgent({
			tenantId: tenantRecord.id,
			provisionContext: true
		})).endSession();
		this.logger.info(`Successfully created tenant '${tenantRecord.id}'`);
		return tenantRecord;
	}
	async getTenantById(tenantId) {
		this.logger.debug(`Getting tenant by id '${tenantId}'`);
		return this.tenantRecordService.getTenantById(this.rootAgentContext, tenantId);
	}
	async findTenantsByLabel(label) {
		this.logger.debug(`Finding tenants by label '${label}'`);
		return this.tenantRecordService.findTenantsByLabel(this.rootAgentContext, label);
	}
	async deleteTenantById(tenantId) {
		this.logger.debug(`Deleting tenant by id '${tenantId}'`);
		const tenantAgent = await this.getTenantAgent({ tenantId });
		this.logger.trace(`Deleting wallet for tenant '${tenantId}'`);
		await this.agentContextProvider.deleteAgentContext(tenantAgent.context);
		return this.tenantRecordService.deleteTenantById(this.rootAgentContext, tenantId);
	}
	async updateTenant(tenant) {
		await this.tenantRecordService.updateTenant(this.rootAgentContext, tenant);
	}
	async findTenantsByQuery(query, queryOptions) {
		return this.tenantRecordService.findTenantsByQuery(this.rootAgentContext, query, queryOptions);
	}
	async getAllTenants() {
		this.logger.debug("Getting all tenants");
		return this.tenantRecordService.getAllTenants(this.rootAgentContext);
	}
	async updateTenantStorage({ tenantId, updateOptions }) {
		this.logger.debug(`Updating tenant storage for tenant '${tenantId}'`);
		const tenantRecord = await this.tenantRecordService.getTenantById(this.rootAgentContext, tenantId);
		if (isStorageUpToDate(tenantRecord.storageVersion)) {
			this.logger.debug(`Tenant storage for tenant '${tenantId}' is already up to date. Skipping update`);
			return;
		}
		await this.agentContextProvider.updateTenantStorage(tenantRecord, updateOptions);
	}
	async getTenantsWithOutdatedStorage() {
		return await this.tenantRecordService.findTenantsByQuery(this.rootAgentContext, { $not: { storageVersion: UpdateAssistant.frameworkStorageVersion } });
	}
	async _getTenantAgent({ tenantId, provisionContext = false }) {
		this.logger.debug(`Getting tenant agent for tenant '${tenantId}'`);
		const tenantContext = await this.agentContextProvider.getAgentContextForContextCorrelationId(this.agentContextProvider.getContextCorrelationIdForTenantId(tenantId), { provisionContext });
		this.logger.trace(`Got tenant context for tenant '${tenantId}'`);
		const tenantAgent = new TenantAgent(tenantContext);
		await tenantAgent.initialize();
		this.logger.trace(`Initializing tenant agent for tenant '${tenantId}'`);
		return tenantAgent;
	}
};
TenantsApi = __decorate([
	injectable(),
	__decorateParam(2, inject(InjectionSymbols.AgentContextProvider)),
	__decorateParam(3, inject(InjectionSymbols.Logger)),
	__decorateMetadata("design:paramtypes", [
		typeof (_ref = typeof TenantRecordService !== "undefined" && TenantRecordService) === "function" ? _ref : Object,
		typeof (_ref2 = typeof AgentContext !== "undefined" && AgentContext) === "function" ? _ref2 : Object,
		typeof (_ref3 = typeof TenantAgentContextProvider !== "undefined" && TenantAgentContextProvider) === "function" ? _ref3 : Object,
		Object
	])
], TenantsApi);

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