function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
export var convertWorkloadType = function convertWorkloadType() {
  var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  switch (type.toLowerCase()) {
    case 'deployment':
      return 'Deployment';
    case 'statefulset':
      return 'StatefulSet';
    case 'daemonset':
      return 'DaemonSet';
    case 'job':
      return 'Job';
    case 'cronjob':
      return 'CronJob';
    default:
      return '';
  }
};
export var fetchMultiParamsItem = function fetchMultiParamsItem() {
  var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var kinds = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  var result = list.join(',') || '*';
  if (kinds.length > 0 && kinds[0] != '*' && (list.length === 0 || list[0] === '*')) {
    var arr = kinds.map(function (item) {
      return "".concat(item, ":*");
    });
    result = arr.join(',');
  }
  return result;
};
export var fetchMultiWorkloadNames = function fetchMultiWorkloadNames() {
  var _params$workloadName;
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var components = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  if ((params === null || params === void 0 || (_params$workloadName = params.workloadName) === null || _params$workloadName === void 0 ? void 0 : _params$workloadName.length) > 0 && params.workloadName[0] != '*') {
    return params.workloadName.join(',');
  }
  var workloadTypes = params.workloadType || [];
  var workloads = [];
  if (workloadTypes.length === 0 || workloadTypes[0] === '*') {
    workloads = components.filter(function (item) {
      return item.namespace === params.namespace;
    }) || [];
  } else {
    workloads = components.filter(function (item) {
      return item.namespace === params.namespace && workloadTypes.includes(convertWorkloadType(item.workloadType));
    }) || [];
  }
  var names = workloads.map(function (item) {
    return "".concat(convertWorkloadType(item.workloadType), ":").concat(item.name);
  });
  return Array.from(new Set(names)).join(',') || '*';
};
export var fetchDateStr = function fetchDateStr() {
  return new Date().toJSON().split('.')[0].replace('T', '').replace(/-/g, '').replace(/:/g, '');
};
export var downlaodFiles = function downlaodFiles(data, filename) {
  var blob = new Blob([data], {
    type: 'application/octet-stream'
  });
  var url = window.URL.createObjectURL(blob);
  var link = document.createElement('a');
  link.href = url;
  link.download = filename;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  window.URL.revokeObjectURL(url);
};
export var fetchStrList = function fetchStrList() {
  var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var key = arguments.length > 1 ? arguments[1] : undefined;
  var newList = [];
  list.forEach(function (item) {
    if (Array.isArray(item === null || item === void 0 ? void 0 : item.options) && item.options.length > 0) {
      item.options.forEach(function (opt) {
        if (opt && opt[key]) {
          newList.push(opt[key]);
        }
      });
    } else if (item && item[key]) {
      newList.push(item[key]);
    }
  });
  return Array.from(new Set(newList));
};
export var fetchValidMetadata = function fetchValidMetadata(metadataData, isCompo) {
  var _data2;
  var workloads = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
  var keys = ['containers', 'pods', 'workloads', 'namespaces'];
  var data = null;
  var indexPattern = null;
  var filters = [];
  for (var _i = 0, _keys = keys; _i < _keys.length; _i++) {
    var _item$filter, _filters2;
    var key = _keys[_i];
    var item = metadataData[key];
    var flag = isCompo && key === 'workloads' && (!(item !== null && item !== void 0 && item.filter) || (item === null || item === void 0 || (_item$filter = item.filter) === null || _item$filter === void 0 ? void 0 : _item$filter.length) === 0);
    if (!data && item !== null && item !== void 0 && item.value && item.value[0] !== '*') {
      var _filters, _data;
      data = item;
      if (!flag && ((_filters = filters) === null || _filters === void 0 ? void 0 : _filters.length) === 0) filters = (_data = data) === null || _data === void 0 ? void 0 : _data.filter;
    }
    if (!indexPattern && item && item.indexPattern) {
      indexPattern = item.indexPattern;
    }
    if (flag && ((_filters2 = filters) === null || _filters2 === void 0 ? void 0 : _filters2.length) === 0) {
      filters = fetchStrList(workloads, 'filter');
    }
  }
  return _objectSpread(_objectSpread({}, data), {}, {
    indexPattern: (_data2 = data) !== null && _data2 !== void 0 && _data2.indexPattern ? data.indexPattern : indexPattern,
    filter: filters.length > 0 ? Array.from(new Set(filters)) : []
  });
};
export var fetchOptionsGroup = function fetchOptionsGroup(newList, oldList) {
  var nList = fetchStrList(newList, 'value');
  var oList = fetchStrList(oldList, 'value');
  return (nList === null || nList === void 0 ? void 0 : nList.length) > 0 ? nList : oList || [];
};
export var updateWorkloadOptions = function updateWorkloadOptions() {
  var list = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  list.forEach(function (item) {
    if (item.value && item.value !== '*' && item.type) {
      item.value = "".concat(item.type, ":").concat(item.value);
    }
    if (item.options && Array.isArray(item.options)) {
      updateWorkloadOptions(item.options);
    }
  });
  return list;
};