'use client';

import { useLayoutStore } from '@/modules/layout/store';
import { useShallow } from 'zustand/shallow';
import { toast } from 'sonner';
import { Folder } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { openLink } from '@/modules/basename';

type Props = {
  pathname?: string;
};
export const AIEditorLink = (props: Props) => {
  const layoutUser = useLayoutStore(
    useShallow((state) => ({
      user: state.me?.username || '',
    })),
  );
  return (
    <Tooltip>
      <TooltipTrigger>
        <div
          className='inline-flex items-center justify-center rounded-md p-2 transition-colors hover:bg-slate-100 disabled:opacity-50 disabled:pointer-events-none'
          onClick={() => {
            if (!layoutUser.user) {
              toast.error('请先登录');
            }
            if (!window) {
              return;
            }
            let folder = `${layoutUser.user}/resources/${props.pathname}`;
            if (folder.endsWith('/')) {
              folder = folder.slice(0, -1);
            }
            let baseUri = location.origin;
            const openUrl = `${baseUri}/root/codepod/?folder=${folder}/`;
            openLink(openUrl, '_blank');
          }}>
          <Folder className='h-4 w-4' />
        </div>
      </TooltipTrigger>
      <TooltipContent>打开对应的文件夹</TooltipContent>
    </Tooltip>
  );
};
