"use strict";
const React = require("react");
const pad = require("../_util/pad.js");
const util = require("./util.js");
const classNames = require("../_util/classNames.js");
const index = require("../ConfigProvider/index.js");
const dayjs = require("../_util/dayjs.js");
const index$1 = require("../Button/index.js");
const timeColumn = require("./time-column.js");
const context = require("./context.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var AMPM = ["am", "pm"];
function isUse12Hours(props) {
  return props.use12Hours || util.getColumnsFromFormat(props.format).use12Hours;
}
function TimePicker(props) {
  var _a = props.format, format = _a === void 0 ? "HH:mm:ss" : _a, onSelect = props.onSelect, popupVisible = props.popupVisible, _b = props.step, step = _b === void 0 ? {} : _b, disabledHours = props.disabledHours, disabledMinutes = props.disabledMinutes, disabledSeconds = props.disabledSeconds, hideDisabledOptions = props.hideDisabledOptions, onConfirmValue = props.onConfirmValue, isRangePicker = props.isRangePicker, confirmBtnDisabled = props.confirmBtnDisabled, propsValueShow = props.valueShow, setValueShow = props.setValueShow, extra = props.extra, disableConfirm = props.disableConfirm, hideFooter = props.hideFooter, _c = props.showNowBtn, showNowBtn = _c === void 0 ? true : _c, scrollSticky = props.scrollSticky;
  var _d = React.useContext(index.ConfigContext), getPrefixCls = _d.getPrefixCls, locale = _d.locale;
  var prefixCls = getPrefixCls("timepicker");
  var _e = React.useContext(context), utcOffset = _e.utcOffset, timezone = _e.timezone;
  var valueShow = dayjs.getDayjsValue(propsValueShow, format);
  var ampm = valueShow && valueShow.hour() >= 12 ? "pm" : "am";
  var use12Hours = isUse12Hours(props);
  var getShowList = React.useCallback(function(type) {
    var stepHour = step.hour || 1;
    var stepMinute = step.minute || 1;
    var stepSecond = step.second || 1;
    var list2 = [];
    if (type === "hour") {
      for (var i = 0; i < (use12Hours ? 12 : 24); i += stepHour) {
        list2.push(i);
      }
      if (use12Hours) {
        list2[0] = 12;
      }
    }
    if (type === "minute") {
      for (var i = 0; i < 60; i += stepMinute) {
        list2.push(i);
      }
    }
    if (type === "second") {
      for (var i = 0; i < 60; i += stepSecond) {
        list2.push(i);
      }
    }
    return list2;
  }, [step.hour, step.minute, step.second, use12Hours]);
  var HOURS = getShowList("hour");
  var MINUTES = getShowList("minute");
  var SECONDS = getShowList("second");
  var selectedHour = valueShow && valueShow.hour();
  selectedHour = use12Hours ? selectedHour > 12 ? selectedHour - 12 : selectedHour : selectedHour;
  if (use12Hours && selectedHour === 0 && ampm === "am") {
    selectedHour += 12;
  }
  var selectedMinute = valueShow && valueShow.minute();
  var selectedSecond = valueShow && valueShow.second();
  var getDefaultStr = React.useCallback(function(type) {
    switch (type) {
      case "hour":
        return typeof disabledHours === "function" ? pad.padStart(HOURS.find(function(h) {
          return disabledHours().indexOf(h) === -1;
        }) || 0, 2, "0") : pad.padStart(HOURS[0], 2, "0");
      case "minute":
        return typeof disabledMinutes === "function" ? pad.padStart(MINUTES.find(function(m) {
          return disabledMinutes(selectedHour).indexOf(m) === -1;
        }) || 0, 2, "0") : pad.padStart(MINUTES[0], 2, "0");
      case "second":
        return typeof disabledSeconds === "function" ? pad.padStart(SECONDS.find(function(s) {
          return disabledSeconds(selectedHour, selectedMinute).indexOf(s) === -1;
        }) || 0, 2, "0") : pad.padStart(SECONDS[0], 2, "0");
      default:
        return "00";
    }
  }, [HOURS, MINUTES, SECONDS, disabledHours, disabledMinutes, disabledSeconds, selectedHour, selectedMinute]);
  function onHandleSelect(selectedValue, unit) {
    var isUpperCase = util.getColumnsFromFormat(format).list.indexOf("A") !== -1;
    var _valueShow = valueShow || dayjs.dayjs(getDefaultStr("hour") + ":" + getDefaultStr("minute") + ":" + getDefaultStr("second"), "HH:mm:ss");
    var hour = _valueShow.hour();
    var minute = _valueShow.minute();
    var second = _valueShow.second();
    var selectedAmpm = isUpperCase ? ampm.toUpperCase() : ampm;
    var valueFormat = "HH:mm:ss";
    var newValue;
    if (use12Hours) {
      if (isUpperCase) {
        valueFormat = valueFormat + " A";
      } else {
        valueFormat = valueFormat + " a";
      }
    }
    if (use12Hours) {
      hour = hour > 12 ? hour - 12 : hour;
    }
    if (unit === "hour") {
      newValue = dayjs.dayjs(selectedValue + ":" + minute + ":" + second + " " + selectedAmpm, valueFormat, "en");
    }
    if (unit === "minute") {
      newValue = dayjs.dayjs(hour + ":" + selectedValue + ":" + second + " " + selectedAmpm, valueFormat, "en");
    }
    if (unit === "second") {
      newValue = dayjs.dayjs(hour + ":" + minute + ":" + selectedValue + " " + selectedAmpm, valueFormat, "en");
    }
    if (unit === "ampm") {
      newValue = dayjs.dayjs(hour + ":" + minute + ":" + second + " " + (isUpperCase ? selectedValue.toUpperCase() : selectedValue), valueFormat, "en");
    }
    newValue = dayjs.dayjs(newValue, valueFormat).locale(dayjs.dayjs.locale());
    onSelect && onSelect(dayjs.toLocal(newValue, utcOffset, timezone).format(format), dayjs.toLocal(newValue, utcOffset, timezone));
    if (!isRangePicker) {
      setValueShow && setValueShow(newValue);
      if (disableConfirm) {
        onConfirmValue(newValue);
      }
    }
  }
  function onConfirmTime() {
    if (valueShow) {
      onConfirmValue(valueShow);
    }
  }
  function onSelectNow() {
    var now = dayjs.getNow();
    var zoneNow = dayjs.getNow(utcOffset, timezone);
    onSelect && onSelect(now.format(format), now);
    if (disableConfirm) {
      onConfirmValue(zoneNow);
    } else {
      setValueShow && setValueShow(zoneNow);
    }
  }
  var baseTimeColumnProps = {
    prefixCls,
    onHandleSelect,
    popupVisible,
    scrollSticky
  };
  function renderHours() {
    var hours = hideDisabledOptions && typeof disabledHours === "function" ? HOURS.filter(function(h) {
      return disabledHours().indexOf(h) === -1;
    }) : HOURS;
    var list2 = hours.map(function(h) {
      return {
        label: pad.padStart("" + h, 2, "0"),
        value: h,
        selected: selectedHour !== void 0 && selectedHour === h,
        disabled: typeof disabledHours === "function" && disabledHours().indexOf(h) !== -1
      };
    });
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(timeColumn, {
      ...__assign({}, baseTimeColumnProps, {
        list: list2,
        value: selectedHour,
        unit: "hour"
      })
    });
  }
  function renderMinutes() {
    var minutes = hideDisabledOptions && typeof disabledMinutes === "function" ? MINUTES.filter(function(h) {
      return disabledMinutes(selectedHour).indexOf(h) === -1;
    }) : MINUTES;
    var list2 = minutes.map(function(m) {
      return {
        label: pad.padStart("" + m, 2, "0"),
        value: m,
        selected: selectedHour !== void 0 && selectedMinute === m,
        disabled: typeof disabledMinutes === "function" && disabledMinutes(selectedHour).indexOf(m) !== -1
      };
    });
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(timeColumn, {
      ...__assign({}, baseTimeColumnProps, {
        list: list2,
        value: selectedMinute,
        unit: "minute"
      })
    });
  }
  function renderSeconds() {
    var seconds = hideDisabledOptions && typeof disabledSeconds === "function" ? SECONDS.filter(function(h) {
      return disabledSeconds(selectedHour, selectedMinute).indexOf(h) === -1;
    }) : SECONDS;
    var list2 = seconds.map(function(s) {
      return {
        label: pad.padStart("" + s, 2, "0"),
        value: s,
        selected: selectedHour !== void 0 && selectedSecond === s,
        disabled: typeof disabledSeconds === "function" && disabledSeconds(selectedHour, selectedMinute).indexOf(s) !== -1
      };
    });
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(timeColumn, {
      ...__assign({}, baseTimeColumnProps, {
        list: list2,
        value: selectedSecond,
        unit: "second"
      })
    });
  }
  function renderAmPm() {
    var isUpperCase = util.getColumnsFromFormat(format).list.indexOf("A") !== -1;
    var list2 = AMPM.map(function(a) {
      return {
        label: isUpperCase ? a.toUpperCase() : a,
        value: a,
        selected: ampm === a
      };
    });
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(timeColumn, {
      ...__assign({}, baseTimeColumnProps, {
        list: list2,
        value: ampm,
        unit: "ampm"
      })
    });
  }
  var list = util.getColumnsFromFormat(format).list;
  var classNames$1 = classNames(prefixCls);
  var _hideFooter = hideFooter || disableConfirm && isRangePicker || !isRangePicker && disableConfirm && !showNowBtn;
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
    children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
      className: classNames$1,
      children: [(list.indexOf("H") !== -1 || list.indexOf("h") !== -1) && renderHours(), list.indexOf("m") !== -1 && renderMinutes(), list.indexOf("s") !== -1 && renderSeconds(), use12Hours && renderAmPm()]
    }), extra && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-footer-extra-wrapper",
      children: extra
    }), !_hideFooter && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
      className: prefixCls + "-footer-btn-wrapper",
      children: [!isRangePicker && showNowBtn ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {
        size: "mini",
        onClick: onSelectNow,
        children: locale.TimePicker.now
      }) : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {}), !disableConfirm && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {
        type: "primary",
        size: "mini",
        onClick: onConfirmTime,
        disabled: confirmBtnDisabled || !valueShow,
        children: locale.TimePicker.ok
      })]
    })]
  });
}
module.exports = TimePicker;
//# sourceMappingURL=time-picker.js.map
