"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 _moment = _interopRequireDefault(require("moment"));

var _classnames = _interopRequireDefault(require("classnames"));

var _button = _interopRequireDefault(require("./components/button"));

var _selector = _interopRequireDefault(require("./components/selector"));

var _popup = _interopRequireDefault(require("../popup"));

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

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

require("./index.css");

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

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

function PeriodSelector(_ref) {
  var align = _ref.align,
      className = _ref.className,
      children = _ref.children,
      _ref$isFetch = _ref.isFetch,
      isFetch = _ref$isFetch === void 0 ? true : _ref$isFetch,
      onChange = _ref.onChange,
      getPopupContainer = _ref.getPopupContainer,
      selected = _ref.selected,
      lastSelected = _ref.lastSelected,
      selectorClass = _ref.selectorClass,
      _ref$showCustom = _ref.showCustom,
      showCustom = _ref$showCustom === void 0 ? true : _ref$showCustom,
      _ref$isShowCustom = _ref.isShowCustom,
      isShowCustom = _ref$isShowCustom === void 0 ? true : _ref$isShowCustom,
      _ref$showFullCycle = _ref.showFullCycle,
      showFullCycle = _ref$showFullCycle === void 0 ? true : _ref$showFullCycle,
      _ref$showMonth = _ref.showMonth,
      showMonth = _ref$showMonth === void 0 ? true : _ref$showMonth,
      _ref$showQuarter = _ref.showQuarter,
      showQuarter = _ref$showQuarter === void 0 ? true : _ref$showQuarter,
      _ref$showYear = _ref.showYear,
      showYear = _ref$showYear === void 0 ? true : _ref$showYear,
      _ref$showBimonthly = _ref.showBimonthly,
      showBimonthly = _ref$showBimonthly === void 0 ? true : _ref$showBimonthly,
      _ref$showHalfYear = _ref.showHalfYear,
      showHalfYear = _ref$showHalfYear === void 0 ? true : _ref$showHalfYear,
      _ref$startMonth = _ref.startMonth,
      startMonth = _ref$startMonth === void 0 ? 1 : _ref$startMonth,
      yearsRange = _ref.yearsRange,
      wrapperStyle = _ref.wrapperStyle,
      popClass = _ref.popClass,
      style = _ref.style,
      _ref$buttonClass = _ref.buttonClass,
      buttonClass = _ref$buttonClass === void 0 ? '' : _ref$buttonClass,
      _ref$noBorder = _ref.noBorder,
      noBorder = _ref$noBorder === void 0 ? false : _ref$noBorder,
      onPopupVisibleChange = _ref.onPopupVisibleChange;

  var _useState = (0, _react.useState)({
    hasSetting: true,
    isOpenCustom: showCustom,
    isOpenMonth: showMonth,
    isOpenQuarter: showQuarter,
    isOpenYear: showYear,
    isOpenBimonthly: showBimonthly,
    isOpenHalfYear: showHalfYear,
    yearStartMonth: 1
  }),
      _useState2 = _slicedToArray(_useState, 2),
      cycleSetting = _useState2[0],
      setCycleSetting = _useState2[1];

  var _useState3 = (0, _react.useState)({
    annualNum: "".concat(new Date().getFullYear()),
    cycleType: '2',
    yqmNum: "".concat((0, _moment["default"])().quarter())
  }),
      _useState4 = _slicedToArray(_useState3, 2),
      selectedPeriod = _useState4[0],
      setSelectedPeriod = _useState4[1];

  var _useState5 = (0, _react.useState)(false),
      _useState6 = _slicedToArray(_useState5, 2),
      visible = _useState6[0],
      setVisible = _useState6[1];

  var fetchRef = (0, _react.useRef)(false);

  var onPopupVisibleChangeHandler = function onPopupVisibleChangeHandler(popupVisible) {
    setVisible(popupVisible);
    onPopupVisibleChange === null || onPopupVisibleChange === void 0 ? void 0 : onPopupVisibleChange(popupVisible);
  };

  (0, _react.useEffect)(function () {
    if (selected) {
      setSelectedPeriod(selected);
    } else if (lastSelected) {
      setSelectedPeriod(lastSelected);
    }
  }, []);
  (0, _react.useEffect)(function () {
    if (!visible && (!isFetch || fetchRef.current)) return;
    var url = (0, _helper.getApiUrl)('okr/cycleSetting');

    if (isFetch) {
      (0, _helper.taker)(url, 'GET').then(function (res) {
        if (res.Code == 1) {
          fetchRef.current = true;
          setCycleSetting(res.Data);

          if (selected) {
            setSelectedPeriod(selected);
          } else if (lastSelected) {} else {
            setSelectedPeriod((0, _common.getDefalutCycle)(res.Data));
            onChange && onChange((0, _common.getDefalutCycle)(res.Data));
          }
        }
      })["catch"](function (error) {
        return console.log(error);
      });
    } else if (selected) {
      setSelectedPeriod(selected);
    } else {
      setSelectedPeriod((0, _common.getDefalutCycle)(cycleSetting));
      onChange === null || onChange === void 0 ? void 0 : onChange((0, _common.getDefalutCycle)(cycleSetting));
    }
  }, [visible]);

  var popup = /*#__PURE__*/_react["default"].createElement(_selector["default"], {
    className: selectorClass,
    selected: selectedPeriod,
    showCustom: isShowCustom ? isFetch ? cycleSetting.isOpenCustom : showCustom : isShowCustom,
    showFullCycle: showFullCycle,
    showMonth: isFetch ? cycleSetting.isOpenMonth : showMonth,
    showQuarter: isFetch ? cycleSetting.isOpenQuarter : showQuarter,
    showYear: isFetch ? cycleSetting.isOpenYear : showYear,
    showHalfYear: isFetch ? cycleSetting.isOpenHalfYear : showHalfYear,
    showDoubleMonth: isFetch ? cycleSetting.isOpenBimonthly : showBimonthly,
    startMonth: isFetch ? cycleSetting.yearStartMonth : startMonth,
    onSelected: function onSelected(period) {
      setSelectedPeriod(period);
      onChange === null || onChange === void 0 ? void 0 : onChange(period);
      setVisible(false);
    },
    yearsRange: yearsRange,
    wrapperStyle: wrapperStyle
  });

  return /*#__PURE__*/_react["default"].createElement("div", {
    className: (0, _classnames["default"])('titaui-period-selector'),
    style: style
  }, /*#__PURE__*/_react["default"].createElement(_popup["default"], {
    getPopupContainer: getPopupContainer,
    popupVisible: visible,
    popupPlacement: align,
    extraClass: popClass,
    popup: popup,
    onPopupVisibleChange: onPopupVisibleChangeHandler
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: className
  }, children || /*#__PURE__*/_react["default"].createElement(_button["default"], {
    type: "default",
    className: buttonClass,
    style: noBorder ? {
      padding: '0',
      border: 'none'
    } : {
      padding: '6px 12px'
    }
  }, /*#__PURE__*/_react["default"].createElement("span", {
    className: "titaui-period-selector_value",
    style: {
      lineHeight: '18px',
      whiteSpace: 'nowrap'
    }
  }, (0, _common.formatPeriod)(selectedPeriod)), /*#__PURE__*/_react["default"].createElement("span", {
    className: "tu-icon-caret-down titaui-period-selector__down-icon"
  })))));
}

PeriodSelector.defaultProps = {
  align: 'bottomLeft',
  // selected: {
  //   annualNum: '2020',
  //   cycleType: '1',
  //   yqmNum: '0',
  // },
  isFetch: true,
  showCustom: true,
  showFullCycle: true,
  showMonth: true,
  showQuarter: true,
  showYear: true,
  yearsRange: [2020, new Date().getFullYear() + 1]
};
PeriodSelector.formatPeriod = _common.formatPeriod;
PeriodSelector.getDateRangeByCycle = _common.getDateRangeByCycle;
PeriodSelector.getDefalutCycle = _common.getDefalutCycle;
var _default = PeriodSelector;
exports["default"] = _default;