"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.simpleNormalizeChildren = simpleNormalizeChildren;
exports["default"] = exports.generFlatRoutes = exports.guid = void 0;

var _proUtils = require("@ant-design/pro-utils");

var _constants = require("./constants");

var _utils = require("./utils");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var guid = function guid(prefix) {
  var date = new Date().getTime();

  if (window.performance && typeof window.performance.now === 'function') {
    date += performance.now();
  }

  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var 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分开


exports.guid = guid;

var getPosition = function getPosition(level, index) {
  return "".concat(level, "-").concat(index);
};

var getKey = function getKey(key, pos) {
  if (key !== null && key !== undefined) {
    return key;
  }

  return pos;
};

var level = 0;

var getURL = function getURL(url) {
  var i = url.indexOf('#');
  var href = i >= 0 ? url.slice(0, i) : url;
  return href;
};

var generRotes = function generRotes(widgets, parent) {
  var level = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var ret = [];
  widgets.forEach(function (item, index) {
    ;

    if (item.hasOwnProperty('widgets')) {
      var _level_ = level + 1;

      var path = "/civweb/".concat(guid('web_console'));
      var 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: _objectSpread({}, item, {
          icon: item && item.icon.replace(/\s*/g, '') || '_'
        })
      });
    } else {
      var baseURL = _constants.FILTER_FOLER_REG.test(item.url) ? 'civweb4' : item.url.replace(/\s/g, '') === '' ? '' : 'civweb5';
      var url = item.hasOwnProperty('url') && !(0, _proUtils.isUrl)(url) && baseURL !== '' ? "/".concat(baseURL, "/").concat(item.url.replace(/\s*/g, '')) : !item.hasOwnProperty('url') ? guid('web_console') : '';

      if (url.includes("url=")) {
        url = url.split("url=")[1];
        url = getURL(url);
      }

      var l = level ? level + 1 : -1;
      var common = {
        name: item.label,
        level: l,
        component: 'BasicLayout',
        path: baseURL !== '' ? (0, _utils.transformURL)(url) : '',
        href: url,
        target: (0, _proUtils.isUrl)(url) ? '_blank' : '',
        key: guid('panda'),
        hideInMenu: l > 3 ? true : false,
        extData: _objectSpread({}, item, {
          icon: item && item.icon.replace(/\s*/g, '') || '_'
        }),
        parent: parent
      };
      ret.push(common);
    }
  });
  return ret;
};

function simpleNormalizeChildren(children) {
  for (var i = 0; i < children.length; i++) {
    if (Array.isArray(children[i])) {
      return Array.prototype.concat.apply([], children);
    }
  }

  return children;
}

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

exports.generFlatRoutes = generFlatRoutes;
var _default = generRotes;
exports["default"] = _default;