# Microsoft Graph Delegated permissions and Application permissions


import re

permissions = [
	'AccessReview.Read.All', 'AccessReview.ReadWrite.All', 'AccessReview.ReadWrite.Membership', 'Acronym.Read.All', 'AdministrativeUnit.Read.All', 'AdministrativeUnit.ReadWrite.All', 'AgreementAcceptance.Read', 'AgreementAcceptance.Read.All', 'Agreement.Read.All', 'Agreement.ReadWrite.All', 'Analytics.Read', 'APIConnectors.Read.All',
	'APIConnectors.ReadWrite.All', 'AppCatalog.Read.All', 'AppCatalog.ReadWrite.All', 'AppCatalog.Submit', 'AppCertTrustConfiguration.Read.All', 'AppCertTrustConfiguration.ReadWrite.All', 'Application-RemoteDesktopConfig.ReadWrite.All', 'Application.Read.All', 'Application.ReadWrite.All', 'AppRoleAssignment.ReadWrite.All', 'ApprovalSolution.Read', 'ApprovalSolution.ReadWrite',
	'ApprovalSolutionResponse.ReadWrite', 'AttackSimulation.Read.All', 'AttackSimulation.ReadWrite.All', 'AuditLog.Read.All', 'AuditLogsQuery-CRM.Read.All', 'AuditLogsQuery-Endpoint.Read.All', 'AuditLogsQuery-Entra.Read.All', 'AuditLogsQuery-Exchange.Read.All', 'AuditLogsQuery-OneDrive.Read.All', 'AuditLogsQuery-SharePoint.Read.All', 'AuditLogsQuery.Read.All', 'AuthenticationContext.Read.All',
	'AuthenticationContext.ReadWrite.All', 'BackupRestore-Configuration.Read.All', 'BackupRestore-Configuration.ReadWrite.All', 'BackupRestore-Control.Read.All', 'BackupRestore-Control.ReadWrite.All', 'BackupRestore-Monitor.Read.All', 'BackupRestore-Restore.Read.All', 'BackupRestore-Restore.ReadWrite.All', 'BackupRestore-Search.Read.All', 'BillingConfiguration.ReadWrite.All', 'BitlockerKey.Read.All', 'BitlockerKey.ReadBasic.All',
	'BookingsAppointment.ReadWrite.All', 'Bookings.Manage.All', 'Bookings.Read.All', 'Bookings.ReadWrite.All', 'Bookmark.Read.All', 'BrowserSiteLists.Read.All', 'BrowserSiteLists.ReadWrite.All', 'BusinessScenarioConfig.Read.All', 'BusinessScenarioConfig.Read.OwnedBy', 'BusinessScenarioConfig.ReadWrite.All', 'BusinessScenarioConfig.ReadWrite.OwnedBy', 'BusinessScenarioData.Read.OwnedBy',
	'BusinessScenarioData.ReadWrite.OwnedBy', 'Calendars.Read', 'Calendars.Read.Shared', 'Calendars.ReadBasic', 'Calendars.ReadWrite', 'Calendars.ReadWrite.Shared', 'CallEvents.Read', 'ChangeManagement.Read.All', 'Channel.Create', 'Channel.Delete.All', 'Channel.ReadBasic.All', 'ChannelMember.Read.All',
	'ChannelMember.ReadWrite.All', 'ChannelMessage.Edit', 'ChannelMessage.Read.All', 'ChannelMessage.ReadWrite', 'ChannelMessage.Send', 'ChannelSettings.Read.All', 'ChannelSettings.ReadWrite.All', 'Chat.Create', 'Chat.ManageDeletion.All', 'Chat.Read', 'Chat.ReadBasic', 'Chat.ReadWrite',
	'Chat.ReadWrite.All', 'ChatMember.Read', 'ChatMember.ReadWrite', 'ChatMessage.Read', 'ChatMessage.Send', 'CloudApp-Discovery.Read.All', 'CloudPC.Read.All', 'CloudPC.ReadWrite.All', 'Community.Read.All', 'Community.ReadWrite.All', 'ConsentRequest.Create', 'ConsentRequest.Read',
	'ConsentRequest.Read.All', 'ConsentRequest.ReadApprove.All', 'ConsentRequest.ReadWrite.All', 'Contacts.Read', 'Contacts.Read.Shared', 'Contacts.ReadWrite', 'Contacts.ReadWrite.Shared', 'CrossTenantInformation.ReadBasic.All', 'CrossTenantUserProfileSharing.Read', 'CrossTenantUserProfileSharing.Read.All', 'CrossTenantUserProfileSharing.ReadWrite', 'CrossTenantUserProfileSharing.ReadWrite.All',
	'CustomAuthenticationExtension.Read.All', 'CustomAuthenticationExtension.ReadWrite.All', 'CustomDetection.Read.All', 'CustomDetection.ReadWrite.All', 'CustomSecAttributeAssignment.Read.All', 'CustomSecAttributeAssignment.ReadWrite.All', 'CustomSecAttributeAuditLogs.Read.All', 'CustomSecAttributeDefinition.Read.All', 'CustomSecAttributeDefinition.ReadWrite.All', 'CustomSecAttributeProvisioning.Read.All', 'CustomSecAttributeProvisioning.ReadWrite.All', 'CustomTags.Read.All',
	'CustomTags.ReadWrite.All', 'DelegatedAdminRelationship.Read.All', 'DelegatedAdminRelationship.ReadWrite.All', 'DelegatedPermissionGrant.Read.All', 'DelegatedPermissionGrant.ReadWrite.All', 'Device.Command', 'Device.Read', 'Device.Read.All', 'DeviceLocalCredential.Read.All', 'DeviceLocalCredential.ReadBasic.All', 'DeviceManagementApps.Read.All', 'DeviceManagementApps.ReadWrite.All',
	'DeviceManagementCloudCA.Read.All', 'DeviceManagementCloudCA.ReadWrite.All', 'DeviceManagementConfiguration.Read.All', 'DeviceManagementConfiguration.ReadWrite.All', 'DeviceManagementManagedDevices.PrivilegedOperations.All', 'DeviceManagementManagedDevices.Read.All', 'DeviceManagementManagedDevices.ReadWrite.All', 'DeviceManagementRBAC.Read.All', 'DeviceManagementRBAC.ReadWrite.All', 'DeviceManagementServiceConfig.Read.All', 'DeviceManagementServiceConfig.ReadWrite.All', 'Directory.AccessAsUser.All',
	'Directory.Read.All', 'Directory.ReadWrite.All', 'DirectoryRecommendations.Read.All', 'DirectoryRecommendations.ReadWrite.All', 'Domain.Read.All', 'Domain.ReadWrite.All', 'EAS.AccessAsUser.All', 'eDiscovery.Read.All', 'eDiscovery.ReadWrite.All', 'EduAdministration.Read', 'EduAdministration.ReadWrite', 'EduAssignments.Read',
	'EduAssignments.ReadBasic', 'EduAssignments.ReadWrite', 'EduAssignments.ReadWriteBasic', 'EduCurricula.Read', 'EduCurricula.ReadWrite', 'EduRoster.Read', 'EduRoster.ReadBasic', 'EduRoster.ReadWrite', 'EntitlementManagement.Read.All', 'EntitlementManagement.ReadWrite.All', 'EntitlementMgmt-SubjectAccess.ReadWrite', 'EventListener.Read.All',
	'EventListener.ReadWrite.All', 'EWS.AccessAsUser.All', 'ExternalConnection.Read.All', 'ExternalConnection.ReadWrite.All', 'ExternalConnection.ReadWrite.OwnedBy', 'ExternalItem.Read.All', 'ExternalItem.ReadWrite.All', 'ExternalItem.ReadWrite.OwnedBy', 'ExternalUserProfile.Read.All', 'ExternalUserProfile.ReadWrite.All', 'Family.Read', 'Files.Read',
	'Files.Read.All', 'Files.Read.Selected', 'Files.ReadWrite', 'Files.ReadWrite.All', 'Files.ReadWrite.AppFolder', 'Files.ReadWrite.Selected', 'Files.SelectedOperations.Selected', 'FileStorageContainer.Selected', 'Financials.ReadWrite.All', 'Goals-Export.Read.All', 'Goals-Export.ReadWrite.All', 'Group-Conversation.Read.All',
	'Group-Conversation.ReadWrite.All', 'Group.Read.All', 'Group.ReadWrite.All', 'GroupMember.Read.All', 'GroupMember.ReadWrite.All', 'HealthMonitoringAlertConfig.Read.All', 'HealthMonitoringAlertConfig.ReadWrite.All', 'HealthMonitoringAlert.Read.All', 'HealthMonitoringAlert.ReadWrite.All', 'IdentityProvider.Read.All', 'IdentityProvider.ReadWrite.All', 'IdentityRiskEvent.Read.All',
	'IdentityRiskEvent.ReadWrite.All', 'IdentityRiskyServicePrincipal.Read.All', 'IdentityRiskyServicePrincipal.ReadWrite.All', 'IdentityRiskyUser.Read.All', 'IdentityRiskyUser.ReadWrite.All', 'IdentityUserFlow.Read.All', 'IdentityUserFlow.ReadWrite.All', 'IMAP.AccessAsUser.All', 'IndustryData-DataConnector.Read.All', 'IndustryData-DataConnector.ReadWrite.All', 'IndustryData-DataConnector.Upload', 'IndustryData-InboundFlow.Read.All',
	'IndustryData-InboundFlow.ReadWrite.All', 'IndustryData-OutboundFlow.Read.All', 'IndustryData-OutboundFlow.ReadWrite.All', 'IndustryData-ReferenceDefinition.Read.All', 'IndustryData-ReferenceDefinition.ReadWrite.All', 'IndustryData-Run.Read.All', 'IndustryData-SourceSystem.Read.All', 'IndustryData-SourceSystem.ReadWrite.All', 'IndustryData-TimePeriod.Read.All', 'IndustryData-TimePeriod.ReadWrite.All', 'IndustryData.ReadBasic.All', 'InformationProtectionConfig.Read',
	'InformationProtectionPolicy.Read', 'Insights-UserMetric.Read.All', 'LearningAssignedCourse.Read', 'LearningContent.Read.All', 'LearningContent.ReadWrite.All', 'LearningProvider.Read', 'LearningProvider.ReadWrite', 'LearningSelfInitiatedCourse.Read', 'LicenseAssignment.ReadWrite.All', 'LifecycleWorkflows.Read.All', 'LifecycleWorkflows.ReadWrite.All', 'ListItems.SelectedOperations.Selected',
	'Lists.SelectedOperations.Selected', 'MailboxFolder.Read', 'MailboxFolder.ReadWrite', 'MailboxItem.ImportExport', 'MailboxItem.Read', 'MailboxSettings.Read', 'MailboxSettings.ReadWrite', 'Mail.Read', 'Mail.Read.Shared', 'Mail.ReadBasic', 'Mail.ReadBasic.Shared', 'Mail.ReadWrite',
	'Mail.ReadWrite.Shared', 'Mail.Send', 'Mail.Send.Shared', 'ManagedTenants.Read.All', 'ManagedTenants.ReadWrite.All', 'Member.Read.Hidden', 'MultiTenantOrganization.Read.All', 'MultiTenantOrganization.ReadBasic.All', 'MultiTenantOrganization.ReadWrite.All', 'NetworkAccess-Reports.Read.All', 'NetworkAccessBranch.Read.All', 'NetworkAccessBranch.ReadWrite.All',
	'NetworkAccess.Read.All', 'NetworkAccess.ReadWrite.All', 'NetworkAccessPolicy.Read.All', 'NetworkAccessPolicy.ReadWrite.All', 'Notes.Create', 'Notes.Read', 'Notes.Read.All', 'Notes.ReadWrite', 'Notes.ReadWrite.All', 'Notes.ReadWrite.CreatedByApp', 'Notifications.ReadWrite.CreatedByApp', 'OnlineMeetingAiInsight.Read.All',
	'OnlineMeetingArtifact.Read.All', 'OnlineMeetingRecording.Read.All', 'OnlineMeetings.Read', 'OnlineMeetings.ReadWrite', 'OnlineMeetingTranscript.Read.All', 'OnPremDirectorySynchronization.Read.All', 'OnPremDirectorySynchronization.ReadWrite.All', 'OnPremisesPublishingProfiles.ReadWrite.All', 'OrganizationalBranding.Read.All', 'OrganizationalBranding.ReadWrite.All', 'Organization.Read.All', 'Organization.ReadWrite.All',
	'OrgContact.Read.All', 'OrgSettings-AppsAndServices.Read.All', 'OrgSettings-AppsAndServices.ReadWrite.All', 'OrgSettings-DynamicsVoice.Read.All', 'OrgSettings-DynamicsVoice.ReadWrite.All', 'OrgSettings-Forms.Read.All', 'OrgSettings-Forms.ReadWrite.All', 'OrgSettings-Microsoft365Install.Read.All', 'OrgSettings-Microsoft365Install.ReadWrite.All', 'OrgSettings-Todo.Read.All', 'OrgSettings-Todo.ReadWrite.All', 'PartnerBilling.Read.All',
	'PartnerSecurity.Read.All', 'PartnerSecurity.ReadWrite.All', 'PendingExternalUserProfile.Read.All', 'PendingExternalUserProfile.ReadWrite.All', 'People.Read', 'People.Read.All', 'PeopleSettings.Read.All', 'PeopleSettings.ReadWrite.All', 'PlaceDevice.Read.All', 'PlaceDevice.ReadWrite.All', 'Place.Read.All', 'Place.ReadWrite.All',
	'Policy.Read.All', 'Policy.Read.ConditionalAccess', 'Policy.Read.DeviceConfiguration', 'Policy.Read.IdentityProtection', 'Policy.Read.PermissionGrant', 'Policy.ReadWrite.AccessReview', 'Policy.ReadWrite.ApplicationConfiguration', 'Policy.ReadWrite.AuthenticationFlows', 'Policy.ReadWrite.AuthenticationMethod', 'Policy.ReadWrite.Authorization', 'Policy.ReadWrite.ConditionalAccess', 'Policy.ReadWrite.ConsentRequest',
	'Policy.ReadWrite.CrossTenantAccess', 'Policy.ReadWrite.DeviceConfiguration', 'Policy.ReadWrite.ExternalIdentities', 'Policy.ReadWrite.FeatureRollout', 'Policy.ReadWrite.FedTokenValidation', 'Policy.ReadWrite.IdentityProtection', 'Policy.ReadWrite.MobilityManagement', 'Policy.ReadWrite.PermissionGrant', 'Policy.ReadWrite.SecurityDefaults', 'Policy.ReadWrite.TrustFramework', 'POP.AccessAsUser.All', 'Presence.Read',
	'Presence.Read.All', 'Presence.ReadWrite', 'PrintConnector.Read.All', 'PrintConnector.ReadWrite.All', 'Printer.Create', 'Printer.FullControl.All', 'Printer.Read.All', 'Printer.ReadWrite.All', 'PrinterShare.Read.All', 'PrinterShare.ReadBasic.All', 'PrinterShare.ReadWrite.All', 'PrintJob.Create',
	'PrintJob.Read', 'PrintJob.Read.All', 'PrintJob.ReadBasic', 'PrintJob.ReadBasic.All', 'PrintJob.ReadWrite', 'PrintJob.ReadWrite.All', 'PrintJob.ReadWriteBasic', 'PrintJob.ReadWriteBasic.All', 'PrintSettings.Read.All', 'PrintSettings.ReadWrite.All', 'PrivilegedAccess.Read.AzureAD', 'PrivilegedAccess.Read.AzureADGroup',
	'PrivilegedAccess.Read.AzureResources', 'PrivilegedAccess.ReadWrite.AzureAD', 'PrivilegedAccess.ReadWrite.AzureADGroup', 'PrivilegedAccess.ReadWrite.AzureResources', 'PrivilegedAssignmentSchedule.Read.AzureADGroup', 'PrivilegedAssignmentSchedule.ReadWrite.AzureADGroup', 'PrivilegedAssignmentSchedule.Remove.AzureADGroup', 'PrivilegedEligibilitySchedule.Read.AzureADGroup', 'PrivilegedEligibilitySchedule.ReadWrite.AzureADGroup', 'PrivilegedEligibilitySchedule.Remove.AzureADGroup', 'ProfilePhoto.Read.All', 'ProfilePhoto.ReadWrite.All',
	'ProgramControl.Read.All', 'ProgramControl.ReadWrite.All', 'PublicKeyInfrastructure.Read.All', 'PublicKeyInfrastructure.ReadWrite.All', 'QnA.Read.All', 'RecordsManagement.Read.All', 'RecordsManagement.ReadWrite.All', 'ReportSettings.Read.All', 'ReportSettings.ReadWrite.All', 'Reports.Read.All', 'ResourceSpecificPermissionGrant.ReadForChat', 'ResourceSpecificPermissionGrant.ReadForTeam',
	'ResourceSpecificPermissionGrant.ReadForUser', 'RoleAssignmentSchedule.Read.Directory', 'RoleAssignmentSchedule.ReadWrite.Directory', 'RoleAssignmentSchedule.Remove.Directory', 'RoleEligibilitySchedule.Read.Directory', 'RoleEligibilitySchedule.ReadWrite.Directory', 'RoleEligibilitySchedule.Remove.Directory', 'RoleManagementAlert.Read.Directory', 'RoleManagementAlert.ReadWrite.Directory', 'RoleManagementPolicy.Read.AzureADGroup', 'RoleManagementPolicy.Read.Directory', 'RoleManagementPolicy.ReadWrite.AzureADGroup',
	'RoleManagementPolicy.ReadWrite.Directory', 'RoleManagement.Read.All', 'RoleManagement.Read.CloudPC', 'RoleManagement.Read.Directory', 'RoleManagement.Read.Exchange', 'RoleManagement.ReadWrite.CloudPC', 'RoleManagement.ReadWrite.Directory', 'RoleManagement.ReadWrite.Exchange', 'SchedulePermissions.ReadWrite.All', 'Schedule.Read.All', 'Schedule.ReadWrite.All', 'SearchConfiguration.Read.All',
	'SearchConfiguration.ReadWrite.All', 'SecurityActions.Read.All', 'SecurityActions.ReadWrite.All', 'SecurityAlert.Read.All', 'SecurityAlert.ReadWrite.All', 'SecurityAnalyzedMessage.Read.All', 'SecurityAnalyzedMessage.ReadWrite.All', 'SecurityEvents.Read.All', 'SecurityEvents.ReadWrite.All', 'SecurityIdentitiesHealth.Read.All', 'SecurityIdentitiesHealth.ReadWrite.All', 'SecurityIdentitiesSensors.Read.All',
	'SecurityIdentitiesSensors.ReadWrite.All', 'SecurityIdentitiesUserActions.Read.All', 'SecurityIdentitiesUserActions.ReadWrite.All', 'SecurityIncident.Read.All', 'SecurityIncident.ReadWrite.All', 'ServiceActivity-Exchange.Read.All', 'ServiceActivity-Microsoft365Web.Read.All', 'ServiceActivity-OneDrive.Read.All', 'ServiceActivity-Teams.Read.All', 'ServiceHealth.Read.All', 'ServiceMessage.Read.All', 'ServiceMessageViewpoint.Write',
	'ServicePrincipalEndpoint.Read.All', 'ServicePrincipalEndpoint.ReadWrite.All', 'SharePointTenantSettings.Read.All', 'SharePointTenantSettings.ReadWrite.All', 'ShortNotes.Read', 'ShortNotes.ReadWrite', 'Sites.FullControl.All', 'Sites.Manage.All', 'Sites.Read.All', 'Sites.ReadWrite.All', 'Sites.Selected', 'SMTP.Send',
	'SpiffeTrustDomain.Read.All', 'SpiffeTrustDomain.ReadWrite.All', 'SubjectRightsRequest.Read.All', 'SubjectRightsRequest.ReadWrite.All', 'Subscription.Read.All', 'SynchronizationData-User.Upload', 'Synchronization.Read.All', 'Synchronization.ReadWrite.All', 'Tasks.Read', 'Tasks.Read.Shared', 'Tasks.ReadWrite', 'Tasks.ReadWrite.Shared',
	'TeamMember.Read.All', 'TeamMember.ReadWrite.All', 'TeamMember.ReadWriteNonOwnerRole.All', 'TeamsActivity.Read', 'TeamsActivity.Send', 'TeamsAppInstallation.ReadForChat', 'TeamsAppInstallation.ReadForTeam', 'TeamsAppInstallation.ReadForUser', 'TeamsAppInstallation.ReadWriteAndConsentForChat', 'TeamsAppInstallation.ReadWriteAndConsentForTeam', 'TeamsAppInstallation.ReadWriteAndConsentForUser', 'TeamsAppInstallation.ReadWriteAndConsentSelfForChat',
	'TeamsAppInstallation.ReadWriteAndConsentSelfForTeam', 'TeamsAppInstallation.ReadWriteAndConsentSelfForUser', 'TeamsAppInstallation.ReadWriteForChat', 'TeamsAppInstallation.ReadWriteForTeam', 'TeamsAppInstallation.ReadWriteForUser', 'TeamsAppInstallation.ReadWriteSelfForChat', 'TeamsAppInstallation.ReadWriteSelfForTeam', 'TeamsAppInstallation.ReadWriteSelfForUser', 'TeamSettings.Read.All', 'TeamSettings.ReadWrite.All', 'TeamsTab.Create', 'TeamsTab.Read.All',
	'TeamsTab.ReadWrite.All', 'TeamsTab.ReadWriteForChat', 'TeamsTab.ReadWriteForTeam', 'TeamsTab.ReadWriteForUser', 'TeamsTab.ReadWriteSelfForChat', 'TeamsTab.ReadWriteSelfForTeam', 'TeamsTab.ReadWriteSelfForUser', 'TeamsUserConfiguration.Read.All', 'Team.Create', 'Team.ReadBasic.All', 'TeamTemplates.Read', 'TeamworkAppSettings.Read.All',
	'TeamworkAppSettings.ReadWrite.All', 'TeamworkDevice.Read.All', 'TeamworkDevice.ReadWrite.All', 'TeamworkTag.Read', 'TeamworkTag.ReadWrite', 'Teamwork.Read.All', 'TeamworkUserInteraction.Read.All', 'TermStore.Read.All', 'TermStore.ReadWrite.All', 'ThreatAssessment.ReadWrite.All', 'ThreatHunting.Read.All', 'ThreatIndicators.Read.All',
	'ThreatIndicators.ReadWrite.OwnedBy', 'ThreatIntelligence.Read.All', 'ThreatSubmissionPolicy.ReadWrite.All', 'ThreatSubmission.Read', 'ThreatSubmission.Read.All', 'ThreatSubmission.ReadWrite', 'ThreatSubmission.ReadWrite.All', 'Topic.Read.All', 'TrustFrameworkKeySet.Read.All', 'TrustFrameworkKeySet.ReadWrite.All', 'UnifiedGroupMember.Read.AsGuest', 'User-ConvertToInternal.ReadWrite.All',
	'User-LifeCycleInfo.Read.All', 'User-LifeCycleInfo.ReadWrite.All', 'User-Mail.ReadWrite.All', 'User-PasswordProfile.ReadWrite.All', 'User-Phone.ReadWrite.All', 'UserActivity.ReadWrite.CreatedByApp', 'UserAuthenticationMethod.Read', 'UserAuthenticationMethod.Read.All', 'UserAuthenticationMethod.ReadWrite', 'UserAuthenticationMethod.ReadWrite.All', 'UserNotification.ReadWrite.CreatedByApp', 'UserTeamwork.Read',
	'UserTimelineActivity.Write.CreatedByApp', 'User.DeleteRestore.All', 'User.EnableDisableAccount.All', 'User.Export.All', 'User.Invite.All', 'User.ManageIdentities.All', 'User.Read', 'User.Read.All', 'User.ReadBasic.All', 'User.ReadWrite', 'User.ReadWrite.All', 'User.RevokeSessions.All',
	'VirtualAppointmentNotification.Send', 'VirtualAppointment.Read', 'VirtualAppointment.ReadWrite', 'VirtualEvent.Read', 'VirtualEvent.ReadWrite', 'WindowsUpdates.ReadWrite.All', 'WorkforceIntegration.Read.All', 'WorkforceIntegration.ReadWrite.All', 'AiEnterpriseInteraction.Read.All', 'Application.ReadWrite.OwnedBy', 'ApprovalSolution.Read.All', 'ApprovalSolution.ReadWrite.All',
	'Calendars.ReadBasic.All', 'CallEvents-Emergency.Read.All', 'CallEvents.Read.All', 'CallRecord-PstnCalls.Read.All', 'CallRecords.Read.All', 'Calls.AccessMedia.All', 'Calls.Initiate.All', 'Calls.InitiateGroupCall.All', 'Calls.JoinGroupCall.All', 'Calls.JoinGroupCallAsGuest.All', 'ChannelMessage.UpdatePolicyViolation.All', 'Chat.Read.All',
	'Chat.Read.WhereInstalled', 'Chat.ReadBasic.All', 'Chat.ReadBasic.WhereInstalled', 'Chat.ReadWrite.WhereInstalled', 'Chat.UpdatePolicyViolation.All', 'ChatMember.Read.All', 'ChatMember.Read.WhereInstalled', 'ChatMember.ReadWrite.All', 'ChatMember.ReadWrite.WhereInstalled', 'ChatMessage.Read.All', 'CustomAuthenticationExtension.Receive.Payload', 'Device.ReadWrite.All',
	'EduAdministration.Read.All', 'EduAdministration.ReadWrite.All', 'EduAssignments.Read.All', 'EduAssignments.ReadBasic.All', 'EduAssignments.ReadWrite.All', 'EduAssignments.ReadWriteBasic.All', 'EduCurricula.Read.All', 'EduCurricula.ReadWrite.All', 'EduReports-Reading.Read.All', 'EduReports-Reading.ReadAnonymous.All', 'EduReports-Reflect.Read.All', 'EduReports-Reflect.ReadAnonymous.All',
	'EduRoster.Read.All', 'EduRoster.ReadBasic.All', 'EduRoster.ReadWrite.All', 'Group.Create', 'InformationProtectionConfig.Read.All', 'InformationProtectionContent.Sign.All', 'InformationProtectionContent.Write.All', 'InformationProtectionPolicy.Read.All', 'LearningAssignedCourse.Read.All', 'LearningAssignedCourse.ReadWrite.All', 'LearningSelfInitiatedCourse.Read.All', 'LearningSelfInitiatedCourse.ReadWrite.All',
	'MailboxFolder.Read.All', 'MailboxFolder.ReadWrite.All', 'MailboxItem.ImportExport.All', 'MailboxItem.Read.All', 'Mail.ReadBasic.All', 'OnlineMeetingAiInsight.Read.Chat', 'OnlineMeetings.Read.All', 'OnlineMeetings.ReadWrite.All', 'PlaceDeviceTelemetry.ReadWrite.All', 'Presence.ReadWrite.All', 'PrintJob.Manage.All', 'PrintTaskDefinition.ReadWrite.All',
	'ResourceSpecificPermissionGrant.ReadForChat.All', 'ResourceSpecificPermissionGrant.ReadForTeam.All', 'ResourceSpecificPermissionGrant.ReadForUser.All', 'Schedule-WorkingTime.ReadWrite.All', 'ShortNotes.Read.All', 'ShortNotes.ReadWrite.All', 'Tasks.Read.All', 'Tasks.ReadWrite.All', 'TeamsActivity.Read.All', 'TeamsAppInstallation.Read.All', 'TeamsAppInstallation.ReadForChat.All', 'TeamsAppInstallation.ReadForTeam.All',
	'TeamsAppInstallation.ReadForUser.All', 'TeamsAppInstallation.ReadWriteAndConsentForChat.All', 'TeamsAppInstallation.ReadWriteAndConsentForTeam.All', 'TeamsAppInstallation.ReadWriteAndConsentForUser.All', 'TeamsAppInstallation.ReadWriteAndConsentSelfForChat.All', 'TeamsAppInstallation.ReadWriteAndConsentSelfForTeam.All', 'TeamsAppInstallation.ReadWriteAndConsentSelfForUser.All', 'TeamsAppInstallation.ReadWriteForChat.All', 'TeamsAppInstallation.ReadWriteForTeam.All', 'TeamsAppInstallation.ReadWriteForUser.All', 'TeamsAppInstallation.ReadWriteSelfForChat.All', 'TeamsAppInstallation.ReadWriteSelfForTeam.All',
	'TeamsAppInstallation.ReadWriteSelfForUser.All', 'TeamsTab.ReadWriteForChat.All', 'TeamsTab.ReadWriteForTeam.All', 'TeamsTab.ReadWriteForUser.All', 'TeamsTab.ReadWriteSelfForChat.All', 'TeamsTab.ReadWriteSelfForTeam.All', 'TeamsTab.ReadWriteSelfForUser.All', 'TeamTemplates.Read.All', 'TeamworkTag.Read.All', 'TeamworkTag.ReadWrite.All', 'Teamwork.Migrate.All', 'ThreatAssessment.Read.All',
	'UserShiftPreferences.Read.All', 'UserShiftPreferences.ReadWrite.All', 'UserTeamwork.Read.All', 'VirtualAppointment.Read.All', 'VirtualAppointment.ReadWrite.All', 'VirtualEventRegistration-Anon.ReadWrite.All', 'VirtualEvent.Read.All', 				
]


def scope_fmt(permissions=permissions, scopes=None):
    """
    Ensures the provided scopes are correctly formatted and validated 
    against a predefined set of permissions.

    Args:
        permissions (list, optional): A list of valid permission strings. 
                                      Defaults to the module's predefined permissions.
        scopes (str | list | None, optional): The input scope(s) to validate and format. 
                                              Can be a space-separated string or a list of strings.

    Returns:
        list: A list of validated and formatted scope strings.
              Defaults to ['User.Read'] if no scopes are provided.
    """	
    if scopes:
        if isinstance(scopes, list):
            scopes = scopes[0] if len(scopes) == 1 else scopes
        if isinstance(scopes, str):
            match = re.match(r'^(\w+(\.\w+)+)( (\w+(\.\w+)+))*$', scopes)
            if match:
                sorted_permissions = sorted(permissions, key=len, reverse=True)
                permissions_pattern = '|'.join(re.escape(permission) for permission in sorted_permissions)
                pattern = rf'({permissions_pattern})'
                matches = re.finditer(pattern, scopes)
                formatted_scopes = []
                last_end = 0
                for match in matches:
                    start, end = match.span()
                    if start != last_end:
                        formatted_scopes.append(scopes[last_end:start])
                    formatted_scopes.append(match.group(0))
                    last_end = end
                if last_end != len(scopes):
                    formatted_scopes.append(scopes[last_end:])
                scopes = ' '.join(formatted_scopes).split()
        if not isinstance(scopes, list):
            scopes = [scopes]
    else:
        scopes = ['User.Read']
    return scopes


def __dir__():
    return ['scope_fmt']

__all__ = ['scope_fmt']
