"use strict";

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

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

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 generRotes = function generRotes(widgets, parent) {
  var ret = [];
  widgets.forEach(function (item) {
    if (item.hasOwnProperty('widgets')) {
      ret.push({
        name: item.label,
        path: "/civweb/".concat(guid('web_console')),
        component: 'BasicLayout',
        routes: generRotes(item.widgets, item),
        extData: _objectSpread({}, item)
      });
    } else {
      var baseURL = _constants.FILTER_FOLER_REG.test(item.url) ? 'civweb4' : 'civweb5';
      var url = item.hasOwnProperty('url') ? "/".concat(baseURL, "/").concat(item.url.replace(/\s*/g, '') + (item.url.indexOf('?') > -1 ? "&fromSource=".concat(guid('panda')) : "?fromSource=".concat(guid('panda')))) : guid('web_console');
      ret.push({
        name: item.label,
        component: 'BasicLayout',
        path: url,
        key: guid('panda'),
        extData: _objectSpread({}, item)
      });
    }
  });
  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) {
  var flatMenu = [];
  (widgets || []).forEach(function (item) {
    if (item.hasOwnProperty('routes')) {
      var route = generFlatRoutes(item.routes, item);
      flatMenu.push(simpleNormalizeChildren(route));
    } else {
      flatMenu.push(_objectSpread({}, item, {
        parent: parent
      }));
    }
  });
  return simpleNormalizeChildren(flatMenu);
};

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