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(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(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; }

/* eslint-disable no-plusplus */
import omit from 'lodash/omit';
import isEmpty from 'lodash/isEmpty';
import { itemTypeMappings } from "./mappings";
var CLU_ITEM = itemTypeMappings.CLU_ITEM,
    NODE_ITEM = itemTypeMappings.NODE_ITEM;

function formatCluster(value, expandsKeys) {
  if (isEmpty(value)) return [];
  var flatted = [];

  function flatData(data) {
    var prevKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
    var itemType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : CLU_ITEM;

    for (var i = 0; i < data.length; i++) {
      var node = data[i];
      var key = void 0;
      var peerNode = null;

      if (Object.prototype.toString.call(node) !== '[object String]') {
        peerNode = omit(node, ['nodes']);
        key = prevKey ? "".concat(prevKey, "-").concat(node.id) : String(node.id);
      } else {
        key = prevKey ? "".concat(prevKey, "-").concat(node) : String(node);
      }

      flatted.push(_objectSpread(_objectSpread({}, peerNode), {}, {
        name: node.name || node.code || node,
        expand: expandsKeys.includes(key),
        parentId: prevKey || '0',
        itemType: itemType,
        key: key
      }));
      var children = node.nodes;

      if (!isEmpty(children)) {
        flatData(children, key, NODE_ITEM);
      }
    }
  }

  flatData(value);
  return flatted;
}

function handleSelect(record, store) {
  var id = record.get('id');
  var itemType = record.get('itemType');
  var parentId = record.get('parentId');
  var key = record.get('key');
  var name = record.get('name');
  var type = record.get('type');
  var status = record.get('status');
  store.setSelectedMenu({
    id: id,
    itemType: itemType,
    parentId: parentId,
    key: key,
    name: name,
    type: type,
    status: status
  });
}

export default (function (store, projectId) {
  return {
    autoQuery: true,
    paging: false,
    selection: 'single',
    parentField: 'parentId',
    expandField: 'expand',
    dateKey: null,
    idField: 'key',
    fields: [{
      name: 'id',
      type: 'string'
    }, {
      name: 'name',
      type: 'string'
    }, {
      name: 'key',
      type: 'string'
    }, {
      name: 'parentId',
      type: 'string'
    }, {
      name: 'itemType',
      type: 'string'
    }],
    events: {
      select: function select(_ref) {
        var record = _ref.record;
        handleSelect(record, store);
      },
      unSelect: function unSelect(_ref2) {
        var record = _ref2.record;
        record.isSelected = true;
      }
    },
    transport: {
      read: {
        url: "/devops/v1/projects/".concat(projectId, "/clusters/tree_menu"),
        method: 'get',
        transformResponse: function transformResponse(response) {
          try {
            var res = JSON.parse(response);

            if (res && res.failed) {
              return res;
            } else {
              var expandsKeys = store.getExpandedKeys;
              return formatCluster(res, expandsKeys);
            }
          } catch (e) {
            return response;
          }
        }
      }
    }
  };
});