import React, { useMemo } from 'react';
import { Button, Tooltip } from 'antd';

const FC = (props) => {
  const { permissionKey, permissions, defaultPermission, disabled, tip, tooltipPlacement = 'top', ...restProps } = props;

  const havePermission = useMemo(() => {
    let _havePermission = true;

    if (defaultPermission!==undefined && defaultPermission!==null) {
      _havePermission = defaultPermission;
    } else if (permissionKey) {
      const index = (permissions||[]).findIndex(item => item === permissionKey);
      _havePermission = (index !== -1);
    }
    
    return _havePermission;
  }, [permissionKey, permissions, defaultPermission])

  return (
    <Tooltip title={tip} placement={tooltipPlacement}>
      <Button disabled={!havePermission||disabled} {...restProps} />
    </Tooltip>
  )
}

export default FC;