
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 DesktopModuleInfoItem {
        
          // 菜单id
          resourceId?: string;
        
          // 菜单名称
          resourceName?: string;
        
          // 路径
          url?: string;
        
          // 云桌面菜单标识
          nameEn?: string;
        
      }
    
  


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

export interface DesktopModuleInfoData {
  
    
      // 模块id
      moduleId?: string;
    
      // 模块名称
      moduleName?: string;
    
      // 模块标识
      moduleNameEn?: string;
    
      // 模块描述
      moduleRemark?: string;
    
      // 模块图标icon
      moduleIcon?: string;
    
      // 云分类id
      cloudCategoryId?: string;
    
      // 云分类名称
      cloudCategoryName?: string;
    
      // 序号
      sort?: number;
    
      // 菜单资源数量
      resourceNum?: number;
    
      // 是否展示
      isShow?: boolean;
    
      // 菜单集合 ,DesktopResourceVO
      resourceList?: DesktopModuleInfoItem[];
    
      // 模块类型：DEFAULT ONLINE TRAILER
      moduleType?: string;
    
      // 预告文案
      trailerText?: string;
    
      // 产品负责人id
      productUserId?: string;
    
      // 产品负责人名称
      productUserName?: string;
    
      // 是否开放模块权限
      isPublish?: boolean;
    
  
}


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


// 20838 模块详情接口

  export const useGetDesktopModuleInfo = () => {
    let isMount = true;
    const [isDesktopModuleInfoLoading, setIsDesktopModuleInfoLoading] = useState(false);
    const [dataDesktopModuleInfo, setDataDesktopModuleInfo] = useState<DesktopModuleInfoData>({} as DesktopModuleInfoData);
    const getDesktopModuleInfoData = async(inputs: DesktopModuleInfoInput, config?: YWRequestConfig): Promise<DesktopModuleInfoData> => {
      
        const params = reduceParams(inputs as unknown as R.Dictionary<DesktopModuleInfoInput>) || {};
      
      if(isMount){
        setIsDesktopModuleInfoLoading(true);
      }
      
      try{
        
          
          const res: any = await Request.get<DesktopModuleInfoData>(`${basePath}/desktop/module/info?${querystring.stringify(params as unknown as ParsedUrlQueryInput)}`, {
            loginHost: loginPath,
            ...config
          });
          
          
          
          if(isMount){
            setDataDesktopModuleInfo(res || {});
            setIsDesktopModuleInfoLoading(false);
          }
          return res;
      } catch(error){
        console.log(error);
        if(isMount){
          setIsDesktopModuleInfoLoading(false);
        }
        return false as unknown as DesktopModuleInfoData;
      }
      
    }

    useEffect(() => {
      return () => {
        isMount = false;
      };
    }, []);
  
    return {
      isDesktopModuleInfoLoading,
      dataDesktopModuleInfo,
      getDesktopModuleInfoData,
    }
  }

