'use client'
import type { Placement } from '@langgenius/dify-ui/dropdown-menu'
import type { FC } from 'react'
import type { SiteInfo } from '@/models/share'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLinkItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from '@langgenius/dify-ui/dropdown-menu'
import * as React from 'react'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'
import ActionButton from '@/app/components/base/action-button'
import ThemeSwitcher from '@/app/components/base/theme-switcher'
import { useWebAppStore } from '@/context/web-app-context'
import { AccessMode } from '@/models/access-control'
import { usePathname, useRouter } from '@/next/navigation'
import { webAppLogout } from '@/service/webapp-auth'
import InfoModal from './info-modal'

type Props = Readonly<{
  data?: SiteInfo
  placement?: Placement
  hideLogout?: boolean
}>

const MenuDropdown: FC<Props> = ({
  data,
  placement,
  hideLogout,
}) => {
  const webAppAccessMode = useWebAppStore(s => s.webAppAccessMode)
  const router = useRouter()
  const pathname = usePathname()
  const { t } = useTranslation()

  const shareCode = useWebAppStore(s => s.shareCode)
  const handleLogout = async () => {
    await webAppLogout(shareCode!)
    router.replace(`/webapp-signin?redirect_url=${pathname}`)
  }

  const [show, setShow] = useState(false)
  const handleOpenInfoModal = () => {
    queueMicrotask(() => {
      setShow(true)
    })
  }

  return (
    <>
      <DropdownMenu>
        <DropdownMenuTrigger
          render={(
            <ActionButton size="l" className="data-popup-open:bg-state-base-hover">
              <span aria-hidden className="i-ri-equalizer-2-line h-[18px] w-[18px]" />
            </ActionButton>
          )}
          aria-label={t('operation.more', { ns: 'common' })}
        />
        <DropdownMenuContent
          placement={placement || 'bottom-end'}
          sideOffset={4}
          popupClassName="w-[224px]"
        >
          <div className="px-3 py-1.5 system-md-regular text-text-secondary">
            <div className="flex items-center gap-2">
              <div className="grow">{t('theme.theme', { ns: 'common' })}</div>
              <ThemeSwitcher />
            </div>
          </div>
          <DropdownMenuSeparator className="my-0" />
          {data?.privacy_policy && (
            <DropdownMenuLinkItem
              className="px-3 system-md-regular"
              href={data.privacy_policy}
              target="_blank"
              rel="noreferrer"
            >
              <span className="grow">{t('chat.privacyPolicyMiddle', { ns: 'share' })}</span>
            </DropdownMenuLinkItem>
          )}
          <DropdownMenuItem
            className="px-3 system-md-regular"
            onClick={handleOpenInfoModal}
          >
            {t('userProfile.about', { ns: 'common' })}
          </DropdownMenuItem>
          {!(hideLogout || webAppAccessMode === AccessMode.EXTERNAL_MEMBERS || webAppAccessMode === AccessMode.PUBLIC) && (
            <DropdownMenuItem
              className="px-3 system-md-regular"
              onClick={handleLogout}
            >
              {t('userProfile.logout', { ns: 'common' })}
            </DropdownMenuItem>
          )}
        </DropdownMenuContent>
      </DropdownMenu>
      {show && (
        <InfoModal
          isShow={show}
          onClose={() => {
            setShow(false)
          }}
          data={data}
        />
      )}
    </>
  )
}
export default React.memo(MenuDropdown)
