

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.94.0/helpers/decorateMetadata.js');
const require_decorate = require('../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorate.js');
const require_decorateParam = require('../../_virtual/_@oxc-project_runtime@0.94.0/helpers/decorateParam.js');
const require_updates = require('./updates.js');
const require_isUpToDate = require('./isUpToDate.js');
const require_StorageVersionRecord = require('./repository/StorageVersionRecord.js');
const require_StorageVersionRepository = require('./repository/StorageVersionRepository.js');
let tsyringe = require("tsyringe");
tsyringe = require_rolldown_runtime.__toESM(tsyringe);

//#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 require_isUpToDate.isStorageUpToDate(await this.getCurrentStorageVersion(agentContext), updateToVersion);
	}
	async hasStorageVersionRecord(agentContext) {
		return await this.storageVersionRepository.findById(agentContext, require_StorageVersionRecord.StorageVersionRecord.storageVersionRecordId) !== null;
	}
	async getCurrentStorageVersion(agentContext) {
		return (await this.getStorageVersionRecord(agentContext)).storageVersion;
	}
	static get frameworkStorageVersion() {
		return require_updates.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, require_StorageVersionRecord.StorageVersionRecord.storageVersionRecordId);
		if (!storageVersionRecord) {
			this.logger.trace("Storage upgrade record does not exist yet. Creating.");
			await this.storageVersionRepository.save(agentContext, new require_StorageVersionRecord.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, require_StorageVersionRecord.StorageVersionRecord.storageVersionRecordId);
		if (!storageVersionRecord) {
			storageVersionRecord = new require_StorageVersionRecord.StorageVersionRecord({ storageVersion: require_updates.INITIAL_STORAGE_VERSION });
			await this.storageVersionRepository.save(agentContext, storageVersionRecord);
		}
		return storageVersionRecord;
	}
};
StorageUpdateService = require_decorate.__decorate([
	(0, tsyringe.injectable)(),
	require_decorateParam.__decorateParam(0, (0, tsyringe.inject)(require_constants.InjectionSymbols.Logger)),
	require_decorateMetadata.__decorateMetadata("design:paramtypes", [Object, typeof (_ref = typeof require_StorageVersionRepository.StorageVersionRepository !== "undefined" && require_StorageVersionRepository.StorageVersionRepository) === "function" ? _ref : Object])
], StorageUpdateService);

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