import {
  InsertChartOutlinedRounded,
  PeopleOutlined,
  PlayCircleOutlined,
  SensorsRounded,
  SettingsOutlined,
  TvOutlined,
  VideoLibraryOutlined,
} from '@mui/icons-material';

import { PCN_GET_PLAYER } from '@/modules/@common/acl/constants';
import {
  AD_SERVERS_PAGE,
  ADVERTISERS_PAGE,
  ANALYTICS_CUSTOM_REPORTS,
  ANALYTICS_LIVE_DASHBOARD,
  ANALYTICS_MONETIZATION_DASHBOARD,
  ANALYTICS_PAGE,
  ANALYTICS_VIDEO_CONTENT_PERFORMANCE,
  CONFIG_PAGE,
  CUSTOM_REPORTS_PAGE,
  EDITORIAL_PAGE,
  ENTITIES_PAGE,
  FEEDS_PAGE,
  FORM_TEMPLATES,
  FORMS,
  HOSTED_WATCH_PAGE,
  HUBS_PAGE,
  INVENTORY_PAGE,
  INVITATIONS_PAGE,
  LIVE_PAGE,
  MARKETPLACE_DASHBOARD_PAGE,
  MARKETPLACE_HB_CONNECTORS_PAGE,
  MARKETPLACE_SELF_SERVE,
  NOTIFICATIONS_PAGE,
  ONLINE_HELP_CONFIG_PAGE,
  PARTNERS_ACCOUNTS_PAGE,
  PERMISSIONS_PAGE,
  PLAYER_PAGE,
  PLAYER_PCN_PAGE,
  PUBLISHING_PAGE,
  ROLES_PERMISSIONS_PAGE,
  SSO_PAGE,
  // HOSTED_WATCH,
  USER_RULES_SETTINGS,
  USERS_PAGE,
  X_RAY_CAMPAIGNS_PAGE,
  X_RAY_CREATIVES_PAGE,
  X_RAY_LINE_ITEMS_PAGE,
} from '@/modules/@common/router/constants';
import { ANALYTIC_MENU } from '@/modules/layout/components/menu/constants';

import { hasPermission } from '@/modules/@common/user/helpers';

import { CustomAnalyticsFilled, CustomInteractionsFilled } from '@/mui/components/CustomIcon';

const getTitleKey = (sourceString) =>
  sourceString.toLowerCase().replace(/\W/g, (match) => (/\d/.test(match) ? match : '-'));

const getHref = (userPermissions, item) =>
  item.permissions.some((permission) => hasPermission(permission, userPermissions)) ? item.path : null;

const filterItemsAndAddSpecialProps = (menu, nesting) =>
  menu.reduce((acc, item) => {
    const id = `${getTitleKey(item.title)}-${nesting}`;
    if (item.href) {
      return acc.concat({
        ...item,
        id,
        nesting,
      });
    }

    if (item.subMenu?.length) {
      const uSubMenu = filterItemsAndAddSpecialProps(item.subMenu, nesting + 1);

      if (uSubMenu.length) {
        return acc.concat({
          ...item,
          subMenu: uSubMenu,
          id,
          nesting,
        });
      }
    }

    return acc;
  }, []);

export const getMenuCollection = (accountId, userPermissions = []) => {
  const menu = [
    {
      title: 'Watch',
      icon: VideoLibraryOutlined,
      href: getHref(userPermissions, HOSTED_WATCH_PAGE),
    },
    {
      title: accountId ? 'Account' : 'Admin',
      iconType: 'light',
      icon: SettingsOutlined,
      subMenu: [
        {
          title: 'Publish Destinations',
          href: getHref(userPermissions, PUBLISHING_PAGE),
        },
        {
          title: 'Hubs',
          href: getHref(userPermissions, HUBS_PAGE),
        },
        {
          title: 'Users',
          href: getHref(userPermissions, USERS_PAGE),
        },
        {
          title: 'Roles & Permissions',
          href: getHref(userPermissions, ROLES_PERMISSIONS_PAGE),
        },
        {
          title: 'Permissions',
          href: getHref(userPermissions, PERMISSIONS_PAGE),
        },
        {
          title: 'Sources',
          href: getHref(userPermissions, FEEDS_PAGE),
        },
        {
          title: 'Notifications',
          href: getHref(userPermissions, NOTIFICATIONS_PAGE),
        },
        {
          title: 'Single Sign-On',
          href: getHref(userPermissions, SSO_PAGE),
        },
        {
          title: 'Invitations',
          href: getHref(userPermissions, INVITATIONS_PAGE),
        },
        {
          title: 'Reports',
          subMenu: [
            {
              title: 'Custom Dashboards',
              href: getHref(userPermissions, CUSTOM_REPORTS_PAGE),
            },
          ],
        },
        {
          title: 'Entities',
          href: getHref(userPermissions, ENTITIES_PAGE),
        },
        {
          title: 'Online Help Configuration',
          href: getHref(userPermissions, ONLINE_HELP_CONFIG_PAGE),
        },
        {
          title: 'Luminous Configurations',
          href: getHref(userPermissions, CONFIG_PAGE),
        },
      ],
    },
    {
      title: 'Partners',
      icon: PeopleOutlined,
      subMenu: [
        {
          title: 'Accounts',
          href: getHref(userPermissions, PARTNERS_ACCOUNTS_PAGE),
        },
        {
          title: 'Advertisers',
          href: getHref(userPermissions, ADVERTISERS_PAGE),
        },
      ],
    },
    {
      title: 'Players',
      icon: PlayCircleOutlined,
      href: getHref(userPermissions, userPermissions.includes(PCN_GET_PLAYER) ? PLAYER_PCN_PAGE : PLAYER_PAGE),
    },
    {
      title: 'Marketplace',
      icon: InsertChartOutlinedRounded,
      isMobile: true,
      subMenu: [
        {
          title: 'Dashboard',
          href: getHref(userPermissions, MARKETPLACE_DASHBOARD_PAGE),
          isMobile: true,
        },
        {
          title: 'Ad Servers',
          href: getHref(userPermissions, AD_SERVERS_PAGE),
        },
        {
          title: 'HB Connectors',
          href: getHref(userPermissions, MARKETPLACE_HB_CONNECTORS_PAGE),
        },
        {
          title: 'Inventory Manager',
          href: getHref(userPermissions, INVENTORY_PAGE),
        },
      ],
    },
    {
      title: 'Marketplace',
      icon: InsertChartOutlinedRounded,
      href: getHref(userPermissions, MARKETPLACE_SELF_SERVE),
      isMobile: true,
    },
    {
      title: 'Studio',
      icon: TvOutlined,
      href: getHref(userPermissions, EDITORIAL_PAGE),
      isMobile: true,
    },
    {
      title: 'Live',
      icon: SensorsRounded,
      href: getHref(userPermissions, LIVE_PAGE),
    },
    {
      title: 'Interactions',
      icon: CustomInteractionsFilled,
      subMenu: [
        {
          title: 'X-Ray Creatives',
          href: getHref(userPermissions, X_RAY_CREATIVES_PAGE),
        },
        {
          title: 'X-Ray Campaigns',
          href: getHref(userPermissions, X_RAY_CAMPAIGNS_PAGE),
        },
        {
          title: 'X-Ray Line Items',
          href: getHref(userPermissions, X_RAY_LINE_ITEMS_PAGE),
        },
        {
          title: 'Form Templates',
          href: getHref(userPermissions, FORM_TEMPLATES),
        },
        {
          title: 'Forms',
          href: getHref(userPermissions, FORMS),
        },
      ],
    },
    {
      title: 'Analytics',
      icon: CustomAnalyticsFilled,
      subMenu: [
        {
          title: 'Analytics',
          href: getHref(userPermissions, ANALYTICS_PAGE),
          componentName: ANALYTIC_MENU.componentName,
          componentProps: {
            inHouseMenuSettings: {
              /*
             Pay attention -> Inhouse menu will be returned from API
             with lookerId in the next format anyclip_analytics::video_performance_external
              - "anyclip_analytics" substring means its inhouse
              - "video_performance_external" its type
              Visibility controls on the Account dashboard settings page
            */
              [ANALYTIC_MENU.inHouseItemTypes.videoPerformanceExternal]: {
                title: 'Video Performance',
                href: ANALYTICS_VIDEO_CONTENT_PERFORMANCE.path,
              },
              [ANALYTIC_MENU.inHouseItemTypes.liveEvents]: {
                title: 'Live Events',
                href: ANALYTICS_LIVE_DASHBOARD.path,
              },
              [ANALYTIC_MENU.inHouseItemTypes.monetization]: {
                title: 'Monetization',
                href: ANALYTICS_MONETIZATION_DASHBOARD.path,
              },
              [ANALYTIC_MENU.inHouseItemTypes.customReports]: {
                title: 'Custom Reports',
                href: ANALYTICS_CUSTOM_REPORTS.path,
              },
            },
          },
        },
      ],
    },
  ];

  return filterItemsAndAddSpecialProps(menu, -1);
};

export const getUserMenuCollections = (userPermissions = []) => {
  const menu = [
    {
      title: 'Personal Settings',
      href: getHref(userPermissions, USER_RULES_SETTINGS),
    },
  ];

  return filterItemsAndAddSpecialProps(menu, -1);
};

export const getActiveIdOfParentMenuItem = (menu, pathname) => {
  let activeId = null;

  const check = (menu$, globalParentId) => {
    menu$.forEach((item) => {
      if (item.href) {
        const parent = item.href.split(/[/]/g)[1];
        const firstOccurrency = `${pathname.split(/[/]/g)[1]}`;

        if (
          parent === firstOccurrency ||
          // need because we have 2 separate routes for old and new analytics
          (parent === 'analytics' && firstOccurrency === 'analytics-new')
        ) {
          activeId = globalParentId || item.id;
        }
      } else {
        check(item.subMenu, item.id);
      }
    });
  };

  check(menu);

  return activeId;
};

export const isMobileApp = false;

export default {};
