

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 { __decorateParam } from "../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.mjs";
import { CURRENT_FRAMEWORK_STORAGE_VERSION, INITIAL_STORAGE_VERSION } from "./updates.mjs";
import { isStorageUpToDate } from "./isUpToDate.mjs";
import { StorageVersionRecord } from "./repository/StorageVersionRecord.mjs";
import { StorageVersionRepository } from "./repository/StorageVersionRepository.mjs";

//#region src/storage/migration/StorageUpdateService.ts
var _ref;
let StorageUpdateService = class StorageUpdateService$1 {
	constructor(logger, storageVersionRepository) {
		this.logger = logger;
		this.storageVersionRepository = storageVersionRepository;
	}
	async isUpToDate(agentContext, updateToVersion) {
		return isStorageUpToDate(await this.getCurrentStorageVersion(agentContext), updateToVersion);
	}
	async hasStorageVersionRecord(agentContext) {
		return await this.storageVersionRepository.findById(agentContext, StorageVersionRecord.storageVersionRecordId) !== null;
	}
	async getCurrentStorageVersion(agentContext) {
		return (await this.getStorageVersionRecord(agentContext)).storageVersion;
	}
	static get frameworkStorageVersion() {
		return CURRENT_FRAMEWORK_STORAGE_VERSION;
	}
	async setCurrentStorageVersion(agentContext, storageVersion) {
		this.logger.debug(`Setting current agent storage version to ${storageVersion}`);
		const storageVersionRecord = await this.storageVersionRepository.findById(agentContext, StorageVersionRecord.storageVersionRecordId);
		if (!storageVersionRecord) {
			this.logger.trace("Storage upgrade record does not exist yet. Creating.");
			await this.storageVersionRepository.save(agentContext, new StorageVersionRecord({ storageVersion }));
		} else {
			this.logger.trace("Storage upgrade record already exists. Updating.");
			storageVersionRecord.storageVersion = storageVersion;
			await this.storageVersionRepository.update(agentContext, storageVersionRecord);
		}
	}
	/**
	* Retrieve the update record, creating it if it doesn't exist already.
	*
	* The storageVersion will be set to the INITIAL_STORAGE_VERSION if it doesn't exist yet,
	* as we can assume the wallet was created before the update record existed
	*/
	async getStorageVersionRecord(agentContext) {
		let storageVersionRecord = await this.storageVersionRepository.findById(agentContext, StorageVersionRecord.storageVersionRecordId);
		if (!storageVersionRecord) {
			storageVersionRecord = new StorageVersionRecord({ storageVersion: INITIAL_STORAGE_VERSION });
			await this.storageVersionRepository.save(agentContext, storageVersionRecord);
		}
		return storageVersionRecord;
	}
};
StorageUpdateService = __decorate([
	injectable(),
	__decorateParam(0, inject(InjectionSymbols.Logger)),
	__decorateMetadata("design:paramtypes", [Object, typeof (_ref = typeof StorageVersionRepository !== "undefined" && StorageVersionRepository) === "function" ? _ref : Object])
], StorageUpdateService);

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