import StructuredData from '@/components/StructuredData';
import { Locales } from '@/locales/resources';
import { ldModule } from '@/server/ld';
import { metadataModule } from '@/server/metadata';
import { DiscoverService } from '@/server/services/discover';
import { translation } from '@/server/translation';
import { isMobileDevice } from '@/utils/responsive';
import request from '@/app/api/request';
import List from './features/List';
import urlJoin from 'url-join';
type Props = { searchParams: { hl?: Locales } };

export const generateMetadata = async ({ searchParams }: Props) => {
  const { t, locale } = await translation('metadata', searchParams?.hl);
  console.log(searchParams,'jiangxucong74774747')
  return metadataModule.generate({
    alternate: true,
    description: t('discover.assistants.description'),
    locale,
    title: t('discover.assistants.title'),
    url: '/discover/assistants',
  });
};

const Page = async ({ searchParams }: Props) => {
  const { t, locale } = await translation('metadata', searchParams?.hl);
  const mobile = isMobileDevice();
  const discoverService = new DiscoverService();
  console.log(searchParams,22222222222)
  const res = await request({
      url: "/flxai/api/robot/appaiassistant/getAllAiAssistant",
      method: "get",
      params: {
        userid: searchParams.userid
      }
  })
  console.log(res,4322222)
  const array2Object = {};
  res?.data?.forEach(item => {
    array2Object[item.identifier] = item;
  });
  let items = await discoverService.getAssistantList(locale);
  console.log(8272772727,'jiangxucong')
  items = items.map((item,index)=> {
      const matchingItem = array2Object[item.identifier];
      if (matchingItem) {
        console.log('5959595959',matchingItem);
        matchingItem.status = '1';
        return matchingItem
      } else {
        item.status = '0';
        return item
      }
  })
  const ld = ldModule.generate({
    description: t('discover.assistants.description'),
    title: t('discover.assistants.title'),
    url: urlJoin('/discover/assistants?userid=',searchParams.userid),
    webpage: {
      enable: true,
      search: '/discover/search',
    },
  });

  return (
    <>
      <StructuredData ld={ld} />
      <List items={items} mobile={mobile} />
    </>
  );
};

Page.DisplayName = 'DiscoverAssistants';

export default Page;
