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

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

var TDateHeader = 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 = useConfig("classPrefix"),
      classPrefix = _useConfig.classPrefix;
    var COMPONENT_NAME = usePrefixClass("date-picker__header");
    var _useConfig2 = useConfig("datePicker"),
      globalConfig = _useConfig2.globalConfig;
    var yearOptions = ref(initOptions(props.year));
    var showMonthPicker = props.mode === "date" || props.mode === "week";
    var nearestYear = 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 = 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(options), _toConsumableArray(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(yearOptions.value), _toConsumableArray(options));
    }
    return function () {
      return createVNode("div", {
        "class": COMPONENT_NAME.value
      }, [createVNode("div", {
        "class": "".concat(COMPONENT_NAME.value, "-controller")
      }, [showMonthPicker && createVNode(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), createVNode(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 createVNode("div", {
            "class": "".concat(classPrefix.value, "-select-option"),
            "onClick": handlePanelTopClick
          }, [createTextVNode("...")]);
        },
        "panelBottomContent": function panelBottomContent() {
          return createVNode("div", {
            "class": "".concat(classPrefix.value, "-select-option"),
            "onClick": handlePanelBottomClick
          }, [createTextVNode("...")]);
        }
      }, null)]), createVNode(PaginationMini, {
        "tips": labelMap[props.mode],
        "size": "small",
        "onChange": props.onJumperClick
      }, null)]);
    };
  }
});

export { TDateHeader as default };
//# sourceMappingURL=Header.js.map
