const require_updateAuthFromVerifyResponse = require('./updateAuthFromVerifyResponse-Dg8THxkT.cjs');
const require_isDynamicWaasEnabled = require('./isDynamicWaasEnabled-v7x1B-0g.cjs');
const require_getSignedSessionId = require('./getSignedSessionId-BWTQMtk6.cjs');
let _dynamic_labs_wallet_browser_wallet_client = require("@dynamic-labs-wallet/browser-wallet-client");

//#region src/errors/NotWaasWalletProviderError.ts
var NotWaasWalletProviderError = class extends require_updateAuthFromVerifyResponse.BaseError {
	constructor({ walletProviderKey }) {
		super({
			cause: null,
			code: "not_waas_wallet_provider_error",
			docsUrl: null,
			name: "NotWaasWalletProviderError",
			shortMessage: `Wallet provider ${walletProviderKey} is not a Dynamic WaaS wallet provider`
		});
	}
};

//#endregion
//#region src/modules/waas/createWaasClient/createWaasClient.ts
/** @notInstrumented */
const createWaasClient = ({ chainName }, client) => {
	const core = require_updateAuthFromVerifyResponse.getCore(client);
	const additionalTrustedOrigins = core.waas?.additionalTrustedOrigins;
	const baseClientKeysharesRelayApiUrl = client.projectSettings?.sdk.waas?.customKeyshareRelayBaseUrl;
	return new _dynamic_labs_wallet_browser_wallet_client.DynamicWalletClient({
		authMode: require_updateAuthFromVerifyResponse.isCookieEnabled(client) ? "cookie" : "header",
		authToken: client.token || "",
		baseApiUrl: (core.apiBaseUrl || require_updateAuthFromVerifyResponse.DEFAULT_WAAS_BASE_API_URL).replace("/api/v0", ""),
		baseMPCRelayApiUrl: client.projectSettings?.sdk.waas?.relayUrl || require_updateAuthFromVerifyResponse.DEFAULT_WAAS_BASE_MPC_RELAY_API_URL,
		chainName,
		eagerlyRecoverKeyShares: true,
		environmentId: core.environmentId,
		sdkVersion: `${require_updateAuthFromVerifyResponse.CLIENT_SDK_NAME}/${core.version}`,
		...additionalTrustedOrigins == void 0 ? {} : { additionalTrustedOrigins },
		...baseClientKeysharesRelayApiUrl == void 0 ? {} : { baseClientKeysharesRelayApiUrl },
		...core.waas?.loadTimeoutMs == void 0 ? {} : { iframeLoadTimeout: core.waas.loadTimeoutMs },
		...core.waas?.maxLoadRetries == void 0 ? {} : { maxRetryAttempts: core.waas.maxLoadRetries }
	}, {
		getSignedSessionId: () => require_getSignedSessionId.getSignedSessionId(client).then(({ signature }) => signature),
		onUnauthorized: () => {
			require_updateAuthFromVerifyResponse.__logoutWithReason_wrapped({ reason: "session-refresh-unauthorized" }, client).catch((error) => {
				core.logger.error("Failed to log out after WaaS unauthorized response", error);
			});
		}
	});
};

//#endregion
//#region src/modules/waas/getWaasChainNameFromChain/getWaasChainNameFromChain.ts
/** @notInstrumented */
const getWaasChainNameFromChain = (chain) => {
	return require_updateAuthFromVerifyResponse.CHAINS_INFO_MAP[chain].waasChainNameOverride || chain;
};

//#endregion
//#region src/modules/waas/getWaasWalletProviderFromWalletAccount/getWaasWalletProviderFromWalletAccount.ts
/** @notInstrumented */
const getWaasWalletProviderFromWalletAccount = ({ walletAccount }, client) => {
	const walletProvider = require_updateAuthFromVerifyResponse.getWalletProviderFromWalletAccount({ walletAccount }, client);
	if (!require_updateAuthFromVerifyResponse.isWaasWalletProvider(walletProvider)) throw new require_isDynamicWaasEnabled.NotWaasWalletAccountError({ walletAddress: walletAccount.address });
	return walletProvider;
};

//#endregion
//#region src/modules/waas/migrateFromFireblocks/migrateFromFireblocks.ts
/**
* Migrates an existing Fireblocks NCW wallet into Dynamic's WaaS infrastructure.
*
* Migration is a single cross-chain operation: one call takes over the Fireblocks
* key material and provisions Dynamic WaaS wallet accounts for every supported
* chain. Because it is not per-chain, this resolves a single WaaS wallet provider
* (any registered chain) and delegates the takeover to it.
*
* @param params.deviceId - The Fireblocks NCW device id being migrated.
* @param params.jwt - The Fireblocks-issued JWT authorizing the takeover.
* @param params.storage - The serialized Fireblocks NCW device storage.
* @param params.password - The password that encrypted the legacy device storage.
* @param params.expectedAddresses - Required per-chain expected address; its keys are the chains migration runs on, and each imported address is verified against it.
* @param [params.coinTypes] - Optional SLIP-0044 coin types per chain name.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise resolving to the per-chain migration results.
* @notInstrumented
*/
const migrateFromFireblocks = async ({ coinTypes, deviceId, expectedAddresses, jwt, password, storage }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const providers = require_updateAuthFromVerifyResponse.getWalletProviders(client);
	if (providers.length === 0) throw new require_updateAuthFromVerifyResponse.NoWalletProviderFoundError({ walletProviderKey: require_updateAuthFromVerifyResponse.DYNAMIC_WAAS_METADATA.displayName });
	const waasProvider = providers.find(require_updateAuthFromVerifyResponse.isWaasWalletProvider);
	if (!waasProvider) throw new NotWaasWalletProviderError({ walletProviderKey: providers[0].key });
	const result = await waasProvider.migrateFromFireblocks({
		coinTypes,
		deviceId,
		expectedAddresses,
		jwt,
		password,
		storage
	});
	await require_isDynamicWaasEnabled.__refreshAuth_wrapped(client);
	return result;
};

//#endregion
Object.defineProperty(exports, 'NotWaasWalletProviderError', {
  enumerable: true,
  get: function () {
    return NotWaasWalletProviderError;
  }
});
Object.defineProperty(exports, 'createWaasClient', {
  enumerable: true,
  get: function () {
    return createWaasClient;
  }
});
Object.defineProperty(exports, 'getWaasChainNameFromChain', {
  enumerable: true,
  get: function () {
    return getWaasChainNameFromChain;
  }
});
Object.defineProperty(exports, 'getWaasWalletProviderFromWalletAccount', {
  enumerable: true,
  get: function () {
    return getWaasWalletProviderFromWalletAccount;
  }
});
Object.defineProperty(exports, 'migrateFromFireblocks', {
  enumerable: true,
  get: function () {
    return migrateFromFireblocks;
  }
});
//# sourceMappingURL=migrateFromFireblocks-BbRj_YOv.cjs.map