/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import { defineComponent, ref, computed } from '@vue/composition-api';
import _Jumper from '../../jumper/jumper.js';
import TSelect from '../../select/select.js';
import { usePrefixClass } from '../../../hooks/use-config.js';
import { useConfig } from '../../config-provider/useConfig.js';
import 'lodash/isObject';
import '@ldesign/icons-vue';
import '../../jumper/props.js';
import '../../../hooks/use-global-icon.js';
import '../../config-provider/context.js';
import 'lodash';
import '../../../chunks/zh_CN.js';
import '../../config-provider/type.js';
import '../../button/index.js';
import '../../button/button.js';
import '../../loading/index.js';
import '../../loading/loading.js';
import '../../loading/icon/gradient.js';
import 'vue';
import '../../../chunks/defineProperty-28395935.js';
import '../../../chunks/set-style.js';
import '../../../chunks/helper2.js';
import '@babel/runtime/helpers/slicedToArray';
import '../../../config.js';
import '../../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../../utils/easing.js';
import '../../../utils/render-node.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../../utils/mixins.js';
import '../../../utils/with-install.js';
import '../../loading/style/css.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../button/props.js';
import '../../../utils/ripple.js';
import '../../button/style/css.js';
import '../../button/type.js';
import '../../../chunks/helper.js';
import '../../../chunks/_commonjsHelpers.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import 'lodash/isFunction';
import 'lodash/debounce';
import 'lodash/get';
import 'lodash/cloneDeep';
import 'lodash/isArray';
import '../../../hooks/use-default-value.js';
import '../../../hooks/use-vmodel.js';
import '../../../hooks/lnode.js';
import 'lodash/isEmpty';
import '../../../hooks/render-lnode.js';
import '../../select/props.js';
import '../../popup/index.js';
import '../../popup/popup.js';
import '@popperjs/core';
import '../../../utils/helper.js';
import '../../popup/props.js';
import '../../popup/container.js';
import '../../../utils/map-props.js';
import '../../popup/style/css.js';
import '../../popup/type.js';
import '../../input/index.js';
import '../../input/input.js';
import '../../../utils/event.js';
import '../../input/props.js';
import '../../input/group.js';
import '../../input/style/css.js';
import '../../input/type.js';
import '../../tag/index.js';
import '../../tag/tag.js';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import '../../tag/style/css.js';
import '../../tag/type.js';
import '../../select-input/index.js';
import '../../select-input/select-input.js';
import '../../select-input/props.js';
import '../../select-input/hooks/use-single.js';
import 'lodash/pick';
import '../../select-input/hooks/use-multiple.js';
import '../../select-input/hooks/use-overlay-innerstyle.js';
import '../../select-input/style/css.js';
import '../../select-input/type.js';
import '../../common/fake-arrow.js';
import '../../select/option.js';
import '../../checkbox/index.js';
import '../../checkbox/group.js';
import 'lodash/intersection';
import '../../checkbox/checkbox.js';
import '../../checkbox/props.js';
import '../../checkbox/style/css.js';
import '../../checkbox/type.js';
import '../../../hooks/use-lazy-load.js';
import '../../../chunks/observe.js';
import '../../select/utils.js';
import '../../../hooks/use-common-classname.js';
import '../../select/select-panel.js';
import '../../../hooks/use-virtual-scroll.js';
import '../../select/hooks/use-select-options.js';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var TDateHeader = defineComponent({
  name: "LDatePickerHeader",
  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"),
        global = _useConfig2.global;

    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 global.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 + extraYear
          });
        }
      } 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: global.value.preDecade,
        current: global.value.now,
        next: global.value.nextDecade
      },
      month: {
        prev: global.value.preYear,
        current: global.value.now,
        next: global.value.nextYear
      },
      date: {
        prev: global.value.preMonth,
        current: global.value.now,
        next: global.value.nextMonth
      }
    };

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

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

    return {
      COMPONENT_NAME: COMPONENT_NAME,
      labelMap: labelMap,
      nearestYear: nearestYear,
      classPrefix: classPrefix,
      monthOptions: monthOptions,
      yearOptions: yearOptions,
      showMonthPicker: showMonthPicker,
      handleScroll: handleScroll,
      handlePanelTopClick: handlePanelTopClick,
      handlePanelBottomClick: handlePanelBottomClick
    };
  },
  render: function render() {
    var _this = this;

    var h = arguments[0];
    var COMPONENT_NAME = this.COMPONENT_NAME,
        labelMap = this.labelMap,
        nearestYear = this.nearestYear,
        classPrefix = this.classPrefix,
        monthOptions = this.monthOptions,
        yearOptions = this.yearOptions,
        showMonthPicker = this.showMonthPicker,
        handlePanelTopClick = this.handlePanelTopClick,
        handlePanelBottomClick = this.handlePanelBottomClick;
    return h("div", {
      "class": COMPONENT_NAME
    }, [h("div", {
      "class": "".concat(COMPONENT_NAME, "-controller")
    }, [showMonthPicker && h(TSelect, {
      "class": "".concat(COMPONENT_NAME, "-controller-month"),
      "props": _objectSpread({}, {
        value: this.month,
        options: monthOptions,
        onChange: function onChange(val) {
          var _this$onMonthChange;

          return (_this$onMonthChange = _this.onMonthChange) === null || _this$onMonthChange === void 0 ? void 0 : _this$onMonthChange.call(_this, val);
        },
        popupProps: {
          attach: function attach(triggerElement) {
            return triggerElement.parentNode;
          },
          overlayClassName: "".concat(COMPONENT_NAME, "-controller-month-popup")
        }
      })
    }), h(TSelect, {
      "class": "".concat(COMPONENT_NAME, "-controller-year"),
      "props": _objectSpread({}, {
        value: this.mode === "year" ? nearestYear : this.year,
        options: yearOptions,
        onChange: function onChange(val) {
          var _this$onYearChange;

          return (_this$onYearChange = _this.onYearChange) === null || _this$onYearChange === void 0 ? void 0 : _this$onYearChange.call(_this, val);
        },
        popupProps: {
          onScroll: this.handleScroll,
          attach: function attach(triggerElement) {
            return triggerElement.parentNode;
          },
          overlayClassName: "".concat(COMPONENT_NAME, "-controller-year-popup")
        },
        panelTopContent: function panelTopContent() {
          return h("div", {
            "class": "".concat(classPrefix, "-select-option"),
            "on": {
              "click": handlePanelTopClick
            }
          }, ["..."]);
        },
        panelBottomContent: function panelBottomContent() {
          return h("div", {
            "class": "".concat(classPrefix, "-select-option"),
            "on": {
              "click": handlePanelBottomClick
            }
          }, ["..."]);
        }
      })
    })]), h(_Jumper, {
      "props": _objectSpread({}, {
        tips: labelMap[this.mode],
        onChange: this.onJumperClick,
        size: "small"
      })
    })]);
  }
});

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