import { isUrl } from '@ant-design/pro-utils';

import { FILTER_FOLER_REG } from './constants';
import { transformURL } from './utils';

export const guid = function(prefix) {
  let date = new Date().getTime();
  if (window.performance && typeof window.performance.now === 'function') {
    date += performance.now();
  }
  let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(
    c,
  ) {
    const r = (date + Math.random() * 16) % 16 | 0;
    date = Math.floor(date / 16);
    return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
  });
  return prefix + uuid.replace(/-/g, '.');
};
// web5 web4路由菜单生成

//重写菜单生成, web4和web5分开
const getPosition = (level, index) => {
  return `${level}-${index}`;
};
const getKey = (key, pos) => {
  if (key !== null && key !== undefined) {
    return key;
  }
  return pos;
};
const level = 0;
const getURL = (url) => {
  const i = url.indexOf('#')
  const href = i >= 0 ? url.slice(0, i) : url;
  return href;
}

const generRotes = (widgets, parent, level = 0) => {
  let ret = [];
  widgets.forEach((item, index) => {
    ;
    if (item.hasOwnProperty('widgets')) {
      const _level_ = level + 1;
      const path = `/civweb/${guid('web_console')}`;
      const subKey = guid('panda');
      ret.push({
        name: item.label,
        level: _level_,
        path: path,
        component: 'BasicLayout',
        parent: parent,
        routes: generRotes(item.widgets, Object.assign({}, item, {path: path, key: subKey}), _level_),
        extData: { ...item, icon: item && item.icon.replace(/\s*/g, '') || '_' },
      });
    } else {
      
      const baseURL = FILTER_FOLER_REG.test(item.url)
        ? 'civweb4'
        : item.url.replace(/\s/g, '') === ''
        ? ''
        : 'civweb5';
      let url = item.hasOwnProperty('url') && !isUrl(url) && baseURL !== '' ? `/${baseURL}/${item.url.replace(/\s*/g, '')}` : !item.hasOwnProperty('url') ? guid('web_console'): '';
      if(url.includes("url=")) {
        url = url.split("url=")[1];
        url = getURL(url);
      }
     
      const l = level ? level + 1 : -1
      const common = {
        name: item.label,
        level: l,
        component: 'BasicLayout',
        path: baseURL !== '' ? transformURL(url) :  '',
        href: url,
        target: isUrl(url) ? '_blank': '',
        key: guid('panda'),
        hideInMenu: l > 3 ? true: false,
        extData: { ...item, icon: item && item.icon.replace(/\s*/g, '') || '_' },
        parent: parent
      };
      ret.push(common);
    }
  });
  return ret;
};
export function simpleNormalizeChildren(children) {
  for (let i = 0; i < children.length; i++) {
    if (Array.isArray(children[i])) {
      return Array.prototype.concat.apply([], children);
    }
  }
  return children;
}

export const generFlatRoutes = (widgets, parent, subSystem) => {
  let treeKeys = []
  let flatMenu = [];
  (widgets || []).forEach(item => {
    if (item.hasOwnProperty('routes')) {
      const route = generFlatRoutes(item.routes, item, item.name);
      flatMenu.push(simpleNormalizeChildren(route));
    } else {
      flatMenu.push({
        ...item,
        treeKeys,
        subSystem: subSystem,
        parent: parent,
      });
    }
  });
  return simpleNormalizeChildren(flatMenu);
};

export default generRotes;
