import { Locales } from '@/locales/resources';
import { DiscoverService } from '@/server/services/discover';

import Back from '../../(detail)/features/Back';
import List from '../../(list)/models/features/List';
import request from '@/app/request';
const ModelsResult = async ({
  locale,
  q,
  mobile,
  userid,
}: {
  locale: Locales;
  mobile?: boolean;
  q: string;
  userid: string;
}) => {
  const res = await request({
    method: "get",
    params: {
      userid: userid
    },
    url: "/flxai/api/robot/appaimodel/getAllAiModel",
  })
  const array2Object = {};
  res?.data?.forEach(item => {
    array2Object[item.identifier] = item;
  });
  const discoverService = new DiscoverService();
  let items = await discoverService.searchModel(locale, q);
  items = items.map((item)=> {
    const matchingItem = array2Object[item.identifier];
    if (matchingItem) {
      matchingItem.status = '1';
      return matchingItem
    } else {
      item.status = '0';
      return item
    }
  })

  return (
    <>
      {!mobile && <Back href={'/discover/models'} style={{ marginBottom: 0 }} />}
      <List items={items} mobile={mobile} searchKeywords={q} />
    </>
  );
};

export default ModelsResult;
