"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toTree = exports.getStatus = exports.getMapText = exports.getMapDeleteText = exports.formatWorkData = exports.formatTime = void 0;

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

var _helpers = require("../../../../../utils/helpers");

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

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

var _interface = require("./interface");

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

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

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 formatWorkData = function formatWorkData() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var milestoneSwitch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  var workTaskSwitch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
  var result = [];
  var list = (data || []).map(function (item) {
    return _objectSpread(_objectSpread({}, item), {}, {
      type: 'work'
    });
  });
  var newList = list.map(function (item) {
    var childMilestones = (item.Milestones || []).map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        type: 'milestones'
      });
    });
    var childWorkTasks = (item.Tasks || []).map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        type: 'workTasks'
      });
    });

    if (milestoneSwitch && !workTaskSwitch) {
      item.children = childMilestones;
    } else if (!milestoneSwitch && workTaskSwitch) {
      item.children = childWorkTasks;
    } else if (milestoneSwitch && workTaskSwitch) {
      item.children = toTree([].concat(_toConsumableArray(childMilestones), _toConsumableArray(childWorkTasks)));
    }

    return item;
  });
  result = toTree(newList);
  return result;
};

exports.formatWorkData = formatWorkData;

var formatTime = function formatTime(type, date) {
  if (type === _interface.EReportType.Daily) {
    return date.replace(/-/g, "/");
  } else if (type === _interface.EReportType.Weekly) {
    var _weeks;

    var _date$split = date.split("-"),
        _date$split2 = _slicedToArray(_date$split, 3),
        year = _date$split2[0],
        month = _date$split2[1],
        week = _date$split2[2];

    var weeks = (0, _helpers.getPointYearMonthWeeksRange)(year, month);
    var startDate = (_weeks = weeks[Number(week) - 1]) === null || _weeks === void 0 ? void 0 : _weeks.startTime;
    var endTime = weeks[Number(week) - 1].endTime;

    var _startDate$replace$sp = startDate.replace("/", "-").split("-"),
        _startDate$replace$sp2 = _slicedToArray(_startDate$replace$sp, 1),
        startMonth = _startDate$replace$sp2[0];

    var _endTime$replace$spli = endTime.replace("/", "-").split("-"),
        _endTime$replace$spli2 = _slicedToArray(_endTime$replace$spli, 1),
        endMonth = _endTime$replace$spli2[0];

    return "".concat(Number(startMonth) > Number(endMonth) ? Number(year) + 1 : year, "/") + weeks[Number(week) - 1].endTime;
  } else if (type === _interface.EReportType.Month) {
    return date.replace(/-/g, "/");
  }
};

exports.formatTime = formatTime;

var getMapText = function getMapText(types, type) {
  if (types.includes(_constant.EObjType.Task)) {
    if (type === 1) {
      return (0, _getLocale.getLocale)("Rep_NewS_TargBeenMdif");
    } else if (type === 2) {
      return (0, _getLocale.getLocale)("Rep_NewS_Modiftaskk");
    }
  }

  return "";
};

exports.getMapText = getMapText;

var getStatus = function getStatus(status) {
  // 复制前面人的逻辑
  var obj = {
    1: 'normal',
    2: 'delay',
    3: 'risk'
  };
  return obj[status] || 'normal';
};

exports.getStatus = getStatus;

var getMapDeleteText = function getMapDeleteText(types) {
  if (types.includes(_constant.EObjType.Task)) {
    return (0, _getLocale.getLocale)("Rep_NewS_Afterdetask");
  } else if (types.includes(_constant.EObjType.Project)) {
    return (0, _getLocale.getLocale)("Rep_Tab_Asumaryution");
  }

  return (0, _getLocale.getLocale)("Rep_NewS_Afterdetask");
}; // 扁平数据转换为树


exports.getMapDeleteText = getMapDeleteText;

var toTree = function toTree(data) {
  var cloneData = (0, _cloneDeep["default"])(data);
  var map = {};
  var res = [];
  cloneData === null || cloneData === void 0 ? void 0 : cloneData.forEach(function (item) {
    return map[item.Id] = item;
  });
  cloneData === null || cloneData === void 0 ? void 0 : cloneData.forEach(function (item) {
    var parent = map[item.ParentId];

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

exports.toTree = toTree;