
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 = "PROSUPPLIER";
  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' ? '/' + 112 : ''}` : '';
  const loginPath = configs['AUTH'] ? configs['AUTH'][env].APPLICATION_URL : '';
  

  

// @ts-ignore




export interface BrandCategoryListInput {
  
    
    /** 叶子类目编号 */
    categoryNo?: string;
  
    
    /** 品牌名, 模糊查询 */
    brandName?: string;
  
}

export interface BrandCategoryListData {
  
}


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


// 12540 查询类目下品牌列表

  export const usePostBrandCategoryList = () => {
    let isMount = true;
    const [isBrandCategoryListLoading, setIsBrandCategoryListLoading] = useState(false);
    const [dataBrandCategoryList, setDataBrandCategoryList] = useState<BrandCategoryListData>({} as BrandCategoryListData);
    const postBrandCategoryListData = async(inputs: BrandCategoryListInput, config?: YWRequestConfig): Promise<BrandCategoryListData> => {
      
        const params = reduceParams(inputs as unknown as R.Dictionary<BrandCategoryListInput>) || {};
      
      if(isMount){
        setIsBrandCategoryListLoading(true);
      }
      
      try{
        
          
          
          const res: any = await Request.post<BrandCategoryListData>(`${basePath}/brand/category/list`, params, {
            loginHost: loginPath,
            ...config
          });
          
          
          if(isMount){
            setDataBrandCategoryList(res || {});
            setIsBrandCategoryListLoading(false);
          }
          return res;
      } catch(error){
        console.log(error);
        if(isMount){
          setIsBrandCategoryListLoading(false);
        }
        return false as unknown as BrandCategoryListData;
      }
      
    }

    useEffect(() => {
      return () => {
        isMount = false;
      };
    }, []);
  
    return {
      isBrandCategoryListLoading,
      dataBrandCategoryList,
      postBrandCategoryListData,
    }
  }

