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

'use strict';

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

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

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

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

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__default["default"](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 = VueCompositionAPI.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 = components_configProvider_useConfig.useConfig("classPrefix"),
        classPrefix = _useConfig.classPrefix;

    var COMPONENT_NAME = hooks_useConfig.usePrefixClass("date-picker__header");

    var _useConfig2 = components_configProvider_useConfig.useConfig("datePicker"),
        global = _useConfig2.global;

    var yearOptions = VueCompositionAPI.ref(initOptions(props.year));
    var showMonthPicker = props.mode === "date" || props.mode === "week";
    var nearestYear = VueCompositionAPI.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 = VueCompositionAPI.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__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));
    }

    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(components_select_select["default"], {
      "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(components_select_select["default"], {
      "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(components_jumper_jumper["default"], {
      "props": _objectSpread({}, {
        tips: labelMap[this.mode],
        onChange: this.onJumperClick,
        size: "small"
      })
    })]);
  }
});

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