import React, { useMemo } from 'react';
import { isJSON } from '@/webPublic/one_stop_public/copy';


export default function Authorized(props) {
  const {
    authority,
    children
  } = props;
  const check = useMemo(() => {
    let parentIframeRouteInfo = localStorage.getItem('parentRouteInfo'); // 获取父节点路由信息
    parentIframeRouteInfo = isJSON(parentIframeRouteInfo) ? JSON.parse(parentIframeRouteInfo) : null;
    let allAuth = localStorage.getItem('antd-pro-authority');
    allAuth = isJSON(allAuth) ? JSON.parse(allAuth) : null;

    if(!parentIframeRouteInfo || !allAuth){
      return false;
    }

    const { path } = parentIframeRouteInfo;
    const checkPath = path + authority;
    console.log('检查权限：', checkPath);
    let findX = allAuth.find((g) => g === checkPath);
    return !!findX;
  }, [authority]);


  if (!check) {
    return null;
  }

  return children;

}
