/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var Vue = require('vue');
var dayjs = require('dayjs');
var datePicker_basic_header = require('../basic/header.js');
var datePicker_basic_table = require('../basic/table.js');
var config = require('../../config.js');
var _common_js_datePicker_utils = require('../../_common/js/date-picker/utils.js');
var datePicker_props = require('../props.js');
require('tdesign-icons-vue');
require('../../button/button.js');
require('../../_chunks/dep-33ee4bce.js');
require('../../utils/classnames.js');
require('../../loading/index.js');
require('../../loading/loading.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('@babel/runtime/helpers/objectWithoutProperties');
require('../../utils/dom.js');
require('raf');
require('lodash/isString');
require('../../utils/easing.js');
require('../../utils/render-tnode.js');
require('@babel/runtime/helpers/typeof');
require('lodash/isObject');
require('../../utils/transfer-dom.js');
require('../../loading/props.js');
require('../../utils/withInstall.js');
require('lodash/capitalize');
require('../../loading/plugin.js');
require('../../button/props.js');
require('../../utils/ripple.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('lodash/merge');
require('../../_common/js/global-config/default-config.js');
require('../../_common/js/global-config/locale/zh_CN.js');
require('../../utils/mixins.js');
require('../basic/cell.js');
require('@babel/runtime/helpers/extends');
require('lodash/chunk');

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

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);
var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);

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 TODAY = _common_js_datePicker_utils.getToday();
var LEFT = "left";
var RIGHT = "right";
var TDateRange = Vue__default["default"].extend({
  name: "TDatePickerDateRange",
  components: {
    TDateHeader: datePicker_basic_header["default"],
    TDateTable: datePicker_basic_table["default"]
  },
  inheritAttrs: false,
  props: {
    global: {
      type: Object
    },
    mode: {
      type: String,
      "default": "date",
      validator: function validator(v) {
        return ["year", "month", "date"].indexOf(v) > -1;
      }
    },
    value: {
      type: Array,
      "default": function _default() {
        return [TODAY, TODAY];
      }
    },
    minDate: Date,
    maxDate: Date,
    firstDayOfWeek: datePicker_props["default"].firstDayOfWeek,
    disableDate: datePicker_props["default"].disableDate,
    onChange: datePicker_props["default"].onChange,
    onPick: Function
  },
  data: function data() {
    return {
      leftYear: null,
      leftMonth: null,
      rightMonth: null,
      rightYear: null,
      leftType: this.mode,
      rightType: this.mode,
      startValue: null,
      endValue: null,
      isFirstClick: true,
      firstClickValue: null
    };
  },
  computed: {
    leftData: function leftData() {
      return this.getData({
        year: this.leftYear,
        month: this.leftMonth,
        type: this.leftType
      });
    },
    rightData: function rightData() {
      return this.getData({
        year: this.rightYear,
        month: this.rightMonth,
        type: this.rightType
      });
    }
  },
  watch: {
    value: {
      handler: function handler(value) {
        var _value = _slicedToArray__default["default"](value, 2),
            _value$ = _value[0],
            startValue = _value$ === void 0 ? TODAY : _value$,
            _value$2 = _value[1],
            endValue = _value$2 === void 0 ? TODAY : _value$2;

        this.startValue = startValue;
        this.endValue = endValue;
      },
      immediate: true
    },
    mode: function mode(value) {
      this.leftType = value;
      this.rightType = value;
    }
  },
  created: function created() {
    this.initialPicker();
  },
  beforeDestroy: function beforeDestroy() {
    this.initialPicker();
  },
  methods: {
    initialPicker: function initialPicker() {
      var data = this.getLeftAndRightDataFromValue(this.value);
      this.leftYear = data.leftYear;
      this.leftMonth = data.leftMonth;
      this.rightYear = data.rightYear;
      this.rightMonth = data.rightMonth;
      this.leftType = this.$props.mode;
      this.rightType = this.$props.mode;

      var _this$$props$value = _slicedToArray__default["default"](this.$props.value, 2),
          startValue = _this$$props$value[0],
          endValue = _this$$props$value[1];

      this.startValue = startValue;
      this.endValue = endValue;
      this.isFirstClick = true;
      this.firstClickValue = TODAY;
    },
    getLeftAndRightDataFromValue: function getLeftAndRightDataFromValue(value) {
      var _ref = value || this.value,
          _ref2 = _slicedToArray__default["default"](_ref, 2),
          _ref2$ = _ref2[0],
          startValue = _ref2$ === void 0 ? TODAY : _ref2$,
          _ref2$2 = _ref2[1],
          endValue = _ref2$2 === void 0 ? TODAY : _ref2$2;

      var leftYear = startValue.getFullYear();
      var leftMonth = startValue.getMonth();
      var rightMonth = endValue.getMonth();
      var rightYear = endValue.getFullYear();

      if (this.mode === "date" && _common_js_datePicker_utils.isSame(startValue, endValue, "month")) {
        var next = _common_js_datePicker_utils.addMonth(endValue, 1);
        rightMonth = _common_js_datePicker_utils.addMonth(endValue, 1).getMonth();
        rightYear = next.getFullYear();
      }

      if (this.mode === "month" && _common_js_datePicker_utils.isSame(startValue, endValue, "year")) {
        rightYear = leftYear + 1;
      }

      if (this.mode === "year" && _common_js_datePicker_utils.isSame(startValue, endValue, "year")) {
        rightYear = leftYear + 10;
      }

      return {
        leftYear: leftYear,
        leftMonth: leftMonth,
        rightMonth: rightMonth,
        rightYear: rightYear
      };
    },
    getData: function getData(_ref3) {
      var _this$global;

      var year = _ref3.year,
          month = _ref3.month,
          type = _ref3.type;
      var disableDate = this.disableDate,
          minDate = this.minDate,
          maxDate = this.maxDate,
          startValue = this.startValue,
          endValue = this.endValue,
          firstDayOfWeek = this.firstDayOfWeek;
      var data;
      var start = startValue;
      var end = endValue;
      var options = {
        disableDate: disableDate,
        minDate: minDate,
        maxDate: maxDate,
        firstDayOfWeek: firstDayOfWeek,
        monthLocal: (_this$global = this.global) === null || _this$global === void 0 ? void 0 : _this$global.months
      };

      switch (type) {
        case "date":
          data = _common_js_datePicker_utils.getWeeks({
            year: year,
            month: month
          }, options);
          break;

        case "month":
          data = _common_js_datePicker_utils.getMonths(year, options);
          break;

        case "year":
          data = _common_js_datePicker_utils.getYears(year, options);
          break;
      }

      return _common_js_datePicker_utils.flagActive(data, {
        start: start,
        end: end,
        type: type
      });
    },
    getClickHandler: function getClickHandler(direction, date, e) {
      var type = this["".concat(direction, "Type")];
      return this["click".concat(_common_js_datePicker_utils.firstUpperCase(type))](date, e, direction);
    },
    clickHeader: function clickHeader(flag, direction) {
      var year = this["".concat(direction, "Year")];
      var month = this["".concat(direction, "Month")];
      var type = this["".concat(direction, "Type")];
      var monthCount;
      var next;

      switch (type) {
        case "date":
          monthCount = 1;
          break;

        case "month":
          monthCount = 12;
          break;

        case "year":
          monthCount = 120;
      }

      var current = new Date(year, month);

      if (flag === 1) {
        next = _common_js_datePicker_utils.addMonth(current, monthCount);
      } else if (flag === -1) {
        next = _common_js_datePicker_utils.subtractMonth(current, monthCount);
      } else {
        next = new Date();
      }

      this["".concat(direction, "Year")] = next.getFullYear();
      this["".concat(direction, "Month")] = next.getMonth();
    },
    clickDate: function clickDate(date, e) {
      var partial = "start";

      if (this.isFirstClick) {
        this.startValue = date;
        this.endValue = date;
        this.isFirstClick = false;
        this.firstClickValue = date;
      } else {
        if (dayjs__default["default"](this.firstClickValue).isBefore(dayjs__default["default"](date), "day")) {
          this.endValue = date;
        } else {
          this.endValue = this.firstClickValue;
          this.startValue = date;
        }

        this.$props.onChange([_common_js_datePicker_utils.setDateTime(this.startValue, 0, 0, 0), _common_js_datePicker_utils.setDateTime(this.endValue, 23, 59, 59)]);
        this.isFirstClick = true;
        partial = "end";
      }

      this.$props.onPick && this.$props.onPick(date, {
        e: e,
        partial: partial
      });
    },
    clickYear: function clickYear(date, e, type) {
      if (this.mode === "year") {
        if (this.isFirstClick) {
          this.startValue = date;
          this.isFirstClick = false;
          this.firstClickValue = date;
        } else {
          this.$props.onChange([this.startValue, this.endValue]);
          this.isFirstClick = true;
        }
      } else {
        this["".concat(type, "Type")] = "month";
        this["".concat(type, "Year")] = date.getFullYear();
      }
    },
    clickMonth: function clickMonth(date, e, type) {
      if (this.mode === "month") {
        if (this.isFirstClick) {
          this.startValue = date;
          this.isFirstClick = false;
          this.firstClickValue = date;
        } else {
          if (this.endValue < this.startValue) {
            this.endValue = this.startValue;
          }

          this.$props.onChange([this.startValue, this.endValue]);
          this.isFirstClick = true;
        }
      } else {
        this["".concat(type, "Type")] = "date";
        this["".concat(type, "Month")] = date.getMonth();
        this["".concat(type, "Year")] = date.getFullYear();
      }
    },
    onMouseEnter: function onMouseEnter(date) {
      if (this.isFirstClick) {
        return;
      }

      if (this.firstClickValue.getTime() > date.getTime()) {
        this.startValue = date;
        this.endValue = this.firstClickValue;
      } else {
        this.startValue = this.firstClickValue;
        this.endValue = date;
      }
    },
    onTypeChange: function onTypeChange() {
      this.startValue = this.firstClickValue;
      this.endValue = this.firstClickValue;
    },
    handleTypeChange: function handleTypeChange(direction, type) {
      this.$data["".concat(direction, "Type")] = type;
    }
  },
  render: function render() {
    var _this = this;

    var h = arguments[0];
    var leftType = this.leftType,
        rightType = this.rightType,
        firstDayOfWeek = this.firstDayOfWeek;
    return h("div", {
      "class": "".concat(config.prefix, "-date-picker__panels")
    }, [h("div", {
      "class": "".concat(config.prefix, "-date-picker__panel")
    }, [h("t-date-header", {
      "attrs": {
        "year": this.leftYear,
        "month": this.leftMonth,
        "type": leftType
      },
      "props": _objectSpread({}, {
        onBtnClick: function onBtnClick(flag) {
          return _this.clickHeader(flag, LEFT);
        },
        onTypeChange: function onTypeChange(type) {
          return _this.handleTypeChange(LEFT, type);
        }
      })
    }), h("t-date-table", {
      "attrs": {
        "type": leftType,
        "first-day-of-week": firstDayOfWeek,
        "data": this.leftData
      },
      "props": _objectSpread({}, {
        onCellClick: function onCellClick(date, e) {
          return _this.getClickHandler(LEFT, date, e);
        },
        onCellMouseEnter: this.onMouseEnter
      })
    })]), h("div", {
      "class": "".concat(config.prefix, "-date-picker__panel ")
    }, [h("t-date-header", {
      "attrs": {
        "year": this.rightYear,
        "month": this.rightMonth,
        "type": rightType
      },
      "props": _objectSpread({}, {
        onBtnClick: function onBtnClick(flag) {
          return _this.clickHeader(flag, RIGHT);
        },
        onTypeChange: function onTypeChange(type) {
          return _this.handleTypeChange(RIGHT, type);
        }
      })
    }), h("t-date-table", {
      "attrs": {
        "type": rightType,
        "first-day-of-week": firstDayOfWeek,
        "data": this.rightData
      },
      "on": _objectSpread({}, {
        "update:type": this.onTypeChange
      }),
      "props": _objectSpread({}, {
        onCellClick: function onCellClick(date, e) {
          return _this.getClickHandler(RIGHT, date, e);
        },
        onCellMouseEnter: this.onMouseEnter
      })
    })])]);
  }
});

exports["default"] = TDateRange;
//# sourceMappingURL=date-range.js.map
