import * as R from 'ramda';
import {
  useGetProjectList,
  ProjectListItem,
  
} from '@/generated/api';
import {useSystemContext} from '@/system/system.context';
import {MenuItem} from './layout.interface';
import Item from 'antd/lib/list/Item';

export const useModel = () => {
  const {store} = useSystemContext();
  const [state, dispatch] = store();
  const { getProjectListData } = useGetProjectList();
  

  const initMenus = async (menus: MenuItem[]): Promise<MenuItem[]> => {
    const res = await getProjectListData({userProjectRelType: "ACCESS"});
    const {projectList = []} = res || {};
    const data = projectList as ProjectListItem[];
    const getProjectByMenu = (item: MenuItem) => R.find<ProjectListItem>(R.propEq('projectId', item.name), projectList)
    const result: MenuItem[] = [];
    menus.map(item => {
      const project = getProjectByMenu(item);
      if(project){
        result.push({
          ...item,
          meta: {
            title: project.projectName,
            icon: item.meta.icon,
          }
        })
      }
    })
    return result
  }

  

  return {
    initMenus,
  };
};
