/**
 * ldesign v0.4.2
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var vue = require('vue');
var pagination_index = require('../../pagination/index.js');
var select_index = require('../../select/index.js');
var hooks_useConfig = require('../../hooks/useConfig.js');
var configProvider_useConfig = require('../../config-provider/useConfig.js');
require('../../utils/withInstall.js');
require('../../pagination/pagination.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/isNaN');
require('@ldesign/icons-vue-next');
require('lodash/isObject');
require('../../hooks/useGlobalIcon.js');
require('lodash/cloneDeep');
require('lodash/isFunction');
require('lodash/isString');
require('../../config-provider/context.js');
require('lodash/mergeWith');
require('lodash/merge');
require('lodash/isArray');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('@babel/runtime/helpers/typeof');
require('../../_chunks/dep-7f0559c3.js');
require('dayjs');
require('../../input-number/index.js');
require('../../input-number/input-number.js');
require('@babel/runtime/helpers/defineProperty');
require('../../button/index.js');
require('../../button/button.js');
require('../../loading/index.js');
require('../../loading/directive.js');
require('../../loading/plugin.js');
require('../../utils/dom.js');
require('../../utils/easing.js');
require('../../loading/loading.js');
require('../../utils/render-tnode.js');
require('lodash/isEmpty');
require('lodash/camelCase');
require('lodash/kebabCase');
require('../../hooks/useTeleport.js');
require('../../loading/props.js');
require('../../loading/icon/gradient.js');
require('../../_common/js/loading/circle-adapter.js');
require('../../_common/js/utils/set-style.js');
require('../../_common/js/utils/helper.js');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('../../hooks/useRipple.js');
require('../../utils/set-style.js');
require('../../hooks/useKeepAnimation.js');
require('../../form/hooks.js');
require('../../hooks/tnode.js');
require('../../button/props.js');
require('../../input/index.js');
require('../../input/input.js');
require('../../input/props.js');
require('../../input/useInput.js');
require('../../form/const.js');
require('../../hooks/useVModel.js');
require('../../input/useLengthLimit.js');
require('../../_common/js/log/log.js');
require('../../input/useInputEventHandler.js');
require('../../input/useInputWidth.js');
require('../../input/input-group.js');
require('../../input/input-group-props.js');
require('../../input-number/props.js');
require('../../input-number/useInputNumber.js');
require('../../hooks/useCommonClassName.js');
require('../../_common/js/input-number/number.js');
require('lodash/isNil');
require('../../_common/js/input-number/large-number.js');
require('../../input-adornment/index.js');
require('../../input-adornment/input-adornment.js');
require('../../input-adornment/props.js');
require('../../hooks/useDefaultValue.js');
require('../../pagination/props.js');
require('../../pagination/usePaginationClasses.js');
require('../../pagination/useMoreAction.js');
require('../../select/select.js');
require('lodash/pick');
require('lodash/debounce');
require('lodash/get');
require('lodash/intersection');
require('../../common-components/fake-arrow.js');
require('../../select-input/index.js');
require('../../select-input/select-input.js');
require('../../popup/index.js');
require('../../popup/popup.js');
require('@popperjs/core');
require('../../popup/container.js');
require('../../hooks/useResizeObserver.js');
require('../../popup/props.js');
require('../../select-input/props.js');
require('../../select-input/useSingle.js');
require('../../select-input/useMultiple.js');
require('../../tag-input/index.js');
require('../../tag-input/tag-input.js');
require('../../tag-input/props.js');
require('../../tag-input/hooks/useTagScroll.js');
require('../../tag-input/useTagList.js');
require('../../tag/index.js');
require('../../tag/tag.js');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('../../tag/check-tag-props.js');
require('../../tag-input/hooks/useHover.js');
require('../../tag-input/hooks/useDragSorter.js');
require('../../select-input/useOverlayInnerStyle.js');
require('../../select/props.js');
require('../../select/select-panel.js');
require('lodash/omit');
require('../../select/option.js');
require('../../checkbox/index.js');
require('../../checkbox/checkbox.js');
require('../../checkbox/props.js');
require('../../checkbox/constants.js');
require('../../checkbox/group.js');
require('../../hooks/slot.js');
require('../../checkbox/checkbox-group-props.js');
require('../../select/option-props.js');
require('../../select/helper.js');
require('../../select/optionGroup.js');
require('../../select/option-group-props.js');
require('../../select/hooks/usePanelVirtualScroll.js');
require('../../hooks/useVirtualScroll.js');
require('../../select/hooks/useSelectOptions.js');
require('../../pagination/pagination-mini.js');
require('../../pagination/pagination-mini-props.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);

var TDateHeader = vue.defineComponent({
  name: "TDatePickerHeader",
  props: {
    mode: {
      type: String,
      "default": "date"
    },
    year: Number,
    month: Number,
    onMonthChange: Function,
    onYearChange: Function,
    onJumperClick: Function
  },
  setup: function setup(props) {
    var _useConfig = configProvider_useConfig.useConfig("classPrefix"),
      classPrefix = _useConfig.classPrefix;
    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("date-picker__header");
    var _useConfig2 = configProvider_useConfig.useConfig("datePicker"),
      globalConfig = _useConfig2.globalConfig;
    var yearOptions = vue.ref(initOptions(props.year));
    var showMonthPicker = props.mode === "date" || props.mode === "week";
    var nearestYear = vue.computed(function () {
      var _yearOptions$value$fi;
      return ((_yearOptions$value$fi = yearOptions.value.find(function (option) {
        return option.value - props.year <= 9 && option.value - props.year >= 0;
      })) === null || _yearOptions$value$fi === void 0 ? void 0 : _yearOptions$value$fi.value) || props.year;
    });
    var monthOptions = vue.computed(function () {
      return globalConfig.value.months.map(function (item, index) {
        return {
          label: item,
          value: index
        };
      });
    });
    function initOptions(year) {
      var options = [];
      if (props.mode === "year") {
        var extraYear = year % 10;
        var minYear = year - extraYear - 100;
        var maxYear = year - extraYear + 100;
        for (var i = minYear; i <= maxYear; i += 10) options.push({
          label: "".concat(i, " - ").concat(i + 9),
          value: i + 9
        });
      } else {
        options.push({
          label: "".concat(year),
          value: year
        });
        for (var _i = 1; _i <= 10; _i++) {
          options.push({
            label: "".concat(year + _i),
            value: year + _i
          });
          options.unshift({
            label: "".concat(year - _i),
            value: year - _i
          });
        }
      }
      return options;
    }
    function loadMoreYear(year, type) {
      var options = [];
      if (props.mode === "year") {
        var extraYear = year % 10;
        if (type === "add") {
          for (var i = year - extraYear + 10; i <= year - extraYear + 50; i += 10) options.push({
            label: "".concat(i, " - ").concat(i + 9),
            value: i
          });
        } else {
          for (var _i2 = year - extraYear - 1; _i2 > year - extraYear - 50; _i2 -= 10) options.unshift({
            label: "".concat(_i2 - 9, " - ").concat(_i2),
            value: _i2
          });
        }
      } else if (type === "add") {
        for (var _i3 = year + 1; _i3 <= year + 10; _i3++) options.push({
          label: "".concat(_i3),
          value: _i3
        });
      } else {
        for (var _i4 = year - 1; _i4 > year - 10; _i4--) options.unshift({
          label: "".concat(_i4),
          value: _i4
        });
      }
      return options;
    }
    var labelMap = {
      year: {
        prev: globalConfig.value.preDecade,
        current: globalConfig.value.now,
        next: globalConfig.value.nextDecade
      },
      month: {
        prev: globalConfig.value.preYear,
        current: globalConfig.value.now,
        next: globalConfig.value.nextYear
      },
      date: {
        prev: globalConfig.value.preMonth,
        current: globalConfig.value.now,
        next: globalConfig.value.nextMonth
      }
    };
    function handleScroll(_ref) {
      var e = _ref.e;
      if (e.target.scrollTop === 0) handlePanelTopClick(e);else if (e.target.scrollTop === e.target.scrollHeight - e.target.clientHeight) handlePanelBottomClick(e);
    }
    function handlePanelTopClick(e) {
      var _e$stopPropagation;
      e === null || e === void 0 ? void 0 : (_e$stopPropagation = e.stopPropagation) === null || _e$stopPropagation === void 0 ? void 0 : _e$stopPropagation.call(e);
      var firstYear = yearOptions.value[0].value;
      var options = loadMoreYear(firstYear, "reduce");
      yearOptions.value = [].concat(_toConsumableArray__default["default"](options), _toConsumableArray__default["default"](yearOptions.value));
    }
    function handlePanelBottomClick(e) {
      var _e$stopPropagation2;
      e === null || e === void 0 ? void 0 : (_e$stopPropagation2 = e.stopPropagation) === null || _e$stopPropagation2 === void 0 ? void 0 : _e$stopPropagation2.call(e);
      var lastYear = yearOptions.value.slice(-1)[0].value;
      var options = loadMoreYear(lastYear, "add");
      yearOptions.value = [].concat(_toConsumableArray__default["default"](yearOptions.value), _toConsumableArray__default["default"](options));
    }
    return function () {
      return vue.createVNode("div", {
        "class": COMPONENT_NAME.value
      }, [vue.createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "-controller")
      }, [showMonthPicker && vue.createVNode(select_index.Select, {
        "class": "".concat(COMPONENT_NAME.value, "-controller-month"),
        "value": props.month,
        "options": monthOptions.value,
        "onChange": function onChange(val) {
          var _props$onMonthChange;
          return (_props$onMonthChange = props.onMonthChange) === null || _props$onMonthChange === void 0 ? void 0 : _props$onMonthChange.call(props, val);
        },
        "popupProps": {
          attach: function attach(triggerElement) {
            return triggerElement.parentNode;
          },
          overlayClassName: "".concat(COMPONENT_NAME.value, "-controller-month-popup")
        }
      }, null), vue.createVNode(select_index.Select, {
        "class": "".concat(COMPONENT_NAME.value, "-controller-year"),
        "value": props.mode === "year" ? nearestYear.value : props.year,
        "options": yearOptions.value,
        "onChange": function onChange(val) {
          var _props$onYearChange;
          return (_props$onYearChange = props.onYearChange) === null || _props$onYearChange === void 0 ? void 0 : _props$onYearChange.call(props, val);
        },
        "popupProps": {
          onScroll: handleScroll,
          attach: function attach(triggerElement) {
            return triggerElement.parentNode;
          },
          overlayClassName: "".concat(COMPONENT_NAME.value, "-controller-year-popup")
        },
        "panelTopContent": function panelTopContent() {
          return vue.createVNode("div", {
            "class": "".concat(classPrefix.value, "-select-option"),
            "onClick": handlePanelTopClick
          }, [vue.createTextVNode("...")]);
        },
        "panelBottomContent": function panelBottomContent() {
          return vue.createVNode("div", {
            "class": "".concat(classPrefix.value, "-select-option"),
            "onClick": handlePanelBottomClick
          }, [vue.createTextVNode("...")]);
        }
      }, null)]), vue.createVNode(pagination_index.PaginationMini, {
        "tips": labelMap[props.mode],
        "size": "small",
        "onChange": props.onJumperClick
      }, null)]);
    };
  }
});

exports["default"] = TDateHeader;
//# sourceMappingURL=Header.js.map
