import 'iconify-icon';

import PropTypes from 'prop-types';
import { createElement, use } from 'react';

// FIXME: 直接从 react 中 import  Fragment 可能会在 vite 下出错，先暂时从 react/jsx-runtime 导入 Fragment 来跳过这个问题
import { SessionContext } from '@arcblock/did-connect-react/lib/Session';
import ThemeModeToggle from '@arcblock/ux/lib/Config/theme-mode-toggle';
import { useLocaleContext } from '@arcblock/ux/lib/Locale/context';
import LocaleSelector from '@arcblock/ux/lib/Locale/selector';
import SessionBlocklet from '@arcblock/ux/lib/SessionBlocklet';
import SessionUser from '@arcblock/ux/lib/SessionUser';
import { Fragment } from 'react/jsx-runtime';
import { WELLKNOWN_BLOCKLET_ADMIN_PATH } from '@abtnode/constant';

import { filterNavByRole, getLocalizedNavigation } from '../blocklets';
import { SessionManagerProps } from '../types';
import DomainWarning from './domain-warning';
import NotificationAddon from './notification-addon';
import OrgsSwitch from './org-switch';

const hasNotification = () => {
  const navigations = window?.blocklet?.navigation ?? [];
  return !!navigations.find((n) => n.id === '/userCenter/notification');
};

const isOrgsEnabled = (blocklet) => {
  const { settings = {} } = blocklet ?? window?.blocklet ?? {};
  return settings?.org?.enabled || false;
};

const inDashboard = () => {
  const { pathname = '' } = window.location || {};
  return pathname.startsWith(WELLKNOWN_BLOCKLET_ADMIN_PATH);
};

// eslint-disable-next-line no-shadow
export default function HeaderAddons({
  formattedBlocklet,
  addons = null,
  showDomainWarningDialog = true,
  sessionManagerProps = { showRole: true },
}) {
  const sessionCtx = use(SessionContext);
  const { locale, languages, defaultLocale } = useLocaleContext() || {};
  const { enableConnect = true, enableLocale = true } = formattedBlocklet;
  const authenticated = !!sessionCtx?.session?.user;
  let localizedNav =
    getLocalizedNavigation({
      navigation: formattedBlocklet?.navigation?.sessionManager,
      locale,
      defaultLocale,
    }) || [];
  // 根据 role 筛选 nav 数据
  localizedNav = filterNavByRole(localizedNav, sessionCtx?.session?.user?.role);

  const renderAddons = () => {
    // 不关心内置的 session manager 和 locale selector, 直接覆盖 UX Header 的 addons
    if (addons && typeof addons !== 'function') {
      return Array.isArray(addons) ? addons : [addons];
    }
    let addonsArray = [];

    if (hasNotification()) {
      addonsArray.push(<NotificationAddon key="notification-addon" session={sessionCtx.session} />);
    }

    // 启用了多语言，且检测到了 locale context，且有多种语言可以切换
    if (enableLocale && locale && languages.length > 1) {
      addonsArray.push(<LocaleSelector key="locale-selector" showText={false} />);
    }

    // 切换明暗主题
    addonsArray.push(<ThemeModeToggle key="theme-mode-toggle" />);

    // 启用了连接钱包并且检测到了 session context
    if (enableConnect && sessionCtx) {
      const menu = [];
      if (authenticated) {
        const navList = localizedNav ? localizedNav.slice(0, 5) : [];
        navList.forEach((x) => {
          menu.push({
            label: x.title,
            icon: x.icon ? <iconify-icon icon={x.icon} height={24} style={{ marginRight: 8 }} /> : null,
            component: 'a',
            href: x.link,
            key: x.link,
          });
        });
      }

      addonsArray.push(<SessionBlocklet key="session-blocklet" session={sessionCtx.session} locale={locale} />);

      addonsArray.push(
        <SessionUser
          key="session-user"
          session={sessionCtx.session}
          locale={locale}
          menu={menu}
          showRole
          {...sessionManagerProps}
        />
      );
    }

    // 在最前面添加 orgs switch, 在 Dashboard 中不显示
    if (!inDashboard() && isOrgsEnabled(formattedBlocklet) && sessionCtx?.session?.user) {
      addonsArray.unshift(<OrgsSwitch key="orgs-switch" session={sessionCtx.session} locale={locale} />);
    }

    if (typeof addons === 'function') {
      addonsArray = addons(addonsArray) || [];
    }

    return addonsArray;
  };

  const renderedAddons = renderAddons();
  const nodes = Array.isArray(renderedAddons) ? renderedAddons : [renderedAddons];
  const mergedNodes = [
    showDomainWarningDialog ? <DomainWarning session={sessionCtx?.session} locale={locale} /> : null,
    ...nodes,
  ].filter(Boolean);

  return createElement(Fragment, null, ...mergedNodes);
}

HeaderAddons.propTypes = {
  formattedBlocklet: PropTypes.object.isRequired,
  // 需要考虑 定制的 addons 与内置的 连接钱包/选择语言 addons 共存的情况
  // - PropTypes.func: 可以把自定义 addons 插在 session-manager 或 locale-selector (如果存在的话) 前/中/后
  // - PropTypes.node: 将 addons 原样传给 UX Header 组件
  addons: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
  sessionManagerProps: SessionManagerProps,
  showDomainWarningDialog: PropTypes.bool,
};
