import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import React, { useContext, useState } from 'react';
import { createBEM } from '@zarm-design/bem';
import { ArrowLeft, ArrowRight } from '@zarm-design/icons';
import dayjs from 'dayjs';
import PickerView from '../picker-view';
import { ConfigContext } from '../config-provider';
import parseDataSource from './utils/parseDataSource';

function Header(props) {
  var _globalLocal$Calendar;

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

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

  var _useState = useState(false),
      _useState2 = _slicedToArray(_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 = dayjs().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 = parseDataSource(months, locale);
  var currentValue = [year, month];

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

  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: bem('header')
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('title', [{
      animate: showDatePicker
    }]),
    onClick: function onClick() {
      return setShowDatePicker(!showDatePicker);
    }
  }, title, /*#__PURE__*/React.createElement(ArrowRight, {
    theme: "primary",
    size: "sm"
  })), /*#__PURE__*/React.createElement("div", {
    className: bem('action')
  }, /*#__PURE__*/React.createElement(ArrowLeft, {
    theme: "primary",
    className: bem('action-btn', [{
      disabled: currentMonth === 0
    }]),
    onClick: function onClick() {
      if (currentMonth > 0) {
        changeMonth(currentMonth - 1);
      }
    }
  }), /*#__PURE__*/React.createElement(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.createElement(PickerView, {
    dataSource: Object.values(dataSource),
    value: currentValue,
    onChange: dateChange
  }) : null);
}

export default /*#__PURE__*/React.memo(Header);