/* eslint-disable */
/* prettier-ignore */
// Generated by elegant-router
// Read more: https://github.com/soybeanjs/elegant-router

import type { RouteRecordRaw, RouteComponent } from 'vue-router';
import type { ElegantConstRoute } from '@elegant-router/vue';
import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types';

/**
 * transform elegant const routes to vue routes
 * @param routes elegant const routes
 * @param layouts layout components
 * @param views view components
 */
export function transformElegantRoutesToVueRoutes(
  routes: ElegantConstRoute[],
  layouts: Record<string, RouteComponent | (() => Promise<RouteComponent>)>,
  views: Record<string, RouteComponent | (() => Promise<RouteComponent>)>
) {
  return routes.flatMap(route => transformElegantRouteToVueRoute(route, layouts, views));
}

/**
 * transform elegant route to vue route
 * @param route elegant const route
 * @param layouts layout components
 * @param views view components
 */
function transformElegantRouteToVueRoute(
  route: ElegantConstRoute,
  layouts: Record<string, RouteComponent | (() => Promise<RouteComponent>)>,
  views: Record<string, RouteComponent | (() => Promise<RouteComponent>)>
) {
  const LAYOUT_PREFIX = 'layout.';
  const VIEW_PREFIX = 'view.';
  const ROUTE_DEGREE_SPLITTER = '_';
  const FIRST_LEVEL_ROUTE_COMPONENT_SPLIT = '$';

  function isLayout(component: string) {
    return component.startsWith(LAYOUT_PREFIX);
  }

  function getLayoutName(component: string) {
    const layout = component.replace(LAYOUT_PREFIX, '');

    if(!layouts[layout]) {
      throw new Error(`Layout component "${layout}" not found`);
    }

    return layout;
  }

  function isView(component: string) {
    return component.startsWith(VIEW_PREFIX);
  }

  function getViewName(component: string) {
    const view = component.replace(VIEW_PREFIX, '');

    if(!views[view]) {
      throw new Error(`View component "${view}" not found`);
    }

    return view;
  }

  function isFirstLevelRoute(item: ElegantConstRoute) {
    return !item.name.includes(ROUTE_DEGREE_SPLITTER);
  }

  function isSingleLevelRoute(item: ElegantConstRoute) {
    return isFirstLevelRoute(item) && !item.children?.length;
  }

  function getSingleLevelRouteComponent(component: string) {
    const [layout, view] = component.split(FIRST_LEVEL_ROUTE_COMPONENT_SPLIT);

    return {
      layout: getLayoutName(layout),
      view: getViewName(view)
    };
  }

  const vueRoutes: RouteRecordRaw[] = [];

  // add props: true to route
  if (route.path.includes(':') && !route.props) {
    route.props = true;
  }

  const { name, path, component, children, ...rest } = route;

  const vueRoute = { name, path, ...rest } as RouteRecordRaw;

  try {
    if (component) {
      if (isSingleLevelRoute(route)) {
        const { layout, view } = getSingleLevelRouteComponent(component);

        const singleLevelRoute: RouteRecordRaw = {
          path,
          component: layouts[layout],
          meta: {
            title: route.meta?.title || ''
          },
          children: [
            {
              name,
              path: '',
              component: views[view],
              ...rest
            } as RouteRecordRaw
          ]
        };

        return [singleLevelRoute];
      }

      if (isLayout(component)) {
        const layoutName = getLayoutName(component);

        vueRoute.component = layouts[layoutName];
      }

      if (isView(component)) {
        const viewName = getViewName(component);

        vueRoute.component = views[viewName];
      }

    }
  } catch (error: any) {
    console.error(`Error transforming route "${route.name}": ${error.toString()}`);
    return [];
  }

  // add redirect to child
  if (children?.length && !vueRoute.redirect) {
    vueRoute.redirect = {
      name: children[0].name
    };
  }

  if (children?.length) {
    const childRoutes = children.flatMap(child => transformElegantRouteToVueRoute(child, layouts, views));

    if(isFirstLevelRoute(route)) {
      vueRoute.children = childRoutes;
    } else {
      vueRoutes.push(...childRoutes);
    }
  }

  vueRoutes.unshift(vueRoute);

  return vueRoutes;
}

/**
 * map of route name and route path
 */
const routeMap: RouteMap = {
  "root": "/",
  "not-found": "/:pathMatch(.*)*",
  "exception": "/exception",
  "exception_403": "/exception/403",
  "exception_404": "/exception/404",
  "exception_500": "/exception/500",
  "document": "/document",
  "document_project": "/document/project",
  "document_project-link": "/document/project-link",
  "document_video": "/document/video",
  "document_vue": "/document/vue",
  "document_vite": "/document/vite",
  "document_unocss": "/document/unocss",
  "document_naive": "/document/naive",
  "document_pro-naive": "/document/pro-naive",
  "document_antd": "/document/antd",
  "document_alova": "/document/alova",
  "403": "/403",
  "404": "/404",
  "500": "/500",
  "about": "/about",
  "config": "/config",
  "config_dict": "/config/dict",
  "config_fright-config": "/config/fright-config",
  "config_order-split": "/config/order-split",
  "delivery": "/delivery",
  "delivery_after-sales-order": "/delivery/after-sales-order",
  "delivery_normal-order": "/delivery/normal-order",
  "delivery_ponits-details": "/delivery/ponits-details",
  "delivery_split-order": "/delivery/split-order",
  "delivery_split-order-detail": "/delivery/split-order-detail",
  "finance": "/finance",
  "finance_commodity-freight": "/finance/commodity-freight",
  "finance_summary": "/finance/summary",
  "goods": "/goods",
  "goods_desk-category": "/goods/desk-category",
  "goods_store-goods": "/goods/store-goods",
  "goods_tag": "/goods/tag",
  "government": "/government",
  "government_government-list": "/government/government-list",
  "government_points": "/government/points",
  "government_user-list": "/government/user-list",
  "home": "/home",
  "iframe-page": "/iframe-page/:url",
  "login": "/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?",
  "manage": "/manage",
  "manage_config": "/manage/config",
  "manage_department": "/manage/department",
  "manage_log": "/manage/log",
  "manage_menu": "/manage/menu",
  "manage_role": "/manage/role",
  "manage_schedule": "/manage/schedule",
  "manage_user": "/manage/user",
  "operation": "/operation",
  "operation_advertisement": "/operation/advertisement",
  "operation_delivery-admin": "/operation/delivery-admin",
  "operation_search": "/operation/search",
  "plugin": "/plugin",
  "plugin_barcode": "/plugin/barcode",
  "plugin_charts": "/plugin/charts",
  "plugin_charts_antv": "/plugin/charts/antv",
  "plugin_charts_echarts": "/plugin/charts/echarts",
  "plugin_charts_vchart": "/plugin/charts/vchart",
  "plugin_copy": "/plugin/copy",
  "plugin_editor": "/plugin/editor",
  "plugin_editor_markdown": "/plugin/editor/markdown",
  "plugin_editor_quill": "/plugin/editor/quill",
  "plugin_excel": "/plugin/excel",
  "plugin_gantt": "/plugin/gantt",
  "plugin_gantt_dhtmlx": "/plugin/gantt/dhtmlx",
  "plugin_gantt_vtable": "/plugin/gantt/vtable",
  "plugin_icon": "/plugin/icon",
  "plugin_map": "/plugin/map",
  "plugin_pdf": "/plugin/pdf",
  "plugin_pinyin": "/plugin/pinyin",
  "plugin_print": "/plugin/print",
  "plugin_swiper": "/plugin/swiper",
  "plugin_tables": "/plugin/tables",
  "plugin_tables_vtable": "/plugin/tables/vtable",
  "plugin_typeit": "/plugin/typeit",
  "plugin_video": "/plugin/video",
  "pro-naive": "/pro-naive",
  "pro-naive_form": "/pro-naive/form",
  "pro-naive_form_basic": "/pro-naive/form/basic",
  "pro-naive_form_query": "/pro-naive/form/query",
  "pro-naive_form_step": "/pro-naive/form/step",
  "pro-naive_table": "/pro-naive/table",
  "pro-naive_table_remote": "/pro-naive/table/remote",
  "pro-naive_table_row-edit": "/pro-naive/table/row-edit",
  "store-management": "/store-management",
  "store-management_store-info": "/store-management/store-info",
  "user-center": "/user-center",
  "user-management": "/user-management",
  "user-management_user-list": "/user-management/user-list",
  "user-management_user-reviews": "/user-management/user-reviews"
};

/**
 * get route path by route name
 * @param name route name
 */
export function getRoutePath<T extends RouteKey>(name: T) {
  return routeMap[name];
}

/**
 * get route name by route path
 * @param path route path
 */
export function getRouteName(path: RoutePath) {
  const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][];

  const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null;

  return routeName;
}
