import * as _vue from "vue";

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) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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 { inject } from 'vue';
import Select from '../select';
import { Group, Button } from '../radio';
import PropTypes from '../_util/vue-types';
import { initDefaultProps } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider';
var Option = Select.Option;

function getMonthsLocale(value) {
  var current = value.clone();
  var localeData = value.localeData();
  var months = [];

  for (var i = 0; i < 12; i++) {
    current.month(i);
    months.push(localeData.monthsShort(current));
  }

  return months;
}

export var HeaderProps = {
  prefixCls: PropTypes.string,
  locale: PropTypes.any,
  fullscreen: PropTypes.boolean,
  yearSelectOffset: PropTypes.number,
  yearSelectTotal: PropTypes.number,
  type: PropTypes.string,
  value: PropTypes.any,
  validRange: PropTypes.array,
  headerRender: PropTypes.func,
  onValueChange: PropTypes.func,
  onTypeChange: PropTypes.func
};
export default {
  name: 'CalendarHeader',
  inheritAttrs: false,
  props: initDefaultProps(HeaderProps, {
    yearSelectOffset: 10,
    yearSelectTotal: 20
  }),
  setup: function setup() {
    return {
      configProvider: inject('configProvider', ConfigConsumerProps)
    };
  },
  // private calendarHeaderNode: HTMLDivElement;
  methods: {
    getYearSelectElement: function getYearSelectElement(prefixCls, year) {
      var _this = this;

      var yearSelectOffset = this.yearSelectOffset,
          yearSelectTotal = this.yearSelectTotal,
          _this$locale = this.locale,
          locale = _this$locale === void 0 ? {} : _this$locale,
          fullscreen = this.fullscreen,
          validRange = this.validRange;
      var start = year - yearSelectOffset;
      var end = start + yearSelectTotal;

      if (validRange) {
        start = validRange[0].get('year');
        end = validRange[1].get('year') + 1;
      }

      var suffix = locale.year === '年' ? '年' : '';
      var options = [];

      var _loop = function _loop(index) {
        options.push(_vue.createVNode(Option, {
          "key": "".concat(index)
        }, {
          default: function _default() {
            return [function () {
              return index + suffix;
            }()];
          }
        }));
      };

      for (var index = start; index < end; index++) {
        _loop(index);
      }

      return _vue.createVNode(Select, {
        "size": fullscreen ? 'default' : 'small',
        "dropdownMatchSelectWidth": false,
        "class": "".concat(prefixCls, "-year-select"),
        "onChange": this.onYearChange,
        "value": String(year),
        "getPopupContainer": function getPopupContainer() {
          return _this.calendarHeaderNode;
        }
      }, {
        default: function _default() {
          return [options];
        }
      });
    },
    getMonthSelectElement: function getMonthSelectElement(prefixCls, month, months) {
      var _this2 = this;

      var fullscreen = this.fullscreen,
          validRange = this.validRange,
          value = this.value;
      var options = [];
      var start = 0;
      var end = 12;

      if (validRange) {
        var _validRange = _slicedToArray(validRange, 2),
            rangeStart = _validRange[0],
            rangeEnd = _validRange[1];

        var currentYear = value.get('year');

        if (rangeEnd.get('year') === currentYear) {
          end = rangeEnd.get('month') + 1;
        }

        if (rangeStart.get('year') === currentYear) {
          start = rangeStart.get('month');
        }
      }

      var _loop2 = function _loop2(index) {
        options.push(_vue.createVNode(Option, {
          "key": "".concat(index)
        }, {
          default: function _default() {
            return [function () {
              return months[index];
            }()];
          }
        }));
      };

      for (var index = start; index < end; index++) {
        _loop2(index);
      }

      return _vue.createVNode(Select, {
        "size": fullscreen ? 'default' : 'small',
        "dropdownMatchSelectWidth": false,
        "class": "".concat(prefixCls, "-month-select"),
        "value": String(month),
        "onChange": this.onMonthChange,
        "getPopupContainer": function getPopupContainer() {
          return _this2.calendarHeaderNode;
        }
      }, {
        default: function _default() {
          return [options];
        }
      });
    },
    onYearChange: function onYearChange(year) {
      var value = this.value,
          validRange = this.validRange;
      var newValue = value.clone();
      newValue.year(parseInt(year, 10)); // switch the month so that it remains within range when year changes

      if (validRange) {
        var _validRange2 = _slicedToArray(validRange, 2),
            start = _validRange2[0],
            end = _validRange2[1];

        var newYear = newValue.get('year');
        var newMonth = newValue.get('month');

        if (newYear === end.get('year') && newMonth > end.get('month')) {
          newValue.month(end.get('month'));
        }

        if (newYear === start.get('year') && newMonth < start.get('month')) {
          newValue.month(start.get('month'));
        }
      }

      this.$emit('valueChange', newValue);
    },
    onMonthChange: function onMonthChange(month) {
      var newValue = this.value.clone();
      newValue.month(parseInt(month, 10));
      this.$emit('valueChange', newValue);
    },
    onInternalTypeChange: function onInternalTypeChange(e) {
      this.triggerTypeChange(e.target.value);
    },
    triggerTypeChange: function triggerTypeChange(val) {
      this.$emit('typeChange', val);
    },
    getMonthYearSelections: function getMonthYearSelections(getPrefixCls) {
      var _this$$props = this.$props,
          customizePrefixCls = _this$$props.prefixCls,
          type = _this$$props.type,
          value = _this$$props.value;
      var prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);
      var yearReactNode = this.getYearSelectElement(prefixCls, value.year());
      var monthReactNode = type === 'month' ? this.getMonthSelectElement(prefixCls, value.month(), getMonthsLocale(value)) : null;
      return {
        yearReactNode: yearReactNode,
        monthReactNode: monthReactNode
      };
    },
    getTypeSwitch: function getTypeSwitch() {
      var _this$$props2 = this.$props,
          _this$$props2$locale = _this$$props2.locale,
          locale = _this$$props2$locale === void 0 ? {} : _this$$props2$locale,
          type = _this$$props2.type,
          fullscreen = _this$$props2.fullscreen;
      var size = fullscreen ? 'default' : 'small';
      return _vue.createVNode(Group, {
        "onChange": this.onInternalTypeChange,
        "value": type,
        "size": size
      }, {
        default: function _default() {
          return [_vue.createVNode(Button, {
            "value": "month"
          }, {
            default: function _default() {
              return [locale.month];
            }
          }), _vue.createVNode(Button, {
            "value": "year"
          }, {
            default: function _default() {
              return [locale.year];
            }
          })];
        }
      });
    },
    triggerValueChange: function triggerValueChange() {
      this.$emit.apply(this, ['valueChange'].concat(Array.prototype.slice.call(arguments)));
    },
    saveCalendarHeaderNode: function saveCalendarHeaderNode(node) {
      this.calendarHeaderNode = node;
    },
    headerRenderCustom: function headerRenderCustom(headerRender) {
      var _this$$props3 = this.$props,
          type = _this$$props3.type,
          value = _this$$props3.value;
      return headerRender({
        value: value,
        type: type || 'month',
        onChange: this.triggerValueChange,
        onTypeChange: this.triggerTypeChange
      });
    }
  },
  render: function render() {
    var customizePrefixCls = this.prefixCls,
        headerRender = this.headerRender;
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('fullcalendar', customizePrefixCls);
    var typeSwitch = this.getTypeSwitch();

    var _this$getMonthYearSel = this.getMonthYearSelections(getPrefixCls),
        yearReactNode = _this$getMonthYearSel.yearReactNode,
        monthReactNode = _this$getMonthYearSel.monthReactNode;

    return headerRender ? this.headerRenderCustom(headerRender) : _vue.createVNode("div", {
      "class": "".concat(prefixCls, "-header"),
      "ref": this.saveCalendarHeaderNode
    }, [yearReactNode, monthReactNode, typeSwitch]);
  }
};