import { ExecutionMode, createExecution, getSmartAccountsEnvironment } from "@metamask/smart-accounts-kit";
import { DelegationManager } from "@metamask/smart-accounts-kit/contracts";
//#region extensions/crypto/src/services/advanced-permissions.ts
/**
* Advanced Permissions Service — MetaMask ERC-7715 integration.
*
* For MetaMask WalletConnect users, delegation is handled via Advanced
* Permissions: the dapp requests scoped permissions from MetaMask, which
* signs the delegation internally. The session account (our agent) then
* redeems via the standard DelegationManager.
*
* This is a parallel path to the raw viem delegation in delegation-service.ts.
* The policy gate routes based on wallet type:
*   - MetaMask → Advanced Permissions (this service)
*   - Private key / other wallets → raw delegation (delegation-service.ts)
*
* Uses @metamask/smart-accounts-kit SDK.
*/
const _grantedPermissions = /* @__PURE__ */ new Map();
function storeGrantedPermissions(policyId, permissions) {
	_grantedPermissions.set(policyId, permissions);
}
function getGrantedPermissions(policyId) {
	return _grantedPermissions.get(policyId) ?? null;
}
function clearGrantedPermissions(policyId) {
	_grantedPermissions.delete(policyId);
}
/** Get the DelegationManager address for a chain from the SDK. */
function getSdkDelegationManager(chainId) {
	return getSmartAccountsEnvironment(chainId).DelegationManager;
}
/**
* Build the redeemDelegations calldata using the SDK.
* This encodes the permissionsContext + execution into the correct
* format for the DelegationManager.
*
* The caller sends this as a raw transaction (to: DelegationManager, data: calldata).
*/
function buildRedeemCalldata(permissionsContext, action) {
	const execution = createExecution({
		target: action.target,
		value: action.value,
		callData: action.callData
	});
	return DelegationManager.encode.redeemDelegations({
		delegations: [[]],
		modes: [ExecutionMode.SingleDefault],
		executions: [[execution]]
	});
}
/**
* Check if Advanced Permissions are available for a wallet.
* Returns true if:
* 1. The wallet is connected via WalletConnect or MetaMask provider
* 2. The wallet has an EIP-7702 designation (smart account)
* 3. There are stored granted permissions
*/
function hasAdvancedPermissions(policyId) {
	return _grantedPermissions.has(policyId);
}
/**
* Get the permission context + DelegationManager address for a policy.
* Returns null if no permissions are stored.
*/
function getPermissionContext(policyId) {
	const perms = _grantedPermissions.get(policyId);
	if (!perms || perms.length === 0) return null;
	const first = perms[0];
	if (!first) return null;
	return {
		context: first.context,
		delegationManager: first.signerMeta.delegationManager
	};
}
//#endregion
export { buildRedeemCalldata, clearGrantedPermissions, getGrantedPermissions, getPermissionContext, getSdkDelegationManager, hasAdvancedPermissions, storeGrantedPermissions };

//# sourceMappingURL=advanced-permissions.mjs.map