/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var debounce = require('lodash/debounce');
var dayjs = require('dayjs');
var _commonjsHelpers = require('./dep-1641a31f.js');
var timePicker_interface = require('../time-picker/interface.js');
var timePicker_panel_props = require('../time-picker/panel/props.js');
var timePicker_constant = require('../time-picker/constant.js');
var config = require('../config.js');

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

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

var customParseFormat = _commonjsHelpers.createCommonjsModule(function (module, exports) {
  !function (e, t) {
    module.exports = t() ;
  }(_commonjsHelpers.commonjsGlobal, function () {

    var e = {
      LTS: "h:mm:ss A",
      LT: "h:mm A",
      L: "MM/DD/YYYY",
      LL: "MMMM D, YYYY",
      LLL: "MMMM D, YYYY h:mm A",
      LLLL: "dddd, MMMM D, YYYY h:mm A"
    },
        t = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?M?M?|Do|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g,
        n = /\d\d/,
        r = /\d\d?/,
        i = /\d*[^\s\d-_:/()]+/,
        o = {},
        s = function s(e) {
      return (e = +e) + (e > 68 ? 1900 : 2e3);
    };

    var a = function a(e) {
      return function (t) {
        this[e] = +t;
      };
    },
        f = [/[+-]\d\d:?(\d\d)?|Z/, function (e) {
      (this.zone || (this.zone = {})).offset = function (e) {
        if (!e) return 0;
        if ("Z" === e) return 0;
        var t = e.match(/([+-]|\d\d)/g),
            n = 60 * t[1] + (+t[2] || 0);
        return 0 === n ? 0 : "+" === t[0] ? -n : n;
      }(e);
    }],
        h = function h(e) {
      var t = o[e];
      return t && (t.indexOf ? t : t.s.concat(t.f));
    },
        u = function u(e, t) {
      var n,
          r = o.meridiem;

      if (r) {
        for (var i = 1; i <= 24; i += 1) {
          if (e.indexOf(r(i, 0, t)) > -1) {
            n = i > 12;
            break;
          }
        }
      } else n = e === (t ? "pm" : "PM");

      return n;
    },
        d = {
      A: [i, function (e) {
        this.afternoon = u(e, !1);
      }],
      a: [i, function (e) {
        this.afternoon = u(e, !0);
      }],
      S: [/\d/, function (e) {
        this.milliseconds = 100 * +e;
      }],
      SS: [n, function (e) {
        this.milliseconds = 10 * +e;
      }],
      SSS: [/\d{3}/, function (e) {
        this.milliseconds = +e;
      }],
      s: [r, a("seconds")],
      ss: [r, a("seconds")],
      m: [r, a("minutes")],
      mm: [r, a("minutes")],
      H: [r, a("hours")],
      h: [r, a("hours")],
      HH: [r, a("hours")],
      hh: [r, a("hours")],
      D: [r, a("day")],
      DD: [n, a("day")],
      Do: [i, function (e) {
        var t = o.ordinal,
            n = e.match(/\d+/);
        if (this.day = n[0], t) for (var r = 1; r <= 31; r += 1) {
          t(r).replace(/\[|\]/g, "") === e && (this.day = r);
        }
      }],
      M: [r, a("month")],
      MM: [n, a("month")],
      MMM: [i, function (e) {
        var t = h("months"),
            n = (h("monthsShort") || t.map(function (e) {
          return e.slice(0, 3);
        })).indexOf(e) + 1;
        if (n < 1) throw new Error();
        this.month = n % 12 || n;
      }],
      MMMM: [i, function (e) {
        var t = h("months").indexOf(e) + 1;
        if (t < 1) throw new Error();
        this.month = t % 12 || t;
      }],
      Y: [/[+-]?\d+/, a("year")],
      YY: [n, function (e) {
        this.year = s(e);
      }],
      YYYY: [/\d{4}/, a("year")],
      Z: f,
      ZZ: f
    };

    function c(n) {
      var r, i;
      r = n, i = o && o.formats;

      for (var s = (n = r.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, function (t, n, r) {
        var o = r && r.toUpperCase();
        return n || i[r] || e[r] || i[o].replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, function (e, t, n) {
          return t || n.slice(1);
        });
      })).match(t), a = s.length, f = 0; f < a; f += 1) {
        var h = s[f],
            u = d[h],
            c = u && u[0],
            l = u && u[1];
        s[f] = l ? {
          regex: c,
          parser: l
        } : h.replace(/^\[|\]$/g, "");
      }

      return function (e) {
        for (var t = {}, n = 0, r = 0; n < a; n += 1) {
          var i = s[n];
          if ("string" == typeof i) r += i.length;else {
            var o = i.regex,
                f = i.parser,
                h = e.slice(r),
                u = o.exec(h)[0];
            f.call(t, u), e = e.replace(u, "");
          }
        }

        return function (e) {
          var t = e.afternoon;

          if (void 0 !== t) {
            var n = e.hours;
            t ? n < 12 && (e.hours += 12) : 12 === n && (e.hours = 0), delete e.afternoon;
          }
        }(t), t;
      };
    }

    return function (e, t, n) {
      n.p.customParseFormat = !0, e && e.parseTwoDigitYear && (s = e.parseTwoDigitYear);
      var r = t.prototype,
          i = r.parse;

      r.parse = function (e) {
        var t = e.date,
            r = e.utc,
            s = e.args;
        this.$u = r;
        var a = s[1];

        if ("string" == typeof a) {
          var f = !0 === s[2],
              h = !0 === s[3],
              u = f || h,
              d = s[2];
          h && (d = s[2]), o = this.$locale(), !f && d && (o = n.Ls[d]), this.$d = function (e, t, n) {
            try {
              if (["x", "X"].indexOf(t) > -1) return new Date(("X" === t ? 1e3 : 1) * e);
              var r = c(t)(e),
                  i = r.year,
                  o = r.month,
                  s = r.day,
                  a = r.hours,
                  f = r.minutes,
                  h = r.seconds,
                  u = r.milliseconds,
                  d = r.zone,
                  l = new Date(),
                  m = s || (i || o ? 1 : l.getDate()),
                  M = i || l.getFullYear(),
                  Y = 0;
              i && !o || (Y = o > 0 ? o - 1 : l.getMonth());
              var p = a || 0,
                  v = f || 0,
                  D = h || 0,
                  g = u || 0;
              return d ? new Date(Date.UTC(M, Y, m, p, v, D, g + 60 * d.offset * 1e3)) : n ? new Date(Date.UTC(M, Y, m, p, v, D, g)) : new Date(M, Y, m, p, v, D, g);
            } catch (e) {
              return new Date("");
            }
          }(t, a, r), this.init(), d && !0 !== d && (this.$L = this.locale(d).$L), u && t != this.format(a) && (this.$d = new Date("")), o = {};
        } else if (a instanceof Array) for (var l = a.length, m = 1; m <= l; m += 1) {
          s[1] = a[m - 1];
          var M = n.apply(this, s);

          if (M.isValid()) {
            this.$d = M.$d, this.$L = M.$L, this.init();
            break;
          }

          m === l && (this.$d = new Date(""));
        } else i.call(this, e);
      };
    };
  });
});

var name = "".concat(config.prefix, "-time-picker-pane-col");
dayjs__default["default"].extend(customParseFormat);
var PanelCol = Vue__default["default"].extend({
  name: name,
  data: function data() {
    return {
      splitValue: /* @__PURE__ */Object.create(null),
      timeArr: [timePicker_interface.EPickerCols.hour, timePicker_interface.EPickerCols.minute, timePicker_interface.EPickerCols.second]
    };
  },
  props: timePicker_panel_props.panelColProps(),
  computed: {
    valStr: function valStr() {
      return dayjs__default["default"](this.value, this.format).format(this.format);
    },
    isPm: function isPm() {
      return dayjs__default["default"](this.valStr, this.format).hour() >= 12;
    },
    currentTimes: function currentTimes() {
      return [Number(this.value.get("hour")), Number(this.value.get("minute")), Number(this.value.get("second"))];
    },
    timeItemMargin: function timeItemMargin() {
      var _this$$el, _this$$el$querySelect;

      var maskDom = (_this$$el = this.$el) === null || _this$$el === void 0 ? void 0 : (_this$$el$querySelect = _this$$el.querySelector) === null || _this$$el$querySelect === void 0 ? void 0 : _this$$el$querySelect.call(_this$$el, ".".concat(timePicker_constant.componentName, "__panel-body-active-mask > div"));
      return maskDom && parseInt(getComputedStyle(maskDom).margin, 10);
    }
  },
  methods: {
    getTimeItemHeight: function getTimeItemHeight(col) {
      var cols = this.$refs["".concat(col, "_scroller")];
      return cols.querySelector("li").offsetHeight;
    },
    generateColTime: function generateColTime(col) {
      var res;
      var count;

      if (this.timeArr.includes(col)) {
        var colIdx = this.timeArr.indexOf(col);

        if (col === timePicker_interface.EPickerCols.hour) {
          count = /[h]{1}/.test(this.format) ? 11 : 23;
        } else {
          count = 59;
        }

        res = this.generateTimeList(count, Number(this.steps[colIdx]));
      } else {
        res = this.localeMeridiems;
      }

      return res;
    },
    generateTimeList: function generateTimeList(num, step) {
      var res = [];
      var count = 0;

      while (count <= num) {
        if (!/[h]{1}/.test(this.format) && count < 10) {
          res.push("0".concat(count));
        } else {
          res.push(count);
        }

        count += step;
      }

      return res;
    },
    disableFilter: function disableFilter(preIdx, col) {
      var _this = this;

      var filteredIdx = preIdx;

      if (this.hideDisabledTime && this.disableTime) {
        var timeList = this.generateColTime(col);
        var index = this.timeArr.indexOf(col);
        filteredIdx = timeList.filter(function (t) {
          var _this$disableTime;

          var params = _this.currentTimes;
          params[index] = Number(t);
          return _this.disableTime && !((_this$disableTime = _this.disableTime) !== null && _this$disableTime !== void 0 && _this$disableTime.apply(_this, params));
        }).indexOf(preIdx);
      }

      return filteredIdx;
    },
    calculateTimeIdx: function calculateTimeIdx(time, step, type) {
      var timeIdx = time;
      timeIdx = this.disableFilter(Number(timeIdx), type);
      return Math.floor(Number(timeIdx) / Number(step));
    },
    getScrollDistance: function getScrollDistance(col, time) {
      var timeIndex;

      if (this.timeArr.includes(col)) {
        var colIdx = this.timeArr.indexOf(col);
        timeIndex = this.calculateTimeIdx(time, this.steps[colIdx], col);

        if (col === timePicker_interface.EPickerCols.hour && /[h]{1}/.test(this.format)) {
          timeIndex %= 12;
        }
      } else {
        timeIndex = this.localeMeridiems.indexOf(time.toUpperCase());
      }

      var timeItemTotalHeight = this.getTimeItemHeight(col) + this.timeItemMargin;
      var distance = timeIndex * timeItemTotalHeight + timeItemTotalHeight / 2;
      return distance;
    },
    scrollToTime: function scrollToTime(col, time) {
      var behavior = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "auto";
      var distance = this.getScrollDistance(col, time);
      var scroller = this.$refs["".concat(col, "_scroller")];
      if (!distance || !scroller) return;
      if (scroller.scrollTop === distance) return;
      scroller.scrollTo({
        top: distance,
        behavior: behavior
      });
    },
    updateTimeScrollPos: function updateTimeScrollPos() {
      var _this2 = this;

      var hour = timePicker_interface.EPickerCols.hour,
          minute = timePicker_interface.EPickerCols.minute,
          second = timePicker_interface.EPickerCols.second;
      var isNormalScroll = this.steps.filter(function (step) {
        return step !== 1;
      }).length < 1 || Number(this.splitValue[hour]) !== Number(this.steps[0]) - 1 || Number(this.splitValue[minute]) !== Number(this.steps[1]) - 1 || Number(this.splitValue[second]) !== Number(this.steps[2]) - 1;
      this.cols.forEach(function (col) {
        isNormalScroll ? _this2.scrollToTime(col, _this2.splitValue[col]) : _this2.scrollToTime(col, 0);
      });
    },
    generateColRows: function generateColRows(col) {
      var _this3 = this;

      var h = this.$createElement;
      return this.generateColTime(col).map(function (el) {
        var _ref;

        if (!_this3.timeItemCanUsed(col, el) && _this3.hideDisabledTime) return null;

        var isCurrent = _this3.isCurrent(col, el);

        if (isCurrent) {
          _this3.splitValue[col] = el;
        }

        var classNames = ["".concat(timePicker_constant.componentName, "__panel-body-scroll-item"), (_ref = {}, _defineProperty__default["default"](_ref, "".concat(config.prefix, "-is-disabled"), !_this3.timeItemCanUsed(col, el)), _defineProperty__default["default"](_ref, "".concat(config.prefix, "-is-current"), isCurrent), _ref)];
        return h("li", {
          "class": classNames,
          "on": {
            "click": function click(e) {
              return _this3.handleTimeItemClick(e, col, el);
            }
          }
        }, [el]);
      });
    },
    handleTimeItemClick: function handleTimeItemClick(_e, col, time) {
      var canUse = this.timeItemCanUsed(col, time);

      if (canUse) {
        this.scrollToTime(col, time, "smooth");
        this.$emit("time-pick", col, time);
      }
    },
    isCurrent: function isCurrent(col, colItem) {
      var colVal;

      switch (col) {
        case timePicker_interface.EPickerCols.meridiem:
          return this.isPm === (colItem === this.localeMeridiems[1]);

        case timePicker_interface.EPickerCols.hour:
        case timePicker_interface.EPickerCols.minute:
        case timePicker_interface.EPickerCols.second:
          colVal = this.value.get(col);

          if (col === timePicker_interface.EPickerCols.hour && /[h]{1}/.test(this.format)) {
            colVal %= 12;
          }

          return colVal === Number(colItem);
      }
    },
    timeItemCanUsed: function timeItemCanUsed(col, time) {
      if (this.timeArr.includes(col)) {
        var _this$disableTime2;

        var index = this.timeArr.indexOf(col);
        var params = this.currentTimes;
        params[index] = Number(time);
        return !(this.disableTime && (_this$disableTime2 = this.disableTime) !== null && _this$disableTime2 !== void 0 && _this$disableTime2.apply(this, params));
      }

      return true;
    },
    renderScrollers: function renderScrollers() {
      var _this4 = this;

      return this.cols.map(function (col) {
        return _this4.renderScroller(col);
      });
    },
    renderScroller: function renderScroller(col) {
      var _this5 = this;

      var h = this.$createElement;
      return h("ul", {
        "class": "".concat(timePicker_constant.componentName, "__panel-body-scroll"),
        "ref": "".concat(col, "_scroller"),
        "on": {
          "scroll": debounce__default["default"](function () {
            return _this5.handleScroll(col);
          }, 50)
        }
      }, [this.generateColRows(col)]);
    },
    closestLookup: function closestLookup(availableArr, calcVal, step) {
      if (step <= 1) return calcVal;
      if (calcVal < step) return 0;
      return availableArr.sort(function (a, b) {
        return Math.abs(calcVal + 1 - a) - Math.abs(calcVal + 1 - b);
      })[0];
    },
    handleScroll: function handleScroll(col) {
      var _this6 = this;

      var scrollVal;
      var cols = this.$refs["".concat(col, "_scroller")];
      var availableList = this.generateColTime(col);
      var scrollTop = cols.scrollTop;
      var itemHeight = this.getTimeItemHeight(col);

      if (this.timeArr.includes(col)) {
        var colIdx = this.timeArr.indexOf(col);
        var max = 59;

        if (col === timePicker_interface.EPickerCols.hour) {
          max = /[h]{1}/.test(this.format) ? 11 : 23;
        }

        scrollVal = Math.min(Math.abs(Math.round((scrollTop - itemHeight / 2) / (itemHeight + this.timeItemMargin) * Number(this.steps[colIdx]))), max);
        scrollVal = this.closestLookup(availableList, scrollVal, Number(this.steps[colIdx]));

        if (this.disableTime && this.hideDisabledTime) {
          scrollVal = availableList.filter(function (t) {
            var _this6$disableTime;

            var params = _this6.currentTimes;
            params[colIdx] = Number(t);
            return !((_this6$disableTime = _this6.disableTime) !== null && _this6$disableTime !== void 0 && _this6$disableTime.apply(_this6, params));
          })[scrollVal];
        }
      } else {
        scrollVal = Math.min(Math.abs(Math.round((scrollTop - itemHeight / 2) / (itemHeight + this.timeItemMargin))), 1);
        scrollVal = this.localeMeridiems[scrollVal];
      }

      var distance = this.getScrollDistance(col, scrollVal);

      if (distance !== scrollTop) {
        var scroller = this.$refs["".concat(col, "_scroller")];
        scroller.scrollTo({
          top: distance,
          behavior: "smooth"
        });
      }

      this.timeItemCanUsed(col, scrollVal) && this.$emit("time-pick", col, scrollVal);
    },
    renderActiveMask: function renderActiveMask() {
      var h = this.$createElement;
      return h("div", {
        "class": "".concat(timePicker_constant.componentName, "__panel-body-active-mask")
      }, [this.cols.map(function (_col, idx) {
        return h("div", {
          "key": idx
        });
      })]);
    }
  },
  render: function render() {
    var h = arguments[0];
    return h("div", {
      "class": "".concat(timePicker_constant.componentName, "__panel-body"),
      "attrs": {
        "col-num": "".concat(this.cols.length)
      }
    }, [this.renderActiveMask(), this.renderScrollers()]);
  }
});

exports.PanelCol = PanelCol;
exports.customParseFormat = customParseFormat;
//# sourceMappingURL=dep-648ca8b7.js.map
