import type { TabContentProps } from './types';
import type { DropMenu } from '@/components/Dropdown';

import { MenuEventEnum } from './types';
import { useMultipleTabStore } from '@/store/modules/multipleTab';
import { RouteLocationNormalized, useRouter } from 'vue-router';
import { useTabs } from '@/hooks/web/useTabs';
<%_ if (enableTranslation) { _%>
import { useI18n } from '@/hooks/web/useI18n';
<%_ } _%>

export function useTabDropdown(tabContentProps: TabContentProps, getIsTabs: ComputedRef<boolean>) {
  const state = reactive({
    current: null as Nullable<RouteLocationNormalized>,
    currentIndex: 0,
  });

  <%_ if (enableTranslation) { _%>
  const { t } = useI18n();
  <%_ } _%>
  const tabStore = useMultipleTabStore();
  const { currentRoute } = useRouter();
  const { refreshPage, closeAll, close, closeLeft, closeOther, closeRight } = useTabs();

  const getTargetTab = computed((): RouteLocationNormalized => {
    return unref(getIsTabs) ? tabContentProps.tabItem : unref(currentRoute);
  });

  /**
   * @description: drop-down list
   */
  const getDropMenuList = computed(() => {
    if (!unref(getTargetTab)) {
      return;
    }
    const { meta } = unref(getTargetTab);
    const { path } = unref(currentRoute);

    const curItem = state.current;

    const isCurItem = curItem ? curItem.path === path : false;

    // Refresh button
    const index = state.currentIndex;
    const refreshDisabled = !isCurItem;
    // Close left
    const closeLeftDisabled = () => {
      if (index === 0) {
        return true;
      }
      const validTabList = tabStore.getTabList.filter(item => !item?.meta?.affix);
      return validTabList[0].path === state.current?.path;
    };

    const closeOtherDisabled = () => {
      if (tabStore.getTabList.length === 1) {
        return true;
      }
      const validTabList = tabStore.getTabList.filter(item => !item?.meta?.affix);
      return validTabList.length == 1;
    };

    // Close right
    const closeRightDisabled =
      !isCurItem || (index === tabStore.getTabList.length - 1 && tabStore.getLastDragEndIndex >= 0);
    const disabled = tabStore.getTabList.length === 1;
    const dropMenuList: DropMenu[] = [
      {
        icon: 'ion:reload-sharp',
        event: MenuEventEnum.REFRESH_PAGE,
        text: <% if (enableTranslation) { %>t('layout.multipleTab.reload')<% } else { %>'t$t('layout.multipleTab.reload')$t$'<% } %>,
        disabled: refreshDisabled,
      },
      {
        icon: 'clarity:close-line',
        event: MenuEventEnum.CLOSE_CURRENT,
        text: <% if (enableTranslation) { %>t('layout.multipleTab.close')<% } else { %>'t$t('layout.multipleTab.close')$t$'<% } %>,
        disabled: !!meta?.affix || disabled,
        divider: true,
      },
      {
        icon: 'line-md:arrow-close-left',
        event: MenuEventEnum.CLOSE_LEFT,
        text: <% if (enableTranslation) { %>t('layout.multipleTab.closeLeft')<% } else { %>'t$t('layout.multipleTab.closeLeft')$t$'<% } %>,
        disabled: closeLeftDisabled(),
        divider: false,
      },
      {
        icon: 'line-md:arrow-close-right',
        event: MenuEventEnum.CLOSE_RIGHT,
        text: <% if (enableTranslation) { %>t('layout.multipleTab.closeRight')<% } else { %>'t$t('layout.multipleTab.closeRight')$t$'<% } %>,
        disabled: closeRightDisabled,
        divider: true,
      },
      {
        icon: 'dashicons:align-center',
        event: MenuEventEnum.CLOSE_OTHER,
        text: <% if (enableTranslation) { %>t('layout.multipleTab.closeOther')<% } else { %>'t$t('layout.multipleTab.closeOther')$t$'<% } %>,
        disabled: disabled || !isCurItem,
      },
      {
        icon: 'clarity:minus-line',
        event: MenuEventEnum.CLOSE_ALL,
        text: <% if (enableTranslation) { %>t('layout.multipleTab.closeAll')<% } else { %>'t$t('layout.multipleTab.closeAll')$t$'<% } %>,
        disabled,
      },
    ];

    return dropMenuList;
  });

  function handleContextMenu(tabItem: RouteLocationNormalized) {
    return (e: Event) => {
      if (!tabItem) {
        return;
      }
      e?.preventDefault();
      const index = tabStore.getTabList.findIndex((tab) => tab.path === tabItem.path);
      state.current = tabItem;
      state.currentIndex = index;
    };
  }

  // Handle right click event
  function handleMenuEvent(menu: DropMenu): void {
    const { event } = menu;
    switch (event) {
      case MenuEventEnum.REFRESH_PAGE:
        // refresh page
        refreshPage();
        break;
      // Close current
      case MenuEventEnum.CLOSE_CURRENT:
        close(tabContentProps.tabItem);
        break;
      // Close left
      case MenuEventEnum.CLOSE_LEFT:
        closeLeft(state.current);
        break;
      // Close right
      case MenuEventEnum.CLOSE_RIGHT:
        closeRight(state.current);
        break;
      // Close other
      case MenuEventEnum.CLOSE_OTHER:
        closeOther(state.current);
        break;
      // Close all
      case MenuEventEnum.CLOSE_ALL:
        closeAll(state.current);
        break;
    }
  }
  return { getDropMenuList, handleMenuEvent, handleContextMenu };
}
