0
  5   ˆl–ßi~” T¾R( 	ÅÍÆœ¸ØÅ£�þB gâ „Óî€ÏÂƒh¹ÁÀ¿      0
  6   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þDËÑ¬ã�f1� nFƒüÿÃƒ|¿ÂÁÀ #‹    <!-- Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/ -->

<script setup lang="ts">
import { computed, watch } from 'vue'
import { useRouter } from 'vue-router'

import {
  NotificationTypes,
  useNotifications,
} from '#shared/components/CommonNotifications/index.ts'
import { useApplicationConfigTwoFactor } from '#shared/composables/authentication/useApplicationConfigTwoFactor.ts'
import type { TwoFactorActionTypes } from '#shared/entities/two-factor/types.ts'
import { useUserCurrentTwoFactorRemoveMethodMutation } from '#shared/entities/user/current/graphql/mutations/two-factor/userCurrentTwoFactorRemoveMethod.api.ts'
import { useUserCurrentTwoFactorSetDefaultMethodMutation } from '#shared/entities/user/current/graphql/mutations/two-factor/userCurrentTwoFactorSetDefaultMethod.api.ts'
import { ErrorRouteType, redirectErrorRoute } from '#shared/router/error.ts'
import { MutationHandler } from '#shared/server/apollo/handler/index.ts'
import { useSessionStore } from '#shared/stores/session.ts'
import { ErrorStatusCodes } from '#shared/types/error.ts'
import type { ObjectLike } from '#shared/types/utils.ts'

import CommonActionMenu from '#desktop/components/CommonActionMenu/CommonActionMenu.vue'
import CommonButton from '#desktop/components/CommonButton/CommonButton.vue'
import { useFlyout } from '#desktop/components/CommonFlyout/useFlyout.ts'
import type { MenuItem } from '#desktop/components/CommonPopoverMenu/types.ts'
import LayoutContent from '#desktop/components/layout/LayoutContent.vue'
import type { TwoFactorConfigurationType } from '#desktop/components/TwoFactor/types.ts'
import { useConfigurationTwoFactor } from '#desktop/entities/two-factor-configuration/composables/useConfigurationTwoFactor.ts'

import { useBreadcrumb } from '../composables/useBreadcrumb.ts'
import { usePersonalSettingTabs } from '../composables/usePersonalSettingTabs.ts'

defineOptions({
  beforeRouteEnter() {
    const { hasEnabledMethods } = useApplicationConfigTwoFactor()

    if (!hasEnabledMethods.value)
      return redirectErrorRoute({
        type: ErrorRouteType.AuthenticatedError,
        title: __('Forbidden'),
        message: __('There are no enabled two-factor authentication methods.'),
        statusCode: ErrorStatusCodes.Forbidden,
      })

    return true
  },
})

const session = useSessionStore()
const router = useRouter()
const { notify } = useNotifications()

const {
  hasEnabledMethods,
  hasEnabledRecoveryCodes,
  hasConfiguredMethods,
  hasRecoveryCodes,
  twoFactorConfigurationMethods,
} = useConfigurationTwoFactor()

watch(hasEnabledMethods, (newValue) => {
  if (newValue) return

  router.replace(
    redirectErrorRoute({
      type: ErrorRouteType.AuthenticatedError,
      title: __('Forbidden'),
      message: __('There are no enabled two-factor authentication methods.'),
      statusCode: ErrorStatusCodes.Forbidden,
    }),
  )
})

const { breadcrumbItems } = useBreadcrumb()

const twoFactorConfigurationFlyout = useFlyout({
  name: 'two-factor-flyout',
  component: () => import('#desktop/components/TwoFactor/TwoFactorConfigurationFlyout.vue'),
})

const openTwoFactorConfigurationFlyout = async (type: TwoFactorConfigurationType) => {
  return twoFactorConfigurationFlyout.open({
    type,
  })
}

const setDefaultTwoFactorMethod = new MutationHandler(
  useUserCurrentTwoFactorSetDefaultMethodMutation(),
  {
    errorNotificationMessage: __('Could not set two-factor authentication method as default'),
  },
)

const submitTwoFactorDefaultMethod = (entity?: ObjectLike) => {
  if (!entity) return

  setDefaultTwoFactorMethod
    .send({
      methodName: entity.name,
    })
    .then(() => {
      session.setUserPreference('two_factor_authentication', {
        ...session.user?.preferences?.two_factor_authentication,
        default: entity.name,
      })

      notify({
        id: 'two-factor-method-set-default',
        type: NotificationTypes.Success,