import { Locales } from '@/locales/resources';
import { DiscoverService } from '@/server/services/discover';
import request from '@/app/api/request';
import Back from '../../(detail)/features/Back';
import List from '../../(list)/assistants/features/List';

const AssistantsResult = async ({
  locale,
  q,
  userid,
  mobile,
}: {
  locale: Locales;
  mobile?: boolean;
  q: string;
  userid: string;
}) => {
  const res = await request({
      url: "/flxai/api/robot/appaiassistant/getAllAiAssistant",
      method: "get",
      params: {
        userid: userid
      }
  })
  const array2Object = {};
  res?.data?.forEach(item => {
    array2Object[item.identifier] = item;
  });
  const discoverService = new DiscoverService();
  let items = await discoverService.searchAssistant(locale, q);
  items = items.map((item,index)=> {
      const matchingItem = array2Object[item.identifier];
      if (matchingItem) {
        matchingItem.status = '1';
        return matchingItem
      } else {
        item.status = '0';
        return item
      }
  })
  console.log(locale,q,userid,'383737363636363')
  const backUrl = '/discover/assistants?userid=' + userid
  return (
    <>
      {!mobile && <Back href={backUrl} style={{ marginBottom: 0 }} />}
      <List items={items} mobile={mobile} searchKeywords={q} />
    </>
  );
};

export default AssistantsResult;
