/**
 * ldesign v0.2.14
 * (c) 2022/11/2 11:42 built by swimly
 * @license MIT
 */

import { defineComponent, toRefs, ref, computed, watch, onMounted, nextTick } from '@vue/composition-api';
import debounce from 'lodash/debounce';
import range from 'lodash/range';
import padStart from 'lodash/padStart';
import dayjs$1 from 'dayjs';
import { c as commonjsGlobal } from './dep-aef08e18.js';
import { d as dayjs } from './dep-c592b430.js';
import { useConfig } from '../components/config-provider/useConfig.js';
import { panelColProps } from '../components/time-picker/panel/props.js';

var customParseFormat$1 = {exports: {}};

(function (module, exports) {
  !function (e, t) {
    module.exports = t() ;
  }(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 (e) {
        return (e = +e) + (e > 68 ? 1900 : 2e3);
      };
    var a = function (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 (e) {
        var t = o[e];
        return t && (t.indexOf ? t : t.s.concat(t.f));
      },
      u = function (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);
      };
    };
  });
})(customParseFormat$1);
var customParseFormat = customParseFormat$1.exports;

function validateInputValue(value, format) {
  return dayjs(value, format).format(format) === value;
}
function formatInputValue(value, format) {
  return dayjs(value, format).format(format);
}
function closestLookup(availableArr, calcVal, step) {
  if (step <= 1) return calcVal;
  return availableArr.sort((a, b) => Math.abs(calcVal + 1 - a) - Math.abs(calcVal + 1 - b))[0];
}

var AM = "am";
var PM = "pm";
var MERIDIEM_LIST = [AM, PM];
var TIME_PICKER_EMPTY = [void 0, void 0];
var DEFAULT_STEPS = [1, 1, 1];
var DEFAULT_FORMAT = "HH:mm:ss";
var EPickerCols = /* @__PURE__ */(EPickerCols2 => {
  EPickerCols2["hour"] = "hour";
  EPickerCols2["minute"] = "minute";
  EPickerCols2["second"] = "second";
  EPickerCols2["milliSecond"] = "millisecond";
  EPickerCols2["meridiem"] = "meridiem";
  return EPickerCols2;
})(EPickerCols || {});
var TIME_FORMAT = /(a\s+|A\s+)?(h+|H+)?:?(m+)?:?(s+)?:?(S+)?(\s+a|\s+A)?/;
var TWELVE_HOUR_FORMAT = /[h]{1}/;

dayjs$1.extend(customParseFormat);
const timeArr = [EPickerCols.hour, EPickerCols.minute, EPickerCols.second, EPickerCols.milliSecond];
const panelOffset = {
  top: 15,
  bottom: 21
};
var SinglePanel = defineComponent({
  name: "LTimePickerPanelCol",
  props: {
    ...panelColProps(),
    position: String,
    triggerScroll: Boolean,
    onChange: Function,
    resetTriggerScroll: Function,
    disableTime: Function,
    isShowPanel: Boolean
  },
  setup(props, ctx) {
    const {
      steps,
      value,
      format,
      position,
      triggerScroll
    } = toRefs(props);
    const {
      global
    } = useConfig("timePicker");
    const {
      classPrefix
    } = useConfig();
    const cols = ref([]);
    const bodyRef = ref();
    const maskRef = ref(null);
    const dayjsValue = computed(() => {
      const isStepsSet = !!steps.value.filter(v => v > 1).length;
      if (value.value) return dayjs$1(value.value, format.value);
      if (isStepsSet) return dayjs$1().hour(0).minute(0).second(0);
      return dayjs$1();
    });
    const panelClassName = computed(() => `${classPrefix.value}-time-picker__panel`);
    watch(() => dayjsValue.value, () => {
      if (dayjsValue.value) updateTimeScrollPos(true);
    });
    watch(() => triggerScroll.value, () => {
      if (triggerScroll.value) updateTimeScrollPos(true);
    });
    onMounted(() => {
      const match = format.value.match(TIME_FORMAT);
      const [, startCol, hourCol, minuteCol, secondCol, milliSecondCol, endCol] = match;
      const {
        meridiem,
        hour,
        minute,
        second,
        milliSecond
      } = EPickerCols;
      const renderCol = [startCol && meridiem, hourCol && hour, minuteCol && minute, secondCol && second, milliSecondCol && milliSecond, endCol && meridiem].filter(v => !!v);
      cols.value = renderCol;
    });
    const getItemHeight = () => {
      var _maskRef$value;
      const maskDom = (_maskRef$value = maskRef.value) === null || _maskRef$value === void 0 ? void 0 : _maskRef$value.querySelector("div");
      if (!maskDom) {
        return {
          offsetHeight: 0,
          margin: 0
        };
      }
      return {
        offsetHeight: maskDom.offsetHeight,
        margin: parseInt(getComputedStyle(maskDom).marginTop, 10)
      };
    };
    const timeItemCanUsed = (col, el) => {
      const colIdx = timeArr.indexOf(col);
      if (colIdx !== -1) {
        var _props$disableTime, _props$disableTime$ca, _props$disableTime$ca2;
        const params = [dayjsValue.value.hour(), dayjsValue.value.minute(), dayjsValue.value.second()];
        params[colIdx] = Number(el);
        return !((_props$disableTime = props.disableTime) !== null && _props$disableTime !== void 0 && (_props$disableTime$ca = _props$disableTime.call(props, ...params, {
          partial: position.value || "start"
        })) !== null && _props$disableTime$ca !== void 0 && (_props$disableTime$ca2 = _props$disableTime$ca[col]) !== null && _props$disableTime$ca2 !== void 0 && _props$disableTime$ca2.includes(Number(el)));
      }
      return true;
    };
    const getColList = col => {
      let count = 0;
      if (timeArr.includes(col)) {
        const colIdx = timeArr.indexOf(col);
        const colStep = steps.value[colIdx] || 1;
        if (col === EPickerCols.hour) count = TWELVE_HOUR_FORMAT.test(format.value) ? 11 : 23;else if (col === EPickerCols.milliSecond) count = 999;else count = 59;
        const colList = range(0, count + 1, Number(colStep)).map(v => padStart(String(v), 2, "0")) || [];
        return props.hideDisabledTime && !!props.disableTime ? colList.filter(t => {
          var _props$disableTime2, _props$disableTime2$c, _props$disableTime2$c2;
          const params = [dayjsValue.value.hour(), dayjsValue.value.minute(), dayjsValue.value.second()];
          params[colIdx] = Number(t);
          return !((_props$disableTime2 = props.disableTime) !== null && _props$disableTime2 !== void 0 && (_props$disableTime2$c = _props$disableTime2.call(props, ...params, {
            partial: position.value || "start"
          })) !== null && _props$disableTime2$c !== void 0 && (_props$disableTime2$c2 = _props$disableTime2$c[col]) !== null && _props$disableTime2$c2 !== void 0 && _props$disableTime2$c2.includes(Number(t)));
        }) : colList;
      }
      return MERIDIEM_LIST;
    };
    const getScrollDistance = (col, time) => {
      if (col === EPickerCols.hour && /[h]{1}/.test(format.value)) time %= 12;
      const itemIdx = getColList(col).indexOf(padStart(String(time), 2, "0"));
      const {
        offsetHeight,
        margin
      } = getItemHeight();
      const timeItemTotalHeight = offsetHeight + margin;
      const distance = Math.abs(Math.max(0, itemIdx) * timeItemTotalHeight);
      return distance;
    };
    const handleScroll = col => {
      var _ctx$refs, _props$onChange;
      let val;
      let formattedVal;
      if (!props.isShowPanel) return;
      const scrollTop = ((_ctx$refs = ctx.refs[`${col}Col`]) === null || _ctx$refs === void 0 ? void 0 : _ctx$refs.scrollTop) + panelOffset.top;
      const {
        offsetHeight,
        margin
      } = getItemHeight();
      const timeItemTotalHeight = offsetHeight + margin;
      let colStep = Math.abs(Math.round(scrollTop / timeItemTotalHeight + 0.5));
      const meridiem = MERIDIEM_LIST[Math.min(colStep - 1, 1)].toLowerCase();
      if (Number.isNaN(colStep)) colStep = 1;
      if (timeArr.includes(col)) {
        let max = 59;
        if (col === EPickerCols.hour) max = /[h]{1}/.test(format.value) ? 11 : 23;else if (col === EPickerCols.milliSecond) max = 999;
        const colIdx = timeArr.indexOf(col);
        const availableArr = range(0, max + 1, Number(steps.value[colIdx]) || 1);
        val = closestLookup(availableArr, Number(getColList(col)[Math.min(colStep - 1, max + 1, availableArr.length - 1)]), Number(steps.value[colIdx]) || 1);
        if (Number.isNaN(val)) val = availableArr[availableArr.length - 1];
        if (col === EPickerCols.hour && cols.value.includes(EPickerCols.meridiem) && dayjsValue.value.hour() >= 12) {
          val = Number(val) + 12;
        }
      } else {
        val = meridiem;
      }
      const distance = getScrollDistance(col, val);
      if (!dayjs$1(dayjsValue.value).isValid() || value.value && !dayjs$1(value.value, format.value, true).isValid()) return;
      if (timeArr.includes(col)) {
        var _dayjsValue$value$col, _dayjsValue$value;
        if (timeItemCanUsed(col, val)) formattedVal = (_dayjsValue$value$col = (_dayjsValue$value = dayjsValue.value)[col]) === null || _dayjsValue$value$col === void 0 ? void 0 : _dayjsValue$value$col.call(_dayjsValue$value, val).format(format.value);
      } else {
        const currentHour = dayjsValue.value.hour();
        if (meridiem === AM && currentHour >= 12) formattedVal = dayjsValue.value.hour(currentHour - 12).format(format.value);else if (meridiem === PM && currentHour < 12) formattedVal = dayjsValue.value.hour(currentHour + 12).format(format.value);else formattedVal = dayjsValue.value.format(format.value);
      }
      if (formattedVal !== value.value) (_props$onChange = props.onChange) === null || _props$onChange === void 0 ? void 0 : _props$onChange.call(props, formattedVal);
      if (distance !== scrollTop) {
        var _scrollCtrl$scrollTo;
        const scrollCtrl = ctx.refs[`${col}Col`];
        if (!scrollCtrl || scrollCtrl.scrollTop === distance) return;
        (_scrollCtrl$scrollTo = scrollCtrl.scrollTo) === null || _scrollCtrl$scrollTo === void 0 ? void 0 : _scrollCtrl$scrollTo.call(scrollCtrl, {
          top: distance,
          behavior: "smooth"
        });
      }
    };
    const scrollToTime = function (col, time, idx) {
      var _scrollCtrl$scrollTo2;
      let behavior = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "auto";
      const distance = getScrollDistance(col, time);
      const scrollCtrl = ctx.refs[`${col}Col`];
      if (!scrollCtrl || scrollCtrl.scrollTop === distance || !timeItemCanUsed(col, time)) return;
      (_scrollCtrl$scrollTo2 = scrollCtrl.scrollTo) === null || _scrollCtrl$scrollTo2 === void 0 ? void 0 : _scrollCtrl$scrollTo2.call(scrollCtrl, {
        top: distance,
        behavior
      });
    };
    const handleTimeItemClick = (col, el, idx) => {
      if (!timeItemCanUsed(col, el)) return;
      if (timeArr.includes(col)) {
        if (col === EPickerCols.hour && dayjsValue.value.format("a") === PM && cols.value.includes(EPickerCols.meridiem)) el = Number(el) + 12;
        scrollToTime(col, el, idx, "smooth");
      } else {
        var _props$onChange2, _props$onChange3;
        const currentHour = dayjsValue.value.hour();
        if (el === AM && currentHour >= 12) (_props$onChange2 = props.onChange) === null || _props$onChange2 === void 0 ? void 0 : _props$onChange2.call(props, dayjsValue.value.hour(currentHour - 12).format(format.value));else if (el === PM && currentHour < 12) (_props$onChange3 = props.onChange) === null || _props$onChange3 === void 0 ? void 0 : _props$onChange3.call(props, dayjsValue.value.hour(currentHour + 12).format(format.value));
      }
    };
    const updateTimeScrollPos = function () {
      let isAutoScroll = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
      const behavior = value.value && !isAutoScroll ? "smooth" : "auto";
      const isStepsSet = !!steps.value.filter(v => v > 1).length;
      nextTick(() => {
        cols.value.forEach((col, idx) => {
          if (!isStepsSet || isStepsSet && value.value) {
            var _dayjsValue$value$col2, _dayjsValue$value2;
            scrollToTime(col, timeArr.includes(col) ? (_dayjsValue$value$col2 = (_dayjsValue$value2 = dayjsValue.value)[col]) === null || _dayjsValue$value$col2 === void 0 ? void 0 : _dayjsValue$value$col2.call(_dayjsValue$value2) : dayjsValue.value.format("a"), idx, behavior);
          } else {
            var _getColList;
            scrollToTime(col, (_getColList = getColList(col)) === null || _getColList === void 0 ? void 0 : _getColList[0], idx, behavior);
          }
        });
      });
      props.resetTriggerScroll();
    };
    const isCurrent = (col, colItem) => {
      var _dayjsValue$value$col3, _dayjsValue$value3;
      let colVal;
      if (col === EPickerCols.meridiem) {
        const currentMeridiem = dayjsValue.value.format("a");
        return currentMeridiem === colItem;
      }
      colVal = (_dayjsValue$value$col3 = (_dayjsValue$value3 = dayjsValue.value)[col]) === null || _dayjsValue$value$col3 === void 0 ? void 0 : _dayjsValue$value$col3.call(_dayjsValue$value3);
      if (col === EPickerCols.hour && /[h]{1}/.test(format.value)) colVal %= 12;
      return colVal === Number(colItem);
    };
    return {
      getColList,
      isCurrent,
      bodyRef,
      maskRef,
      global,
      classPrefix,
      panelClassName,
      cols,
      timeItemCanUsed,
      handleScroll,
      handleTimeItemClick
    };
  },
  render() {
    var _this$cols$map, _this$cols, _this$cols$map2, _this$cols2;
    const h = arguments[0];
    return h("div", {
      "class": `${this.panelClassName}-body`,
      "ref": "bodyRef"
    }, [h("div", {
      "class": `${this.panelClassName}-body-active-mask`,
      "ref": "maskRef"
    }, [(_this$cols$map = (_this$cols = this.cols).map) === null || _this$cols$map === void 0 ? void 0 : _this$cols$map.call(_this$cols, (col, idx) => h("div", {
      "key": `${col}_${idx}`
    }))]), (_this$cols$map2 = (_this$cols2 = this.cols).map) === null || _this$cols$map2 === void 0 ? void 0 : _this$cols$map2.call(_this$cols2, (col, idx) => h("ul", {
      "key": `${col}_${idx}`,
      "ref": `${col}Col`,
      "class": `${this.panelClassName}-body-scroll`,
      "on": {
        "scroll": debounce(() => this.handleScroll(col), 50)
      }
    }, [this.getColList(col).map(el => h("li", {
      "key": el,
      "class": [`${this.panelClassName}-body-scroll-item`, {
        [`${this.classPrefix}-is-disabled`]: !this.timeItemCanUsed(col, el),
        [`${this.classPrefix}-is-current`]: this.isCurrent(col, el)
      }],
      "on": {
        "click": () => this.handleTimeItemClick(col, el, idx)
      }
    }, [timeArr.includes(col) ? TWELVE_HOUR_FORMAT.test(this.format) && el === "00" ? "12" : el : this.global[el === AM ? "anteMeridiem" : "postMeridiem"]]))]))]);
  }
});

export { DEFAULT_FORMAT as D, SinglePanel as S, TIME_PICKER_EMPTY as T, DEFAULT_STEPS as a, customParseFormat as c, formatInputValue as f, validateInputValue as v };
//# sourceMappingURL=dep-ac0ff195.js.map
