"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

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

var _bem = require("@zarm-design/bem");

var _icons = require("@zarm-design/icons");

var _dayjs = _interopRequireDefault(require("dayjs"));

var _pickerView = _interopRequireDefault(require("../picker-view"));

var _configProvider = require("../config-provider");

var _parseDataSource = _interopRequireDefault(require("./utils/parseDataSource"));

function Header(props) {
  var _globalLocal$Calendar;

  var _useContext = (0, _react.useContext)(_configProvider.ConfigContext),
      prefixCls = _useContext.prefixCls,
      globalLocal = _useContext.locale;

  var locale = globalLocal === null || globalLocal === void 0 ? void 0 : globalLocal.Calendar;
  var bem = (0, _bem.createBEM)('calendar', {
    prefixCls: prefixCls
  });

  var _useState = (0, _react.useState)(false),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      showDatePicker = _useState2[0],
      setShowDatePicker = _useState2[1];

  var changeMonth = props.changeMonth,
      months = props.months,
      currentMonth = props.currentMonth;
  var current = months[currentMonth] || new Date();
  var year = current.getFullYear();
  var month = current.getMonth();
  var title = (0, _dayjs.default)().year(year).month(month).format(globalLocal === null || globalLocal === void 0 ? void 0 : (_globalLocal$Calendar = globalLocal.Calendar) === null || _globalLocal$Calendar === void 0 ? void 0 : _globalLocal$Calendar.yearMonthFormat);
  var dataSource = (0, _parseDataSource.default)(months, locale);
  var currentValue = [year, month];

  var dateChange = function dateChange(value) {
    var day = (0, _dayjs.default)().year(value[0]).month(value[1]);
    var index = months.findIndex(function (i) {
      return (0, _dayjs.default)(i).isSame(day, 'month');
    });
    changeMonth(index);
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('header')
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: bem('title', [{
      animate: showDatePicker
    }]),
    onClick: function onClick() {
      return setShowDatePicker(!showDatePicker);
    }
  }, title, /*#__PURE__*/_react.default.createElement(_icons.ArrowRight, {
    theme: "primary",
    size: "sm"
  })), /*#__PURE__*/_react.default.createElement("div", {
    className: bem('action')
  }, /*#__PURE__*/_react.default.createElement(_icons.ArrowLeft, {
    theme: "primary",
    className: bem('action-btn', [{
      disabled: currentMonth === 0
    }]),
    onClick: function onClick() {
      if (currentMonth > 0) {
        changeMonth(currentMonth - 1);
      }
    }
  }), /*#__PURE__*/_react.default.createElement(_icons.ArrowRight, {
    theme: "primary",
    className: bem('action-btn', [{
      disabled: currentMonth >= months.length - 1
    }]),
    onClick: function onClick() {
      if (currentMonth < months.length - 1) {
        changeMonth(currentMonth + 1);
      }
    }
  }))), showDatePicker ? /*#__PURE__*/_react.default.createElement(_pickerView.default, {
    dataSource: Object.values(dataSource),
    value: currentValue,
    onChange: dateChange
  }) : null);
}

var _default = /*#__PURE__*/_react.default.memo(Header);

exports.default = _default;