
import { useState,  useEffect } from 'react';

import Request, {YWRequestConfig} from '@ywfe/request';
// @ts-ignore
import * as querystring from 'querystring';
// @ts-ignore
import { ParsedUrlQueryInput } from 'querystring';
// @ts-ignore
import * as R from 'ramda';
import {configs} from './config';


  const projectId = "PROAUTH";
  let env;
  if(typeof window === 'undefined'){
    env = (process.env.REACT_APP_ENV === 'prod' ? 'prod': process.env.REACT_APP_ENV) || 'prod';
  } else {
    env = window.localStorage.getItem('env') || (process.env.REACT_APP_ENV === 'prod' ? 'prod': process.env.REACT_APP_ENV) || 'prod';
  }
  
  const basePath = configs[projectId] ? configs[projectId][env].API_BASE_URL + `${env === 'mock' ? '/' + 88 : ''}` : '';
  const loginPath = configs['AUTH'] ? configs['AUTH'][env].APPLICATION_URL : '';
  

  

// @ts-ignore



  
    
      export interface DesktopResourceModuleListItem {
        
          // 资源父id
          resourceParentId?: string;
        
          // 名称
          name?: string;
        
          // 英文名
          nameEn?: string;
        
          // 跳转url
          url?: string;
        
          // icon
          icon?: string;
        
          // 序号
          sort?: number;
        
          // 菜单层级
          level?: number;
        
          // 
          status?: string;
        
          // 备注
          remark?: string;
        
          // 资源id
          resourceId?: string;
        
          // 资源类型，MENU ACTION BUTTON
          resourceType?: string;
        
          // 资源菜单类型；  HIDE_MENU（隐藏菜单）  COMMON_MENU（普通菜单）
          resourceMenuType: string;
        
          // PUBLISH PRIVATE
          isPublish?: string;
        
          // 是否拥有该资源
          isHasResourcce?: boolean;
        
      }
    
  


export interface DesktopResourceModuleListInput {
  
    
      /** (String) */
      moduleId?: string;
    
  
}

export interface DesktopResourceModuleListData {
  
    
      // 菜单列表 ,ResourceMenuDto
      menuResourceList?: DesktopResourceModuleListItem[];
    
      // 其他资源列表 ,ResourceMenuDto
      otherResourceList?: DesktopResourceModuleListItem[];
    
      // 产品负责人名称
      productUserName?: string;
    
      // 产品负责人id
      productUserId?: string;
    
  
}


const reduceParams = (data: R.Dictionary<DesktopResourceModuleListInput>) => {
  if(data instanceof FormData){
    return data;
  }
  const result = R.filter(item => !R.isNil(item) && !R.isEmpty(item))(data) || {};
  return result as unknown as DesktopResourceModuleListInput;
}


// 20844 获取模块下可视模块资源

  export const useGetDesktopResourceModuleList = () => {
    let isMount = true;
    const [isDesktopResourceModuleListLoading, setIsDesktopResourceModuleListLoading] = useState(false);
    const [dataDesktopResourceModuleList, setDataDesktopResourceModuleList] = useState<DesktopResourceModuleListData>({} as DesktopResourceModuleListData);
    const getDesktopResourceModuleListData = async(inputs: DesktopResourceModuleListInput, config?: YWRequestConfig): Promise<DesktopResourceModuleListData> => {
      
        const params = reduceParams(inputs as unknown as R.Dictionary<DesktopResourceModuleListInput>) || {};
      
      if(isMount){
        setIsDesktopResourceModuleListLoading(true);
      }
      
      try{
        
          
          const res: any = await Request.get<DesktopResourceModuleListData>(`${basePath}/desktop/resource/module/list?${querystring.stringify(params as unknown as ParsedUrlQueryInput)}`, {
            loginHost: loginPath,
            ...config
          });
          
          
          
          if(isMount){
            setDataDesktopResourceModuleList(res || {});
            setIsDesktopResourceModuleListLoading(false);
          }
          return res;
      } catch(error){
        console.log(error);
        if(isMount){
          setIsDesktopResourceModuleListLoading(false);
        }
        return false as unknown as DesktopResourceModuleListData;
      }
      
    }

    useEffect(() => {
      return () => {
        isMount = false;
      };
    }, []);
  
    return {
      isDesktopResourceModuleListLoading,
      dataDesktopResourceModuleList,
      getDesktopResourceModuleListData,
    }
  }

