import { RootState } from 'models';
import { TanantListType } from 'models/user.types';
import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { resultLocalProductionName } from 'utils/utils';
import {SKU_ID_TYPS} from 'models/user.types';
/**
 *
 * @author wanfeng
 * @return 通过这个勾子获取平台所有产品用户角色相关信息
 * @description
 * 相关业务文档https://dtstack.yuque.com/docs/share/0863c779-90d9-4d79-b8ec-ba98ba837b39?#
 */
type ProductionInfoType = {
  isAdmin?: boolean;
  isRenew?: boolean; //是否为renew版本
  isLocal?: boolean; //是否为独立部署
  localType?: keyof TanantListType; //独立部署产品版本
  localTypeName?: TanantListType[keyof TanantListType]; //独立部署产品中文名
  skuId?: SKU_ID_TYPS; //1=>saas试用版 2、5=>saas高级版 3、6=>saas尊享版 4=>saas基础版
  isRoot?: boolean; //是否root权限用户
};

type useProductionInfoType = () => ProductionInfoType;

const useProductionInfo: useProductionInfoType = () => {
  const user = useSelector((state: RootState) => state.user);
  const [productionInfo, setProductionInfo] = useState<ProductionInfoType>({});
  useEffect(() => {
    if (user.userInfo) {
      if (window.appConfig.LOCAL) {
        setProductionInfo({
          isLocal: window.appConfig.LOCAL,
          localType: user.userInfo.type,
          localTypeName: resultLocalProductionName(user.userInfo.type!),
          isRenew: false,
          isAdmin: user.isAdmin,
          isRoot: user.isRoot
        });
        return;
      }
      setProductionInfo({
        isLocal: window.appConfig.LOCAL,
        skuId: user.userInfo.skuId,
        isAdmin: user.isAdmin,
        isRoot: user.isRoot,
        isRenew: user.isRenew
      });
    }
  }, [user]);
  return productionInfo;
};
export default useProductionInfo;
