

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');
const require_parse = require('../domain/parse.js');
const require_decorateParam = require('../../../_virtual/_@oxc-project_runtime@0.95.0/helpers/decorateParam.js');
const require_DidsModuleConfig = require('../DidsModuleConfig.js');
const require_DidResolverService = require('./DidResolverService.js');
let tsyringe = require("tsyringe");
tsyringe = require_rolldown_runtime.__toESM(tsyringe);

//#region src/modules/dids/services/DidRegistrarService.ts
var _ref, _ref2;
let DidRegistrarService = class DidRegistrarService$1 {
	constructor(logger, didsModuleConfig, didResolverService) {
		this.logger = logger;
		this.didsModuleConfig = didsModuleConfig;
		this.didResolverService = didResolverService;
	}
	async create(agentContext, options) {
		this.logger.debug(`creating did ${options.did ?? options.method}`);
		const errorResult = {
			didDocumentMetadata: {},
			didRegistrationMetadata: {},
			didState: {
				state: "failed",
				did: options.did
			}
		};
		if (!options.did && !options.method || options.did && options.method) return {
			...errorResult,
			didState: {
				...errorResult.didState,
				reason: "Either did OR method must be specified"
			}
		};
		const method = options.method ?? require_parse.tryParseDid(options.did)?.method;
		if (!method) return {
			...errorResult,
			didState: {
				...errorResult.didState,
				reason: `Could not extract method from did ${options.did}`
			}
		};
		const registrar = this.findRegistrarForMethod(method);
		if (!registrar) return {
			...errorResult,
			didState: {
				...errorResult.didState,
				reason: `Unsupported did method: '${method}'`
			}
		};
		return await registrar.create(agentContext, options);
	}
	async update(agentContext, options) {
		this.logger.debug(`updating did ${options.did}`);
		const method = require_parse.tryParseDid(options.did)?.method;
		const errorResult = {
			didDocumentMetadata: {},
			didRegistrationMetadata: {},
			didState: {
				state: "failed",
				did: options.did
			}
		};
		if (!method) return {
			...errorResult,
			didState: {
				...errorResult.didState,
				reason: `Could not extract method from did ${options.did}`
			}
		};
		const registrar = this.findRegistrarForMethod(method);
		if (!registrar) return {
			...errorResult,
			didState: {
				...errorResult.didState,
				reason: `Unsupported did method: '${method}'`
			}
		};
		await this.didResolverService.invalidateCacheForDid(agentContext, options.did);
		return await registrar.update(agentContext, options);
	}
	async deactivate(agentContext, options) {
		this.logger.debug(`deactivating did ${options.did}`);
		const errorResult = {
			didDocumentMetadata: {},
			didRegistrationMetadata: {},
			didState: {
				state: "failed",
				did: options.did
			}
		};
		const method = require_parse.tryParseDid(options.did)?.method;
		if (!method) return {
			...errorResult,
			didState: {
				...errorResult.didState,
				reason: `Could not extract method from did ${options.did}`
			}
		};
		const registrar = this.findRegistrarForMethod(method);
		if (!registrar) return {
			...errorResult,
			didState: {
				...errorResult.didState,
				reason: `Unsupported did method: '${method}'`
			}
		};
		await this.didResolverService.invalidateCacheForDid(agentContext, options.did);
		return await registrar.deactivate(agentContext, options);
	}
	findRegistrarForMethod(method) {
		return this.didsModuleConfig.registrars.find((r) => r.supportedMethods.includes(method)) ?? null;
	}
	/**
	* Get all supported did methods for the did registrar.
	*/
	get supportedMethods() {
		return Array.from(new Set(this.didsModuleConfig.registrars.flatMap((r) => r.supportedMethods)));
	}
};
DidRegistrarService = 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_DidsModuleConfig.DidsModuleConfig !== "undefined" && require_DidsModuleConfig.DidsModuleConfig) === "function" ? _ref : Object,
		typeof (_ref2 = typeof require_DidResolverService.DidResolverService !== "undefined" && require_DidResolverService.DidResolverService) === "function" ? _ref2 : Object
	])
], DidRegistrarService);

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