"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toTree = exports.getIfInitCondition = exports.getETreeNode = exports.formatTreeDataToNode = exports.formatEListDataToSelectTree = exports.formatCondition = void 0;

var _cloneDeep = _interopRequireDefault(require("clone-deep"));

var _constant = require("./constant");

var _bsGlobal = require("../../../utils/bs-global");

var _getLocale = require("../../../utils/getLocale");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : 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; }

// 扁平数据转换为树
var toTree = function toTree(data) {
  var cloneData = (0, _cloneDeep["default"])(data);
  var map = {};
  var res = [];
  cloneData.forEach(function (item) {
    return map[item.taskId] = item;
  });
  cloneData.forEach(function (item) {
    var parent = map[item.parentTaskId];

    if (parent) {
      if (!parent.children) parent.children = [];
      parent.children.push(item);
    } else {
      res.push(item);
    }
  });
  return res;
};

exports.toTree = toTree;

var getETreeNode = function getETreeNode(tasks, isFirstLevel, createUserId, krUser, krAiteUsers) {
  // 只有第一级的e为空的时候返回空节点信息
  if ((!tasks || tasks.length === 0) && isFirstLevel) {
    return [{
      key: 0,
      index: 0,
      title: "",
      nodeType: "e-empty",
      isLeaf: true,
      children: [],
      data: {}
    }];
  } // 针对多级e情况下，最后一级e的tasks为空


  if (!tasks) {
    return [];
  }

  return tasks.map(function (task, index) {
    return {
      key: task.taskId,
      index: index,
      title: task.taskName,
      nodeType: task.objType == 4 ? "e-project" : isFirstLevel ? "e-task" : "e-task-level2",
      isLeaf: task.children && task.children.length === 0,
      children: getETreeNode(task.children, false, createUserId, krUser, krAiteUsers),
      data: _objectSpread({}, task),
      createUserId: createUserId,
      krUser: _objectSpread({}, krUser),
      krAiteUsers: krAiteUsers
    };
  });
};

exports.getETreeNode = getETreeNode;

var formatTreeDataToNode = function formatTreeDataToNode(tree, isInitCondition) {
  if (isInitCondition) {
    return tree.map(function (item, index) {
      return {
        key: item.mileStoneId,
        index: index,
        title: item.mileStoneId !== 0 ? item.mileStoneName : (0, _getLocale.getLocale)("OKR_MyO_Butt_Otherexecutions"),
        nodeType: "e-kr",
        isLeaf: false,
        children: getETreeNode(item.tasks, true, item.createUserId, item.user, item.aiteUsers),
        data: _objectSpread({}, item),
        isOTask: item.mileStoneId === 0
      };
    });
  }

  var result = [];
  tree.forEach(function (item, index) {
    if (item.tasks && item.tasks.length > 0) {
      result.push({
        key: item.mileStoneId,
        index: index,
        title: item.mileStoneId !== 0 ? item.mileStoneName : (0, _getLocale.getLocale)("OKR_MyO_Butt_Otherexecutions"),
        nodeType: "e-kr",
        isLeaf: false,
        children: getETreeNode(item.tasks, true, item.createUserId, item.user, item.aiteUsers),
        data: _objectSpread({}, item),
        isOTask: item.mileStoneId === 0
      });
    }
  });
  return result;
};

exports.formatTreeDataToNode = formatTreeDataToNode;

var formatEListDataToSelectTree = function formatEListDataToSelectTree(data, workId, isInitCondition) {
  var tree = data.map(function (item) {
    return _objectSpread(_objectSpread({}, item), {}, {
      tasks: toTree(item.tasks)
    });
  });
  var formatKrsTreeDataToNode = formatTreeDataToNode(tree.filter(function (d) {
    return d.mileStoneId !== 0;
  }), isInitCondition);
  var formatOTreeDataToNode = formatTreeDataToNode(tree.filter(function (d) {
    return d.mileStoneId === 0;
  }), isInitCondition);
  var result = {
    krsEList: formatKrsTreeDataToNode,
    oEList: isInitCondition && formatOTreeDataToNode.length === 0 ? [[{
      key: 0,
      index: 0,
      title: (0, _getLocale.getLocale)("OKR_MyO_Butt_Otherexecutions"),
      nodeType: "e-kr",
      isLeaf: false,
      children: [{
        key: "1",
        index: 0,
        title: "",
        nodeType: "e-empty",
        isLeaf: true,
        children: [],
        data: {}
      }],
      data: {
        workId: workId,
        mileStoneId: 0
      },
      isOTask: true
    }]] : [formatOTreeDataToNode]
  };
  return result;
};

exports.formatEListDataToSelectTree = formatEListDataToSelectTree;

var formatCondition = function formatCondition(_ref) {
  var okrId = _ref.okrId,
      noteOrder = _ref.noteOrder,
      selectName = _ref.selectName,
      checkedStatus = _ref.checkedStatus,
      users = _ref.users,
      keyWord = _ref.keyWord,
      startDate = _ref.startDate,
      endDate = _ref.endDate,
      toLevel = _ref.toLevel,
      IncludeNoTaskKr = _ref.IncludeNoTaskKr,
      IsDateFilter = _ref.IsDateFilter;
  var resultStatus = [];

  if (checkedStatus.includes("all")) {
    resultStatus = [0, 1, 2, 3, 4, 6];
  } else {
    checkedStatus.forEach(function (statusCondition) {
      resultStatus.push(_constant.EStatusMap[statusCondition]);
    });
  }

  return {
    sortType: selectName ? selectName === "orderBySystem" ? 1 : _constant.SortTypeMap[selectName][noteOrder] : 1,
    work_id: okrId,
    startNum: 1,
    count: 1000,
    relation: 1,
    detail_type: "pq",
    appId: 1,
    appType: 1,
    taskType: 0,
    COUNT: 1000,
    to_user_id: (0, _bsGlobal.getUserInfo)().Id,
    pageIndex: 1,
    FeedId: 0,
    filter: 1,
    toLevel: toLevel || 999,
    app_id: 1,
    app_type: 1,
    FilterKey: keyWord,
    startDate: startDate || '',
    endDate: endDate || '',
    IncludeNoTaskKr: IncludeNoTaskKr,
    IsDateFilter: IsDateFilter,
    status: resultStatus.join(","),
    userObjects: users.map(function (user) {
      return {
        id: user.userId,
        name: user.name,
        email: user.email
      };
    })
  };
};

exports.formatCondition = formatCondition;

var getIfInitCondition = function getIfInitCondition(selectName, checkedStatus, users, keyWord) {
  if (keyWord || selectName !== "orderBySystem" || checkedStatus.length !== 0 || users.length !== 0) {
    return false;
  }

  return true;
};

exports.getIfInitCondition = getIfInitCondition;