import React from 'react';

let allBtns = [];
let current = [];

const AuthButton = ({ children, name, noRight }) => {
  const btn = allBtns.filter(x => x.code === name)[0]

  // 没维护的按钮, 或者不需要权限的按钮直接通过
  // if (!btn || !btn.roles || !btn.roles.length) {
  //   return <>{children}</>
  // }

  // const hasRight = btn.roles.some(x => current.some(y => x === y))

  if (!btn || !btn.rolesList || !btn.rolesList.length) {
    return <>{children}</>
  }

  const hasRight = btn.rolesList.some(x => current.some(y => x.roleId === y))
  return hasRight ? <>{children}</> : <>{noRight}</>
}

const setAllBtnAuth = x => allBtns = x;
const setUserBtnAuth = x => current = x;

export default AuthButton;
export {
  setAllBtnAuth,
  setUserBtnAuth,
};
