const require_isMfaRequiredForAction = require('./isMfaRequiredForAction-Btejc5g3.cjs');
const require_updateAuthFromVerifyResponse = require('./updateAuthFromVerifyResponse-CElYpi97.cjs');
const require_isDynamicWaasEnabled = require('./isDynamicWaasEnabled-BwP1oowd.cjs');
const require_getWalletProviderByKey = require('./getWalletProviderByKey-CP9Te5Sz.cjs');
const require_migrateFromFireblocks = require('./migrateFromFireblocks-B_q8Oawb.cjs');
let _dynamic_labs_sdk_assert_package_version = require("@dynamic-labs-sdk/assert-package-version");
let _dynamic_labs_sdk_api_core = require("@dynamic-labs/sdk-api-core");

//#region src/modules/waas/backupWaasKeySharesToGoogleDrive/backupWaasKeySharesToGoogleDrive.ts
/**
* Backs up WaaS wallet key shares to Google Drive for secure recovery.
*
* This function creates a backup of the wallet's cryptographic key shares
* to Google Drive, allowing users to recover their wallet later.
*
* @param params.walletAccount - The WaaS wallet account to backup.
* @param [params.password] - Optional password to encrypt the backup.
* @param [params.googleDriveAccessToken] - Optional consumer-supplied Drive-scoped access token; used directly instead of the stored OAuth token (avoids a second Google consent). Useful for external-auth setups.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the backup process is complete.
* @notInstrumented
*/
const backupWaasKeySharesToGoogleDrive = async ({ googleDriveAccessToken, password, walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).backupKeySharesToGoogleDrive({
		googleDriveAccessToken,
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/waas/isWalletCreationRequirement/isWalletCreationRequirement.ts
/**
* Type guard to check if an item is a WalletCreationRequirement object.
* Chain values are strings, so we can distinguish by checking if it's an object with a 'chain' property.
*
* @param item - The item to check, either a WalletCreationRequirement or Chain string
* @returns True if the item is a WalletCreationRequirement object
* @notInstrumented
*/
const isWalletCreationRequirement = (item) => typeof item === "object" && item !== null && "chain" in item;

//#endregion
//#region src/modules/waas/normalizeWalletRequirements/normalizeWalletRequirements.ts
const DEFAULT_BITCOIN_ADDRESS_TYPE = "native_segwit";
/**
* Normalizes wallet creation input from either legacy Chain[] format
* or new WalletCreationRequirement[] format to WalletCreationRequirement[].
*
* For BTC chains without explicit bitcoinConfig, defaults to native_segwit.
*
* @param params.input - Either an array of Chain strings or WalletCreationRequirement objects
* @param params.defaultBitcoinConfig - Optional default bitcoin config to use for BTC chains when converting from legacy format
* @returns Array of WalletCreationRequirement objects
* @notInstrumented
*/
const normalizeWalletRequirements = ({ defaultBitcoinConfig, input }) => {
	if (input.length === 0) return [];
	const [firstItem] = input;
	if (isWalletCreationRequirement(firstItem)) return input.map((requirement) => {
		if (requirement.chain === "BTC" && !requirement.bitcoinConfig) return {
			...requirement,
			bitcoinConfig: { addressType: DEFAULT_BITCOIN_ADDRESS_TYPE }
		};
		return requirement;
	});
	return input.map((chain) => {
		const requirement = { chain };
		if (chain === "BTC") requirement.bitcoinConfig = defaultBitcoinConfig ?? { addressType: DEFAULT_BITCOIN_ADDRESS_TYPE };
		return requirement;
	});
};

//#endregion
//#region src/modules/waas/createWaasWalletAccounts/createWaasWalletAccounts.ts
/**
* Creates wallet accounts for the specified chains.
*
* This function creates wallet accounts for the specified chains.
* If the same chain is passed multiple times, just as many wallet accounts will be created
* for that chain.
*
* @example
* ```ts
* // Legacy format - will create wallet accounts for EVM and SOL, BTC defaults to native_segwit
* await createWaasWalletAccounts({ chains: ['EVM', 'SOL', 'BTC'] });
*
* // New format with chain-specific config
* await createWaasWalletAccounts({
*   chains: [
*     { chain: 'BTC', bitcoinConfig: { addressType: 'taproot' } },
*     { chain: 'EVM' },
*   ]
* });
*
* // Create both BTC address types
* await createWaasWalletAccounts({
*   chains: [
*     { chain: 'BTC', bitcoinConfig: { addressType: 'native_segwit' } },
*     { chain: 'BTC', bitcoinConfig: { addressType: 'taproot' } },
*   ]
* });
* ```
*
* @param params.chains - The chains to create wallet accounts for.
* @param params.password - The optional password to use for the wallet accounts.
* @param params.thresholdSignatureScheme - The threshold signature scheme to use for the wallet accounts. (Default: TWO_OF_TWO)
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when all wallet accounts are created.
* @notInstrumented
*/
const createWaasWalletAccounts = async ({ chains, password, thresholdSignatureScheme }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const normalizedRequirements = normalizeWalletRequirements({ input: chains });
	const supportedChains = [
		"BTC",
		"EVM",
		"SOL",
		"SUI",
		"TON",
		"TRON"
	];
	const filteredRequirements = normalizedRequirements.filter((requirement) => supportedChains.includes(requirement.chain));
	if (filteredRequirements.length === 0) return;
	const walletProviderMap = /* @__PURE__ */ new Map();
	for (const requirement of filteredRequirements) {
		const walletProviderKey = require_updateAuthFromVerifyResponse.formatWalletProviderKey({
			chain: requirement.chain,
			displayName: require_updateAuthFromVerifyResponse.DYNAMIC_WAAS_METADATA.displayName,
			walletProviderType: _dynamic_labs_sdk_api_core.WalletProviderEnum.EmbeddedWallet
		});
		if (!walletProviderMap.has(walletProviderKey)) {
			const walletProvider = require_getWalletProviderByKey.getWalletProviderByKey({ walletProviderKey }, client);
			if (!walletProvider) throw new require_updateAuthFromVerifyResponse.NoWalletProviderFoundError({ walletProviderKey });
			if (!require_updateAuthFromVerifyResponse.isWaasWalletProvider(walletProvider)) throw new require_migrateFromFireblocks.NotWaasWalletProviderError({ walletProviderKey });
			walletProviderMap.set(walletProviderKey, walletProvider);
		}
	}
	await Promise.all(filteredRequirements.map((requirement) => {
		const walletProviderKey = require_updateAuthFromVerifyResponse.formatWalletProviderKey({
			chain: requirement.chain,
			displayName: require_updateAuthFromVerifyResponse.DYNAMIC_WAAS_METADATA.displayName,
			walletProviderType: _dynamic_labs_sdk_api_core.WalletProviderEnum.EmbeddedWallet
		});
		const walletProvider = walletProviderMap.get(walletProviderKey);
		if (!walletProvider || !require_updateAuthFromVerifyResponse.isWaasWalletProvider(walletProvider)) return Promise.resolve();
		return walletProvider.createWalletAccount({
			bitcoinConfig: requirement.bitcoinConfig,
			password,
			thresholdSignatureScheme
		});
	}));
	await require_isDynamicWaasEnabled.__refreshAuth_wrapped(client);
};

//#endregion
//#region src/modules/waas/delegateWaasKeyShares/delegateWaasKeyShares.ts
/**
* Delegates cryptographic key shares for a WaaS wallet account.
*
* This function allows the delegation of key management responsibilities,
* enabling distributed key management for enhanced security.
*
* @param params.walletAccount - The WaaS wallet account to delegate shares for.
* @param [params.password] - Optional password for key share operations.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the key delegation is complete.
* @notInstrumented
*/
const delegateWaasKeyShares = async ({ password, walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	await require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).delegateKeyShares({
		password,
		walletAccount
	});
	await require_isDynamicWaasEnabled.__refreshAuth_wrapped(client);
};

//#endregion
//#region src/modules/waas/exportWaasClientKeyshares/exportWaasClientKeyshares.ts
/**
* Exports the client-side key shares for a WaaS wallet account.
*
* This function retrieves the cryptographic key shares that are stored
* on the client side for backup or migration purposes.
*
* @param params.walletAccount - The WaaS wallet account to export shares from.
* @param [params.password] - Optional password to encrypt the exported shares.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves to the exported key shares.
* @notInstrumented
*/
const exportWaasClientKeyshares = async ({ password, walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).exportClientKeyshares({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/waas/exportWaasPrivateKey/exportWaasPrivateKey.ts
/**
* Exports the private key for a WaaS wallet account with secure display.
*
* This function reveals the private key for a WaaS wallet and displays it
* securely within an iframe dynamically inserted into the specified HTML container element.
*
* @param params.walletAccount - The WaaS wallet account to export the private key from.
* @param params.displayContainer - The HTML element where the iframe will be injected.
* @param [params.password] - Optional password to decrypt the private key.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the private key export is complete.
* @notInstrumented
*/
const exportWaasPrivateKey = async ({ displayContainer, password, walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).exportPrivateKey({
		displayContainer,
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/wallets/userHasEmbeddedWalletForChain/userHasEmbeddedWalletForChain.ts
/** @notInstrumented */
const userHasEmbeddedWalletForChain = ({ user, chain }) => {
	return user.verifiedCredentials.some((vc) => vc.walletProvider === _dynamic_labs_sdk_api_core.WalletProviderEnum.EmbeddedWallet && vc.chain && require_updateAuthFromVerifyResponse.getChainFromVerifiedCredentialChain(vc.chain) === chain);
};

//#endregion
//#region src/modules/waas/getChainsMissingWaasWalletAccounts/getChainsMissingWaasWalletAccounts.ts
/**
* Gets all chains missing a waas wallet account for the current user.
*
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns An array of all chains that have waas wallet accounts enabled
* but for which the current user does not yet have an embedded wallet account.
* @notInstrumented
*/
const getChainsMissingWaasWalletAccounts = (client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const projectSettings = client.projectSettings;
	require_updateAuthFromVerifyResponse.assertDefined(projectSettings, "Project settings are not available");
	const embeddedWalletSettings = projectSettings.sdk.embeddedWallets;
	const isWaasEnabled = require_isDynamicWaasEnabled.isDynamicWaasEnabled(client);
	const user = client.user;
	if (!user || !isWaasEnabled) return [];
	return (embeddedWalletSettings?.chainConfigurations?.filter((chain) => chain.enabled) ?? []).map((chain) => chain.name).filter((chain) => !userHasEmbeddedWalletForChain({
		chain,
		user
	}));
};

//#endregion
//#region src/modules/waas/getWalletRecoveryState/getWalletRecoveryState.ts
/**
* Retrieves the recovery state of a WaaS wallet account.
*
* @param params.walletAccount - The WaaS wallet account to get the recovery state for.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves with the wallet recovery state.
* @notInstrumented
*/
const getWalletRecoveryState = async ({ walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).getWalletRecoveryState({ walletAccount });
};

//#endregion
//#region src/modules/waas/hasDelegatedAccess/hasDelegatedAccess.ts
/**
* Checks if a WaaS wallet account has delegated access.
*
* @param params.walletAccount - The WaaS wallet account to check for delegated access.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns True if the wallet account has delegated access, false otherwise.
* @notInstrumented
*/
const hasDelegatedAccess = ({ walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const verifiedCredential = require_getWalletProviderByKey.getVerifiedCredentialForWalletAccount({ walletAccount }, client);
	require_updateAuthFromVerifyResponse.assertDefined(verifiedCredential, "Verified credential not found for Waas wallet account");
	return !!verifiedCredential.walletProperties?.keyShares?.some((keyShare) => keyShare.backupLocation === "delegated");
};

//#endregion
//#region src/modules/waas/getWaasWalletProviderByChain/getWaasWalletProviderByChain.ts
/** @notInstrumented */
const getWaasWalletProviderByChain = ({ chain }, client) => {
	const waasProvider = require_updateAuthFromVerifyResponse.findWaasWalletProviderByChain({ chain }, client);
	if (!waasProvider) throw new require_updateAuthFromVerifyResponse.NoWalletProviderFoundError({ walletProviderKey: require_updateAuthFromVerifyResponse.formatWalletProviderKey({
		chain,
		displayName: require_updateAuthFromVerifyResponse.DYNAMIC_WAAS_METADATA.displayName,
		walletProviderType: _dynamic_labs_sdk_api_core.WalletProviderEnum.EmbeddedWallet
	}) });
	return waasProvider;
};

//#endregion
//#region src/modules/waas/importWaasPrivateKey/importWaasPrivateKey.ts
/**
* Imports an existing private key to create a new WaaS wallet account.
*
* This function allows users to import their existing private key into
* Dynamic's WaaS infrastructure for secure management.
*
* @param params.chain - The chain for the imported wallet.
* @param params.privateKey - The private key to import.
* @param [params.thresholdSignatureScheme] - Optional threshold signature scheme configuration. Default is TWO_OF_TWO.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the private key is successfully imported.
* @notInstrumented
*/
const importWaasPrivateKey = async ({ chain, privateKey, thresholdSignatureScheme }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return getWaasWalletProviderByChain({ chain }, client).importPrivateKey({
		privateKey,
		thresholdSignatureScheme
	});
};

//#endregion
//#region src/modules/waas/isPasswordRequiredForWaasWallets/isPasswordRequiredForWaasWallets.ts
/**
* Checks if the user Waas wallets are password protected.
*
* If the user's Waas wallets are password protected, it means that user's will have to be prompted for a
* password for the first wallet action that happens on each session.
* The password is also required for creating any new Waas wallets.
*
* @param client - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns True if the user has any password protected Waas wallets, false otherwise
* @notInstrumented
*/
const isPasswordRequiredForWaasWallets = (client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	const { user } = client;
	require_updateAuthFromVerifyResponse.assertDefined(user, "User is not authenticated");
	return user.verifiedCredentials.some((credential) => credential.walletProperties?.keyShares?.some((keyShare) => keyShare.passwordEncrypted));
};

//#endregion
//#region src/modules/waas/refreshWaasWalletAccountShares/refreshWaasWalletAccountShares.ts
/**
* Refreshes the cryptographic key shares for a WaaS wallet account.
*
* This function generates new key shares for the wallet while maintaining
* the same threshold signature scheme.
*
* @param params.walletAccount - The WaaS wallet account to refresh shares for.
* @param [params.password] - Optional password for key share operations.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the key shares are refreshed.
* @notInstrumented
*/
const refreshWaasWalletAccountShares = async ({ walletAccount, password }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).refreshWalletAccountShares({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/waas/revokeWaasDelegation/revokeWaasDelegation.ts
/**
* Revokes the delegation of a WaaS wallet account.
*
* @param params.walletAccount - The WaaS wallet account to revoke delegation for.
* @param [params.password] - Optional password for delegation operations.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the delegation is revoked.
* @notInstrumented
*/
const revokeWaasDelegation = async ({ password, walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	await require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).revokeDelegation({
		password,
		walletAccount
	});
	await require_isDynamicWaasEnabled.__refreshAuth_wrapped(client);
};

//#endregion
//#region src/modules/waas/setWaasWalletAccountPassword/setWaasWalletAccountPassword.ts
/**
* Sets a password for a WaaS wallet account.
*
* @param params.walletAccount - The WaaS wallet account to set the password for.
* @param params.password - The password to set for the wallet.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the password is successfully set.
* @notInstrumented
*/
const setWaasWalletAccountPassword = async ({ password, walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).setWaasWalletAccountPassword({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/wallets/userHasExternalWallet/userHasExternalWallet.ts
/** @notInstrumented */
const userHasExternalWallet = (user) => {
	return user?.verifiedCredentials.some((vc) => vc.format === _dynamic_labs_sdk_api_core.JwtVerifiedCredentialFormatEnum.Blockchain && vc.walletProvider !== _dynamic_labs_sdk_api_core.WalletProviderEnum.EmbeddedWallet);
};

//#endregion
//#region src/modules/waas/shouldAutoCreateWalletForChain/shouldAutoCreateWalletForChain.ts
/**
* Checks if a WaaS wallet should be automatically created for the specified blockchain.
*
* This function determines whether the current user configuration and project
* settings require automatic wallet creation for the given chain, considering
* whether the user already has an embedded wallet for the enabled chains or an external wallet.
*
* @param params.chain - The blockchain network to check.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns True if a wallet should be auto-created for the chain, false otherwise.
* @notInstrumented
*/
const shouldAutoCreateWalletForChain = ({ chain }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	require_updateAuthFromVerifyResponse.assertDefined(client.projectSettings, "Project settings are not available");
	const embeddedWalletSettings = client.projectSettings.sdk.embeddedWallets;
	if (!embeddedWalletSettings?.automaticEmbeddedWalletCreation || !client.user) return false;
	const automaticEmbeddedWalletCreationForExternalEnabled = embeddedWalletSettings?.automaticEmbeddedWalletCreationForExternal;
	if (!(!userHasExternalWallet(client.user) || automaticEmbeddedWalletCreationForExternalEnabled)) return false;
	return getChainsMissingWaasWalletAccounts(client).includes(chain);
};

//#endregion
//#region src/modules/waas/unlockWallet/unlockWallet.ts
/**
* Unlocks a WaaS wallet account.
*
* @param params.walletAccount - The WaaS wallet account to unlock.
* @param params.password - The password to unlock the wallet.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves with the wallet information.
* @notInstrumented
*/
const unlockWallet = async ({ password, walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).unlockWallet({
		password,
		walletAccount
	});
};

//#endregion
//#region src/modules/waas/updateWaasPassword/updateWaasPassword.ts
/**
* Updates the password for a WaaS wallet account.
*
* This function changes the password used to encrypt and protect the
* cryptographic key shares for the specified wallet account.
*
* @param params.walletAccount - The WaaS wallet account to update the password for.
* @param params.currentPassword - The current password for the wallet.
* @param params.newPassword - The new password to set for the wallet.
* @param [client] - The Dynamic client instance. Only required when using multiple Dynamic clients.
* @returns A promise that resolves when the password is successfully updated.
* @notInstrumented
*/
const updateWaasPassword = async ({ currentPassword, newPassword, walletAccount }, client = require_updateAuthFromVerifyResponse.getDefaultClient()) => {
	return require_migrateFromFireblocks.getWaasWalletProviderFromWalletAccount({ walletAccount }, client).updatePassword({
		currentPassword,
		newPassword,
		walletAccount
	});
};

//#endregion
//#region src/exports/waas.ts
(0, _dynamic_labs_sdk_assert_package_version.assertPackageVersion)(require_updateAuthFromVerifyResponse.name, require_updateAuthFromVerifyResponse.version);

//#endregion
exports.NotWaasWalletProviderError = require_migrateFromFireblocks.NotWaasWalletProviderError;
exports.backupWaasKeySharesToGoogleDrive = backupWaasKeySharesToGoogleDrive;
exports.createWaasWalletAccounts = createWaasWalletAccounts;
exports.delegateWaasKeyShares = delegateWaasKeyShares;
exports.exportWaasClientKeyshares = exportWaasClientKeyshares;
exports.exportWaasPrivateKey = exportWaasPrivateKey;
exports.getChainsMissingWaasWalletAccounts = getChainsMissingWaasWalletAccounts;
exports.getWalletRecoveryState = getWalletRecoveryState;
exports.hasDelegatedAccess = hasDelegatedAccess;
exports.importWaasPrivateKey = importWaasPrivateKey;
exports.isDynamicWaasEnabled = require_isDynamicWaasEnabled.isDynamicWaasEnabled;
exports.isPasswordRequiredForWaasWallets = isPasswordRequiredForWaasWallets;
exports.isWaasWalletAccount = require_updateAuthFromVerifyResponse.isWaasWalletAccount;
exports.isWalletCreationRequirement = isWalletCreationRequirement;
exports.migrateFromFireblocks = require_migrateFromFireblocks.migrateFromFireblocks;
exports.normalizeWalletRequirements = normalizeWalletRequirements;
exports.refreshWaasWalletAccountShares = refreshWaasWalletAccountShares;
exports.revokeWaasDelegation = revokeWaasDelegation;
exports.setWaasWalletAccountPassword = setWaasWalletAccountPassword;
exports.shouldAutoCreateWalletForChain = shouldAutoCreateWalletForChain;
exports.unlockWallet = unlockWallet;
exports.updateWaasPassword = updateWaasPassword;
//# sourceMappingURL=waas.cjs.map