"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

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

var _common = require("../../common");

var _mockData = require("../../mockData");

var _style = require("./style");

var _helper = require("../../helper");

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

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

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 i18nInfo = (0, _helper.getBSGlobal)("I18NInfo");

function PeriodList(_ref) {
  var actived = _ref.actived,
      periodData = _ref.periodData,
      onSelected = _ref.onSelected,
      style = _ref.style,
      _ref$size = _ref.size,
      size = _ref$size === void 0 ? "m" : _ref$size;

  var _useState = (0, _react.useState)(["0"]),
      _useState2 = _slicedToArray(_useState, 2),
      activeOrder = _useState2[0],
      setActiveOrder = _useState2[1];

  (0, _react.useEffect)(function () {
    setActiveOrder(actived);
  }, [actived]);
  return /*#__PURE__*/_react["default"].createElement(_style.List, {
    style: style
  }, periodData.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement(_style.Item, {
      className: item.order[0] == activeOrder[0] ? "active" : "",
      key: index,
      onClick: function onClick() {
        return onSelected(item.order[0]);
      },
      size: size
    }, item.title);
  }), new Array(6).fill(1).map(function () {
    return /*#__PURE__*/_react["default"].createElement(_style.PlaceHolder, {
      size: size
    });
  }));
}

function getDoubleMonthData(startMonth) {
  var start = startMonth % 2 == 1 ? 1 : 2;
  var month = start;

  var getNextMonth = function getNextMonth(month) {
    var step = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
    return (month + step) % 12 || 12;
  };

  var datas = [];
  var monthLang = (0, _constant.getMonth)();

  if (!i18nInfo || i18nInfo.I18NOpen && i18nInfo.I18NUserLang === "zh_CN") {
    datas.push({
      title: "".concat(start, " - ").concat(getNextMonth(start), "\u6708"),
      order: ["4,".concat(start)],
      checked: false
    });

    while (1) {
      month = getNextMonth(month, 2);
      if (month == start) break;
      datas.push({
        title: "".concat(month, " - ").concat(getNextMonth(month), "\u6708"),
        order: ["4,".concat(month)],
        checked: false
      });
    }
  } else {
    datas.push({
      title: "".concat(monthLang[start - 1], " - ").concat(monthLang[getNextMonth(start) - 1]),
      order: ["4,".concat(start)],
      checked: false
    });

    while (1) {
      month = getNextMonth(month, 2);
      if (month == start) break;
      datas.push({
        title: "".concat(monthLang[month - 1], " - ").concat(monthLang[getNextMonth(month) - 1]),
        order: ["4,".concat(month)],
        checked: false
      });
    }
  }

  return datas;
}

function Selector(_ref2) {
  var className = _ref2.className,
      onSelected = _ref2.onSelected,
      selected = _ref2.selected,
      showCustom = _ref2.showCustom,
      showFullCycle = _ref2.showFullCycle,
      showMonth = _ref2.showMonth,
      showQuarter = _ref2.showQuarter,
      showYear = _ref2.showYear,
      showHalfYear = _ref2.showHalfYear,
      showDoubleMonth = _ref2.showDoubleMonth,
      yearsRange = _ref2.yearsRange,
      wrapperStyle = _ref2.wrapperStyle,
      _ref2$startMonth = _ref2.startMonth,
      startMonth = _ref2$startMonth === void 0 ? 1 : _ref2$startMonth;

  var _useState3 = (0, _react.useState)("".concat(new Date().getFullYear())),
      _useState4 = _slicedToArray(_useState3, 2),
      annualNum = _useState4[0],
      setAnnualNum = _useState4[1];

  var _useState5 = (0, _react.useState)(0),
      _useState6 = _slicedToArray(_useState5, 2),
      annualNumIndex = _useState6[0],
      setAnnualNumIndex = _useState6[1];

  var _useState7 = (0, _react.useState)("-1"),
      _useState8 = _slicedToArray(_useState7, 2),
      cycleType = _useState8[0],
      setCycleType = _useState8[1];

  var _useState9 = (0, _react.useState)((0, _mockData.getMockPeriod)()),
      _useState10 = _slicedToArray(_useState9, 2),
      periodData = _useState10[0],
      setPeriodData = _useState10[1];

  var _useState11 = (0, _react.useState)("0"),
      _useState12 = _slicedToArray(_useState11, 2),
      yqmNum = _useState12[0],
      setYqmNum = _useState12[1];

  var yearsRangeLen = (0, _common.dealYearsRange)(yearsRange).length;

  function setYear(computeType, condition) {
    var yearsData = (0, _common.dealYearsRange)(yearsRange);

    if (condition) {
      var yearIndex = (0, _common.computeYearIndex)(Number(annualNum), yearsData, computeType);
      setAnnualNum(yearsData[yearIndex].title);
      setAnnualNumIndex(yearIndex);
      setCycleType("");
      setYqmNum("");
    }
  }

  function handleSelected(period) {
    var selected = _objectSpread({
      annualNum: annualNum,
      cycleType: (0, _common.dealSelectedPeriodData)(period).cycleType,
      yqmNum: (0, _common.dealSelectedPeriodData)(period).yqmNum
    }, (0, _common.dealSelectedPeriodData)(period));

    onSelected(_objectSpread(_objectSpread({}, selected), (0, _common.getDateRangeByCycle)(selected, startMonth)));
  }

  (0, _react.useEffect)(function () {
    setAnnualNum(selected.annualNum);
    setCycleType(selected.cycleType);
    setYqmNum(selected.yqmNum);
    setAnnualNumIndex((0, _common.dealYearsRange)(yearsRange).findIndex(function (item) {
      return Number(item.title) == Number(selected.annualNum);
    }));
    var periodDatas = [showFullCycle && {
      title: "".concat((0, _getLocale.getLocale)("OKR_MyO_Butt_AllStatee")),
      order: ["-1"],
      checked: false
    }, showYear && {
      title: "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_Year")),
      order: ["1"],
      checked: false
    }].filter(function (item) {
      return item;
    });

    if (showHalfYear) {
      periodDatas.push({
        title: "".concat((0, _getLocale.getLocale)("OKR_MyO_Pop_Firsthalf")),
        order: ["5,1"],
        checked: false
      }, {
        title: (0, _getLocale.getLocale)("OKR_MyO_Pop_Secondhalf"),
        order: ["5,2"],
        checked: false
      });
    }

    if (showQuarter) {
      periodDatas.push.apply(periodDatas, _toConsumableArray((0, _mockData.getMockQuarter)()));
    }

    if (showCustom) {
      periodDatas.push({
        title: (0, _getLocale.getLocale)("Mod_Customization"),
        order: ["0,0"],
        checked: false
      });
    }

    setPeriodData(periodDatas);
  }, [selected, showCustom, showFullCycle, showMonth, showQuarter, showYear]);
  return /*#__PURE__*/_react["default"].createElement(_style.Wrapper, {
    className: className,
    style: wrapperStyle
  }, /*#__PURE__*/_react["default"].createElement(_style.Head, null, yearsRange.length > 1 && /*#__PURE__*/_react["default"].createElement(_style.Icon, {
    className: "tu-icon-angle-left ".concat(yearsRangeLen > 1 && annualNumIndex != 0 ? "" : "disabled"),
    onClick: function onClick() {
      return setYear("subtract", yearsRangeLen > 1 && annualNumIndex != 0);
    }
  }), /*#__PURE__*/_react["default"].createElement(_style.Title, null, annualNum), yearsRange.length > 1 && /*#__PURE__*/_react["default"].createElement(_style.Icon, {
    className: "tu-icon-angle-right ".concat(yearsRangeLen > 1 && annualNumIndex != yearsRangeLen - 1 ? "" : "disabled"),
    onClick: function onClick() {
      return setYear("add", yearsRangeLen > 1 && annualNumIndex != yearsRangeLen - 1);
    }
  })), /*#__PURE__*/_react["default"].createElement(PeriodList, {
    actived: ["".concat(cycleType == "-1" ? "-1" : cycleType == "1" ? cycleType : "".concat(cycleType, ",").concat(yqmNum))],
    periodData: periodData,
    onSelected: function onSelected(period) {
      handleSelected(period);
    }
  }), showDoubleMonth && /*#__PURE__*/_react["default"].createElement(PeriodList, {
    actived: ["".concat(cycleType == "-1" ? "-1" : cycleType == "1" ? cycleType : "".concat(cycleType, ",").concat(yqmNum))],
    periodData: getDoubleMonthData(startMonth),
    onSelected: function onSelected(period) {
      handleSelected(period);
    }
  }), showMonth && /*#__PURE__*/_react["default"].createElement(PeriodList, {
    actived: ["".concat(cycleType == "-1" ? "-1" : cycleType == "1" ? cycleType : "".concat(cycleType, ",").concat(yqmNum))],
    size: "s",
    periodData: (0, _mockData.getMockMonth)(),
    onSelected: function onSelected(period) {
      handleSelected(period);
    }
  }));
}

var _default = Selector;
exports["default"] = _default;