
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 DesktopCloudLockModuleInput {
  
    
    /** 模块id */
    moduleId?: string;
  
}

export interface DesktopCloudLockModuleData {
  
}


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


// 20848 锁定用户模块

  export const usePostDesktopCloudLockModule = () => {
    let isMount = true;
    const [isDesktopCloudLockModuleLoading, setIsDesktopCloudLockModuleLoading] = useState(false);
    const [dataDesktopCloudLockModule, setDataDesktopCloudLockModule] = useState<DesktopCloudLockModuleData>({} as DesktopCloudLockModuleData);
    const postDesktopCloudLockModuleData = async(inputs: DesktopCloudLockModuleInput, config?: YWRequestConfig): Promise<DesktopCloudLockModuleData> => {
      
        const params = reduceParams(inputs as unknown as R.Dictionary<DesktopCloudLockModuleInput>) || {};
      
      if(isMount){
        setIsDesktopCloudLockModuleLoading(true);
      }
      
      try{
        
          
          
          const res: any = await Request.post<DesktopCloudLockModuleData>(`${basePath}/desktop/cloud/lock/module`, params, {
            loginHost: loginPath,
            ...config
          });
          
          
          if(isMount){
            setDataDesktopCloudLockModule(res || {});
            setIsDesktopCloudLockModuleLoading(false);
          }
          return res;
      } catch(error){
        console.log(error);
        if(isMount){
          setIsDesktopCloudLockModuleLoading(false);
        }
        return false as unknown as DesktopCloudLockModuleData;
      }
      
    }

    useEffect(() => {
      return () => {
        isMount = false;
      };
    }, []);
  
    return {
      isDesktopCloudLockModuleLoading,
      dataDesktopCloudLockModule,
      postDesktopCloudLockModuleData,
    }
  }

