"use strict";

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

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

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

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

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

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 _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 _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 _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; }

// 扁平数据转换为树
var toTree = function toTree(data) {
  var cloneData = (0, _cloneDeep["default"])(data);
  var map = {};
  var res = [];
  cloneData.forEach(function (item) {
    return map[item.Id] = item;
  });
  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;

var formatTaskData = function formatTaskData(data) {
  var result = {
    1: [],
    2: [],
    3: []
  };
  if (!data.length) return result;
  data.forEach(function (item) {
    if (item.Status === 2) {
      result[1] = toTree(item.Rows);
    } else if (item.Status === 1) {
      result[2] = toTree(item.Rows);
    } else if (item.Status === 3) {
      result[3] = toTree(item.Rows);
    }
  });
  return result;
};

exports.formatTaskData = formatTaskData;

var getStatusStatistic = function getStatusStatistic(data) {
  var result = {
    done: 0,
    doing: 0,
    overDue: 0
  };
  if (!data.length) return result;
  data.forEach(function (items) {
    // 1 进行中 2 已完成 3已延期
    if (items.Status === 3) {
      items.Rows.forEach(function (item) {
        if (item.Status === 3) {
          result.overDue += 1;
        }
      });
    } else if (items.Status === 1) {
      items.Rows.forEach(function (item) {
        if (item.Status === 1) {
          result.doing += 1;
        }
      });
    } else if (items.Status === 2) {
      items.Rows.forEach(function (item) {
        if (item.Status === 2) {
          result.done += 1;
        }
      });
    }
  });
  return result;
};

exports.getStatusStatistic = getStatusStatistic;

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

  if (type === _interface.EReportType.Weekly) {
    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[Number(week) - 1].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, "/").concat(weeks[Number(week) - 1].endTime);
  }

  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");
    }

    if (type === 2) {
      return (0, _getLocale.getLocale)("Rep_NewS_ModifOKR");
    }
  }

  return "";
};

exports.getMapText = getMapText;