import Link from 'next/link';
import { ShoppingCart } from 'lucide-react';

import { Trans } from '@kit/ui/trans';
import { AppLogo } from '~/components/app-logo';
import { ProfileAccountDropdownContainer } from '~/components/personal-account-dropdown-container';
import { Search } from '~/components/ui/search';
import { SIDEBAR_WIDTH_PROPERTY } from '@/packages/ui/src/shadcn/constants';
import { Button } from '@kit/ui/button';

import { UserNotifications } from '../_components/user-notifications';
import { type UserWorkspace } from '../_lib/server/load-user-workspace';
import { StoreCart } from '@medusajs/types';
import { formatCurrency } from '@/packages/shared/src/utils';
import { createI18nServerInstance } from '@/lib/i18n/i18n.server';

export async function HomeMenuNavigation(props: { workspace: UserWorkspace, cart: StoreCart | null }) {
  const { language } = await createI18nServerInstance();
  const { workspace, user, accounts } = props.workspace;
  const totalValue = props.cart?.total ? formatCurrency({
    currencyCode: props.cart.currency_code,
    locale: language,
    value: props.cart.total,
  }) : 0;

  const cartItemsCount = props.cart?.items?.length ?? 0;
  const hasCartItems = cartItemsCount > 0;

  return (
    <div className={'flex w-full flex-1 items-center justify-between gap-3'}>
      <div className={`flex items-center ${SIDEBAR_WIDTH_PROPERTY}`}>
        <AppLogo />

      </div>
      <Search
        className="flex grow"
        startElement={<Trans i18nKey="common:search" values={{ end: '...' }} />}
      />

      <div className="flex items-center justify-end gap-3">
        {hasCartItems && (
          <Button className='relative px-4 py-2 h-10 border-1 mr-0 cursor-pointer' variant='ghost'>
            <span className='flex items-center text-nowrap'>{totalValue}</span>
          </Button>
        )}
        <Link href='/home/cart'>
          <Button variant="ghost" className='relative px-4 py-2 h-10 border-1 mr-0 cursor-pointer' >
            <ShoppingCart className="stroke-[1.5px]" />
            <Trans i18nKey="common:shoppingCart" /> ({hasCartItems ? cartItemsCount : 0})
          </Button>
        </Link>
        <UserNotifications userId={user.id} />

        <div>
          <ProfileAccountDropdownContainer
            user={user}
            account={workspace}
            showProfileName
            accounts={accounts}
          />
        </div>
      </div>
    </div>
  );
}
