function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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

import React, { createElement, Fragment, useState } from "react";
import dayjs from 'dayjs';
import classNames from 'classnames';
import { Text } from "@alifd/meet-react-component-one";
import Button from '../button';
import { EmbedMonthPicker, EmbedYearPicker } from '../date-picker';
import Icon from '../icon';
import View from '../view';
import { HeaderType } from './types';
import { formatNumber, updateMonth, updateWeek, updateYear } from './utils';

var formatTitle = function formatTitle(date, mode) {
  if (!date) {
    return '';
  }

  var year = date.getFullYear();
  var month = date.getMonth() + 1;

  if (mode === 'month' || mode === 'week') {
    return "".concat(formatNumber(year), ".").concat(formatNumber(month));
  }

  return formatNumber(year);
};

var CalendarHeader = function CalendarHeader(props) {
  var _classNames;

  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      date = props.date,
      mode = props.mode,
      type = props.type,
      maxDate = props.maxDate,
      minDate = props.minDate,
      _props$enableYearPick = props.enableYearPicker,
      enableYearPickerProp = _props$enableYearPick === void 0 ? false : _props$enableYearPick,
      enableTimePicker = props.enableTimePicker,
      renderHeadLeft = props.renderHeadLeft,
      renderHeadRight = props.renderHeadRight,
      locale = props.locale,
      _props$onDateChange = props.onDateChange,
      onDateChange = _props$onDateChange === void 0 ? function () {} : _props$onDateChange,
      _props$onModeChange = props.onModeChange,
      onModeChange = _props$onModeChange === void 0 ? function () {} : _props$onModeChange,
      _props$onPickerShow = props.onPickerShow,
      onPickerShow = _props$onPickerShow === void 0 ? function () {} : _props$onPickerShow;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      showPicker = _useState2[0],
      setShowPicker = _useState2[1];

  var enableYearPicker = type === HeaderType.select || enableYearPickerProp;
  var datePickerProps = {
    value: date,
    onChange: function onChange(v) {
      var d = dayjs(v);
      var y = d.get('year');
      var m = d.get('month');
      var newDate = dayjs().year(y).month(m).date(1);
      onDateChange(newDate.toDate(), false, 'picker');
    }
  };
  var clsPrefix = "".concat(prefix, "calendar");
  var clses = {
    picker: classNames((_classNames = {}, _defineProperty(_classNames, "".concat(clsPrefix, "-header-picker"), !enableTimePicker), _defineProperty(_classNames, "".concat(clsPrefix, "-header-picker--with-time"), enableTimePicker), _classNames))
  };

  var togglePicker = function togglePicker() {
    if (!enableYearPicker) {
      return;
    }

    var nextState = !showPicker;
    setShowPicker(nextState);

    if (onPickerShow) {
      onPickerShow(nextState);
    }
  };

  var renderSelectedHeader = function renderSelectedHeader() {
    var headPrefix = "".concat(prefix, "calendar-header");
    return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(View, {
      className: classNames("".concat(headPrefix), "".concat(headPrefix, "-select"))
    }, /*#__PURE__*/React.createElement(View, {
      className: "".concat(headPrefix, "-select-title-container ").concat(headPrefix, "-select-title-container-").concat(mode),
      onClick: togglePicker
    }, /*#__PURE__*/React.createElement(Text, {
      className: classNames("".concat(headPrefix, "-select-title"), _defineProperty({}, "".concat(headPrefix, "-select-title--highlight"), showPicker))
    }, dayjs(date).format(mode === 'year' ? 'YYYY' : 'YYYY.MM')), /*#__PURE__*/React.createElement(Icon, {
      name: "arrow-down",
      size: "xs",
      className: showPicker ? "".concat(prefix, "calendar-header-arrow-up") : "".concat(prefix, "calendar-header-arrow")
    })), /*#__PURE__*/React.createElement(View, {
      className: "".concat(headPrefix, "-select-button-container")
    }, /*#__PURE__*/React.createElement(Button.Group, {
      size: "small"
    }, /*#__PURE__*/React.createElement(Button, {
      disabled: enableYearPicker && showPicker,
      type: mode === 'month' ? 'primary' : 'normal',
      onClick: function onClick() {
        if (onModeChange) {
          onModeChange('month');
        }
      }
    }, locale && locale.month || '月'), /*#__PURE__*/React.createElement(Button, {
      disabled: enableYearPicker && showPicker,
      type: mode === 'year' ? 'primary' : 'normal',
      onClick: function onClick() {
        if (onModeChange) {
          onModeChange('year');
        }
      }
    }, locale && locale.year || '年')))), showPicker ? /*#__PURE__*/React.createElement(EmbedMonthPicker, _extends({
      className: clses.picker,
      validRange: [minDate, maxDate]
    }, datePickerProps)) : null);
  };

  var renderNormalHeader = function renderNormalHeader() {
    var _classNames6, _classNames8;

    var headPrefix = "".concat(prefix, "calendar-header");
    var leftYearIcon = null;
    var rightYearIcon = null;

    if (!enableYearPickerProp) {
      leftYearIcon = /*#__PURE__*/React.createElement(Icon, {
        className: classNames("".concat(headPrefix, "-normal-icon double"), _defineProperty({}, "".concat(headPrefix, "-normal-icon-disabled"), showPicker)),
        name: "arrow-double-left",
        onClick: function onClick() {
          if (showPicker) {
            return;
          }

          if (onDateChange) {
            if (mode !== 'week') {
              onDateChange(updateYear(date, false, maxDate, minDate), false, 'year');
            } else {
              onDateChange(updateMonth(date, false, maxDate, minDate), false, 'month');
            }
          }
        }
      });
      rightYearIcon = /*#__PURE__*/React.createElement(Icon, {
        className: classNames("".concat(headPrefix, "-normal-icon double"), _defineProperty({}, "".concat(headPrefix, "-normal-icon-disabled"), showPicker)),
        name: "arrow-double-right",
        onClick: function onClick() {
          if (showPicker) {
            return;
          }

          if (onDateChange) {
            if (mode !== 'week') {
              onDateChange(updateYear(date, true, maxDate, minDate), true, 'year');
            } else {
              onDateChange(updateMonth(date, true, maxDate, minDate), true, 'month');
            }
          }
        }
      });
    }

    if (renderHeadLeft) {
      leftYearIcon = renderHeadLeft();
    }

    if (renderHeadRight) {
      rightYearIcon = renderHeadRight();
    }

    return /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(View, {
      className: classNames("".concat(headPrefix), "".concat(headPrefix, "-normal"), _defineProperty({}, "".concat(headPrefix, "-normal--highlighted"), showPicker))
    }, /*#__PURE__*/React.createElement(View, {
      className: "".concat(headPrefix, "-normal-left-container")
    }, leftYearIcon, /*#__PURE__*/React.createElement(Icon, {
      className: classNames("".concat(headPrefix, "-normal-icon"), (_classNames6 = {}, _defineProperty(_classNames6, "".concat(headPrefix, "-normal-icon-disabled"), showPicker), _defineProperty(_classNames6, "".concat(headPrefix, "-normal-icon--only"), !leftYearIcon), _classNames6)),
      name: "arrow-left",
      onClick: function onClick() {
        if (showPicker) {
          return;
        }

        if (onDateChange) {
          if (mode !== 'week') {
            onDateChange(updateMonth(date, false, maxDate, minDate), false, 'month');
          } else {
            onDateChange(updateWeek(date, false, maxDate, minDate), false, 'week');
          }
        }
      }
    })), /*#__PURE__*/React.createElement(View, {
      className: "".concat(headPrefix, "-normal-title-text-wrapper"),
      onClick: togglePicker
    }, /*#__PURE__*/React.createElement(Text, {
      className: classNames("".concat(headPrefix, "-normal-title-text"), _defineProperty({}, "".concat(headPrefix, "-normal-title-text--highlighted"), showPicker)),
      onClick: function onClick() {}
    }, showPicker ? formatTitle(date, mode) : formatTitle(date, mode)), enableYearPicker ? /*#__PURE__*/React.createElement(Icon, {
      name: "arrow-down",
      size: "xs",
      className: showPicker ? "".concat(prefix, "calendar-header-arrow-up") : "".concat(prefix, "calendar-header-arrow")
    }) : null), /*#__PURE__*/React.createElement(View, {
      className: "".concat(headPrefix, "-normal-right-container")
    }, /*#__PURE__*/React.createElement(Icon, {
      className: classNames("".concat(headPrefix, "-normal-icon"), (_classNames8 = {}, _defineProperty(_classNames8, "".concat(headPrefix, "-normal-icon-disabled"), showPicker), _defineProperty(_classNames8, "".concat(headPrefix, "-normal-icon--only"), !rightYearIcon), _classNames8)),
      name: "arrow-right",
      onClick: function onClick() {
        if (showPicker) {
          return;
        }

        if (onDateChange) {
          if (mode !== 'week') {
            onDateChange(updateMonth(date, true, maxDate, minDate), true, 'month');
          } else {
            onDateChange(updateWeek(date, true, maxDate, minDate), true, 'week');
          }
        }
      }
    }), rightYearIcon)), showPicker ? /*#__PURE__*/React.createElement(Fragment, null, mode === 'year' ? /*#__PURE__*/React.createElement(EmbedYearPicker, _extends({
      className: clses.picker,
      validRange: [minDate, maxDate]
    }, datePickerProps)) : /*#__PURE__*/React.createElement(EmbedMonthPicker, _extends({
      className: clses.picker,
      validRange: [minDate, maxDate]
    }, datePickerProps))) : null);
  };

  return type === HeaderType.select ? renderSelectedHeader() : renderNormalHeader();
};

export default CalendarHeader;