/**
 * 用户中心 fallback 组件，用于没有权限访问时展示的页面
 */

import React from 'react';
import Empty from '@arcblock/ux/lib/Empty';
import { translate } from '@arcblock/ux/lib/Locale/util';
import { useMemoizedFn } from 'ahooks';
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';

import { translations } from '../libs/locales';
import { UserCenterTab } from '../../@types';
import { useUserFollowersContext } from '../../contexts/user-followers';

function Fallback({
  currentActiveTab,
  isMyself,
  isSupportFollow = false,
  children,
}: {
  currentActiveTab: UserCenterTab;
  isMyself: boolean;
  isSupportFollow?: boolean;
  children: React.ReactNode;
}) {
  const { locale } = useLocaleContext();
  const t = useMemoizedFn((key, data = {}) => {
    return translate(translations, key, locale, 'en', data);
  });
  const { followed } = useUserFollowersContext();

  // 自己的页面或公开内容，直接显示
  if (isMyself || !currentActiveTab?.protected || currentActiveTab?.protected === 'all') {
    return children;
  }

  const { protected: protection } = currentActiveTab;

  // 如果还没有支持 follow 关系，但是已经设置了仅粉丝可见，则直接显示内容
  if (!isSupportFollow && protection === 'follower-only') {
    return children;
  }
  // 私密内容（仅自己可见）
  if (protection === true || protection === 'private') {
    return <Empty>{t('underProtected')}</Empty>;
  }

  // 仅粉丝可见内容
  if (protection === 'follower-only') {
    return followed ? children : <Empty>{t('followersOnly')}</Empty>;
  }

  // 未知的保护类型，默认不显示
  return <Empty>{t('underProtected')}</Empty>;
}

export default Fallback;
