"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getDateRangeByCycle = exports.formatPeriod = exports.dealYearsRange = exports.dealSelectedPeriodData = exports.computeYearIndex = void 0;
exports.getDefalutCycle = getDefalutCycle;
exports.getQuaterNum = getQuaterNum;

var _moment = _interopRequireDefault(require("moment"));

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

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

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 dealSelectedPeriodData = function dealSelectedPeriodData(period) {
  var option = {};
  option.cycleType = Number(period) === -1 ? "-1" : period.substring(0, 1);
  option.yqmNum = period.substring(2) === "" ? "0" : period.substring(2);
  return option;
};

exports.dealSelectedPeriodData = dealSelectedPeriodData;

var dealYearsRange = function dealYearsRange(yearsRange, showAllYears) {
  var yearsData = [];

  if (yearsRange.length) {
    if (yearsRange.length > 1 && showAllYears) {
      yearsData = [{
        title: "".concat((0, _getLocale.getLocale)("OKR_MyO_Butt_Allyears")),
        order: ["-1"],
        checked: false
      }];
    }

    var startYear = yearsRange[0];
    var endYear = yearsRange[yearsRange.length - 1];

    for (var i = startYear; i <= endYear; i += 1) {
      yearsData.push({
        checked: false,
        order: ["".concat(i)],
        title: "".concat(i)
      });
    }
  } else {
    var nowYear = new Date().getFullYear();
    yearsData = [{
      title: "".concat(nowYear),
      order: ["".concat(nowYear)],
      checked: false
    }];
  }

  return yearsData;
};

exports.dealYearsRange = dealYearsRange;

var formatPeriod = function formatPeriod(selected) {
  if (!selected) return "";
  var quarter = ["", "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_1stQuarter")), "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_2ndQuarter")), "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_3rdQuarter")), "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_4thQuarter"))];
  var cycleDate = "";
  var annualNum = Number("".concat(selected.annualNum)) === -1 ? "".concat((0, _getLocale.getLocale)("OKR_MyO_Butt_Allyears")) : "".concat(selected.annualNum).concat(_getLocale.isEn ? "" : "年");
  var month = (0, _constant.getMonth)();

  switch ("".concat(selected.cycleType)) {
    case "-1":
      cycleDate = "".concat((0, _getLocale.getLocale)("OKR_MyO_Butt_AllStatee"));
      break;

    case "1":
      cycleDate = "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_Year"));
      break;

    case "2":
      // 季度
      cycleDate = quarter[Number(selected.yqmNum)];
      break;

    case "3":
      cycleDate = month[Number(selected.yqmNum) - 1];
      break;

    case "4":
      cycleDate = "".concat(month[Number(selected.yqmNum) - 1], " - ").concat(month[((Number(selected.yqmNum) + 1) % 12 || 12) - 1]);
      break;

    case "5":
      cycleDate = Number("".concat(selected.yqmNum)) === 1 ? "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_Firsthalf")) : "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_Secondhalf"));
      break;

    default:
      cycleDate = "".concat((0, _getLocale.getLocale)("Mod_Customization"));
  }

  return "".concat(annualNum, "  ").concat(cycleDate);
};

exports.formatPeriod = formatPeriod;

var computeYearIndex = function computeYearIndex(currentYear, yearsData, computeType) {
  var newIndex;
  var currentYearIndex = yearsData.findIndex(function (item) {
    return Number(item.title) === currentYear;
  });

  if (computeType === "add") {
    newIndex = currentYearIndex + 1;
  } else {
    newIndex = currentYearIndex - 1;
  }

  return newIndex;
};

exports.computeYearIndex = computeYearIndex;

var getDateRangeByCycle = function getDateRangeByCycle(cycle) {
  var startMonth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;

  if (!cycle) {
    var start = (0, _moment["default"])();
    return {
      startDate: start.format("YYYY/MM/DD"),
      endDate: start.add(1, "M").format("YYYY/MM/DD")
    };
  }

  switch (cycle.cycleType) {
    case "-1":
      {
        return {
          startDate: "",
          endDate: ""
        };
      }

    case "1":
      {
        var _start = (0, _moment["default"])().year(Number(cycle.annualNum)).month(startMonth - 1).date(1);

        return {
          startDate: _start.format("YYYY/MM/DD"),
          endDate: _start.add(11, "M").endOf("M").format("YYYY/MM/DD")
        };
      }

    case "2":
      {
        var yearStart = (0, _moment["default"])().year(Number(cycle.annualNum)).month(startMonth - 1).date(1);
        var quarterStart = yearStart.add((Number(cycle.yqmNum) - 1) * 3, "M");
        return {
          startDate: quarterStart.format("YYYY/MM/DD"),
          endDate: quarterStart.add(2, "M").endOf("M").format("YYYY/MM/DD")
        };
      }

    case "3":
      {
        return {
          startDate: "".concat(cycle.annualNum, "/").concat(Number(cycle.yqmNum) < 10 ? "0".concat(cycle.yqmNum) : cycle.yqmNum, "/01"),
          endDate: (0, _moment["default"])("".concat(cycle.annualNum, "/").concat(cycle.yqmNum, "/", 1)).add(1, "month").set("date", 0).format("YYYY/MM/DD")
        };
      }

    case "4":
      {
        var _start2 = (0, _moment["default"])().year(Number(cycle.annualNum)).month(Number(cycle.yqmNum) - 1).date(1);

        return {
          startDate: _start2.format("YYYY/MM/DD"),
          endDate: _start2.add(1, "month").endOf("M").format("YYYY/MM/DD")
        };
      }

    case "5":
      {
        var _start3 = (0, _moment["default"])().year(Number(cycle.annualNum)).month(startMonth - 1).date(1);

        if (cycle.yqmNum === "2") _start3.add(6, "M");
        return {
          startDate: _start3.format("YYYY/MM/DD"),
          endDate: _start3.add(5, "month").endOf("M").format("YYYY/MM/DD")
        };
      }

    default:
      {
        return {
          startDate: (0, _moment["default"])().set("year", Number(cycle.annualNum)).format("YYYY/MM/DD"),
          endDate: (0, _moment["default"])().set("year", Number(cycle.annualNum)).add(30, "day").format("YYYY/MM/DD")
        };
      }
  }
};

exports.getDateRangeByCycle = getDateRangeByCycle;

function getQuaterNum(month) {
  var startMonth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  var m = month >= startMonth ? month : month + 12;
  return Math.floor((m - startMonth) / 3) + 1;
}

function getDefalutCycle(setting) {
  if (setting.isOpenQuarter) {
    var _cycle = {
      annualNum: "".concat(new Date().getFullYear()),
      cycleType: "2",
      yqmNum: "".concat(getQuaterNum(new Date().getMonth() + 1, setting.yearStartMonth))
    };
    return _objectSpread(_objectSpread({}, _cycle), getDateRangeByCycle(_cycle, setting.yearStartMonth));
  }

  if (setting.isOpenMonth) {
    var _cycle2 = {
      annualNum: "".concat(new Date().getFullYear()),
      cycleType: "3",
      yqmNum: "".concat(new Date().getMonth() + 1)
    };
    return _objectSpread(_objectSpread({}, _cycle2), getDateRangeByCycle(_cycle2, setting.yearStartMonth));
  }

  if (setting.isOpenBimonthly) {
    var month = new Date().getMonth() + 1;

    if (setting.yearStartMonth % 2 === 1) {
      month -= month % 2 === 1 ? 0 : 1;
    } else {
      month = month - month % 2 || 12;
    }

    var _cycle3 = {
      annualNum: "".concat(new Date().getFullYear()),
      cycleType: "4",
      yqmNum: "".concat(month)
    };
    return _objectSpread(_objectSpread({}, _cycle3), getDateRangeByCycle(_cycle3, setting.yearStartMonth));
  }

  if (setting.isOpenHalfYear) {
    var _cycle4 = {
      annualNum: "".concat(new Date().getFullYear()),
      cycleType: "5",
      yqmNum: "1"
    };
    return _objectSpread(_objectSpread({}, _cycle4), getDateRangeByCycle(_cycle4, setting.yearStartMonth));
  }

  var cycle = {
    annualNum: "".concat(new Date().getFullYear()),
    cycleType: "1",
    yqmNum: "1"
  };
  return _objectSpread({
    annualNum: "".concat(new Date().getFullYear()),
    cycleType: "1",
    yqmNum: "1"
  }, getDateRangeByCycle(cycle, setting.yearStartMonth));
}