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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import Vue from 'vue';
import dayjs from 'dayjs';
import TDateHeader from '../basic/header.js';
import TDateTable from '../basic/table.js';
import { prefix } from '../../config.js';
import { getToday, isSame, addMonth, getYears, getMonths, getWeeks, flagActive, firstUpperCase, subtractMonth, setDateTime } from '../../_common/js/date-picker/utils.js';
import props from '../props.js';
import 'tdesign-icons-vue';
import '../../button/button.js';
import '../../_chunks/dep-1ec74b8b.js';
import '../../utils/classnames.js';
import '../../loading/index.js';
import '../../loading/loading.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 '@babel/runtime/helpers/objectWithoutProperties';
import '../../utils/dom.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '../../utils/render-tnode.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/isObject';
import '../../utils/transfer-dom.js';
import '../../loading/props.js';
import '../../utils/withInstall.js';
import 'lodash/capitalize';
import './style/css.js';
import '../../loading/type.js';
import '../../loading/plugin.js';
import '../../button/props.js';
import '../../utils/ripple.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import 'lodash/merge';
import '../../_common/js/global-config/default-config.js';
import '../../_common/js/global-config/locale/zh_CN.js';
import '../../config-provider/type.js';
import '../../utils/mixins.js';
import '../basic/cell.js';
import '@babel/runtime/helpers/extends';
import 'lodash/chunk';

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 TODAY = getToday();
var LEFT = "left";
var RIGHT = "right";
var TDateRange = Vue.extend({
  name: "TDatePickerDateRange",
  components: {
    TDateHeader: TDateHeader,
    TDateTable: TDateTable
  },
  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: props.firstDayOfWeek,
    disableDate: props.disableDate,
    onChange: props.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(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(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(_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" && isSame(startValue, endValue, "month")) {
        var next = addMonth(endValue, 1);
        rightMonth = addMonth(endValue, 1).getMonth();
        rightYear = next.getFullYear();
      }

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

      if (this.mode === "year" && 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 = getWeeks({
            year: year,
            month: month
          }, options);
          break;

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

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

        default:
          break;
      }

      return flagActive(data, {
        start: start,
        end: end,
        type: type
      });
    },
    getClickHandler: function getClickHandler(direction, date, e) {
      var type = this["".concat(direction, "Type")];
      return this["click".concat(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 = addMonth(current, monthCount);
      } else if (flag === -1) {
        next = 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(this.firstClickValue).isBefore(dayjs(date), "day")) {
          this.endValue = date;
        } else {
          this.endValue = this.firstClickValue;
          this.startValue = date;
        }

        this.$props.onChange([setDateTime(this.startValue, 0, 0, 0), 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(prefix, "-date-picker__panels")
    }, [h("div", {
      "class": "".concat(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(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
      })
    })])]);
  }
});

export { TDateRange as default };
//# sourceMappingURL=date-range.js.map
