"use strict";
const React = require("react");
const index$2 = require("../Trigger/index.js");
const is = require("../_util/is.js");
const index = require("../ConfigProvider/index.js");
const dayjs = require("../_util/dayjs.js");
const index$1 = require("../../icon/react-icon/IconClockCircle/index.js");
const input = require("../_class/picker/input.js");
const inputRange = require("../_class/picker/input-range.js");
const useMergeProps = require("../_util/hooks/useMergeProps.js");
const usePrevious = require("../_util/hooks/usePrevious.js");
const useUpdate = require("../_util/hooks/useUpdate.js");
const context = require("./context.js");
const util = require("./util.js");
const pick = require("../_util/pick.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");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
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 __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var __spreadArray = globalThis && globalThis.__spreadArray || function(to, from, pack) {
  if (pack || arguments.length === 2)
    for (var i = 0, l = from.length, ar; i < l; i++) {
      if (ar || !(i in from)) {
        if (!ar)
          ar = Array.prototype.slice.call(from, 0, i);
        ar[i] = from[i];
      }
    }
  return to.concat(ar || Array.prototype.slice.call(from));
};
function getFormat(props) {
  return props.format || "HH:mm:ss";
}
var defaultProps = {
  allowClear: true,
  position: "bl",
  format: "HH:mm:ss",
  editable: true,
  order: true,
  scrollSticky: true
};
var Picker = function(baseProps) {
  var _a = React.useContext(index.ConfigContext), locale = _a.locale, getPrefixCls = _a.getPrefixCls, componentConfig = _a.componentConfig, rtl = _a.rtl;
  if (rtl) {
    defaultProps.position = "br";
  }
  var props = useMergeProps(baseProps, defaultProps, componentConfig === null || componentConfig === void 0 ? void 0 : componentConfig.TimePicker);
  var _b = props.allowClear, allowClear = _b === void 0 ? true : _b, className = props.className, disableConfirm = props.disableConfirm, style = props.style, placeholder = props.placeholder, getPopupContainer = props.getPopupContainer, disabled = props.disabled, position = props.position, isRangePicker = props.isRangePicker, picker = props.picker, error = props.error, triggerElement = props.triggerElement, triggerProps = props.triggerProps, propsValue = props.value, onChange = props.onChange, icons = props.icons, size = props.size, editable = props.editable, unmountOnExit = props.unmountOnExit, order = props.order, utcOffset = props.utcOffset, timezone = props.timezone;
  var format = getFormat(props);
  var prefixCls = getPrefixCls("timepicker");
  function getDefaultValue() {
    var value2;
    if (props.value) {
      value2 = dayjs.getDayjsValue(props.value, format, utcOffset, timezone);
    } else if (props.defaultValue) {
      value2 = dayjs.getDayjsValue(props.defaultValue, format, utcOffset, timezone);
    }
    return value2;
  }
  var _c = __read(React.useState(false), 2), popupVisible = _c[0], setPopupVisible = _c[1];
  var _d = __read(React.useState(getDefaultValue()), 2), value = _d[0], setValue = _d[1];
  var _e = __read(React.useState(), 2), valueShow = _e[0], setValueShow = _e[1];
  var _f = __read(React.useState(), 2), inputValue = _f[0], setInputValue = _f[1];
  var _g = __read(React.useState(0), 2), focusedInputIndex = _g[0], setFocusedInputIndex = _g[1];
  var mergedValue = "value" in props ? dayjs.getDayjsValue(propsValue, format, utcOffset, timezone) : value;
  var mergedPopupVisible = "popupVisible" in props ? props.popupVisible : popupVisible;
  var previousUtcOffset = usePrevious(utcOffset);
  var previousTimezone = usePrevious(timezone);
  useUpdate(function() {
    if (value && (previousUtcOffset !== utcOffset || timezone !== previousTimezone)) {
      var localValue = is.isArray(value) ? value.map(function(v) {
        return dayjs.toLocal(v, previousUtcOffset, previousTimezone);
      }) : dayjs.toLocal(value, previousUtcOffset, previousTimezone);
      var zoneValue = is.isArray(localValue) ? localValue.map(function(v) {
        return dayjs.toTimezone(v, utcOffset, timezone);
      }) : dayjs.toTimezone(localValue, utcOffset, timezone);
      setValue(zoneValue);
    }
  }, [utcOffset, previousUtcOffset, timezone, previousTimezone]);
  var refInput = React.useRef(null);
  function focusInput(index2) {
    refInput.current && refInput.current.focus && refInput.current.focus(index2);
  }
  function changeFocusedInputIndex(index2) {
    setFocusedInputIndex(index2);
    setTimeout(function() {
      return focusInput(index2);
    });
  }
  function onVisibleChange(visible) {
    if (visible) {
      setOpen(visible, function() {
        setTimeout(function() {
          return focusInput();
        });
      });
    } else {
      setOpen(false);
    }
  }
  function setOpen(visible, callback) {
    setPopupVisible(visible);
    setInputValue(void 0);
    callback && callback();
    if (!visible) {
      setValueShow(void 0);
    }
  }
  function onConfirmValue(vs) {
    var newValue = isRangePicker && order && is.isArray(vs) ? dayjs.getSortedDayjsArray(vs.map(function(v) {
      return util.getFormatTime(v);
    })) : vs;
    setValue(newValue);
    setValueShow(void 0);
    setInputValue(void 0);
    onHandleChange(newValue);
    if (!disableConfirm) {
      setOpen(false);
    }
  }
  function onHandleChange(vs) {
    if (is.isArray(vs) && dayjs.isDayjsArrayChange(mergedValue, vs)) {
      onChange && onChange(vs.map(function(t) {
        return dayjs.toLocal(t, utcOffset, timezone).format(format);
      }), vs.map(function(t) {
        return dayjs.toLocal(t, utcOffset, timezone);
      }));
    }
    if (is.isDayjs(vs) && dayjs.isDayjsChange(mergedValue, vs)) {
      onChange && onChange(dayjs.toLocal(vs, utcOffset, timezone).format(format), dayjs.toLocal(vs, utcOffset, timezone));
    }
  }
  function renderPopup() {
    var vs = isRangePicker ? is.isArray(valueShow) && valueShow.length ? valueShow : mergedValue : valueShow || mergedValue;
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
      className: prefixCls + "-container",
      onClick: function() {
        return focusInput();
      },
      children: React__default.default.cloneElement(picker, __assign(__assign({}, props), {
        format,
        inputValue,
        setInputValue,
        onConfirmValue,
        setValueShow,
        valueShow: vs,
        value: mergedValue,
        popupVisible: mergedPopupVisible,
        focusedInputIndex,
        changeFocusedInputIndex
      }))
    });
  }
  function onChangeInput(e) {
    var newInputValue = e.target.value;
    if (!popupVisible) {
      setPopupVisible(true);
    }
    setInputValue(newInputValue);
    confirmInputValue(newInputValue);
  }
  function confirmInputValue(newInputValue) {
    var newInputDayjs = dayjs.getDayjsValue(newInputValue, format);
    if (isRangePicker) {
      var newValueShow = __spreadArray([], __read(is.isArray(valueShow) ? valueShow : value || []), false);
      if (dayjs.isValidTimeString(newInputValue, format)) {
        newValueShow[focusedInputIndex] = newInputDayjs;
        var localDayjsArray = newValueShow.map(function(nv) {
          return dayjs.toLocal(nv, utcOffset, timezone);
        });
        props.onSelect && props.onSelect(localDayjsArray.map(function(la) {
          return la && la.format(format);
        }), localDayjsArray);
        setValueShow(newValueShow);
        setInputValue(void 0);
      }
    } else if (dayjs.isValidTimeString(newInputValue, format)) {
      var localDayjs = dayjs.toLocal(newInputDayjs, utcOffset, timezone);
      props.onSelect && props.onSelect(localDayjs.format(format), localDayjs);
      setValueShow(newInputDayjs);
      setInputValue(void 0);
    }
  }
  function onPressEnter() {
    if (isRangePicker) {
      if (is.isArray(valueShow) && valueShow.length) {
        if (inputValue && !dayjs.isValidTimeString(inputValue, format)) {
          setOpen(false);
        } else if (valueShow[0] === void 0 || valueShow[1] === void 0) {
          changeFocusedInputIndex(focusedInputIndex === 0 ? 1 : 0);
        } else if (valueShow.length === 2) {
          onConfirmValue(valueShow);
        }
      } else {
        setOpen(false);
      }
    } else {
      onConfirmValue(valueShow || mergedValue);
    }
  }
  function onPressTab(e) {
    e.preventDefault();
  }
  function onClear(e) {
    e.stopPropagation();
    onConfirmValue(void 0);
    onChange && onChange(void 0, void 0);
    props.onClear && props.onClear();
  }
  var rangePickerPlaceholder = isRangePicker && is.isArray(placeholder) ? placeholder : locale.TimePicker.placeholders;
  var inputPlaceHolder = placeholder || locale.TimePicker.placeholder;
  var suffixIcon = icons && icons.inputSuffix || /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {});
  var baseInputProps = __assign({
    style,
    className,
    popupVisible: mergedPopupVisible,
    format,
    disabled,
    error,
    size,
    onPressEnter,
    onClear,
    suffixIcon,
    editable,
    allowClear
  }, pick.pickDataAttributes(props));
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(context.Provider, {
    value: {
      utcOffset,
      timezone
    },
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$2.default, {
      ...__assign({
        popup: function() {
          return renderPopup();
        },
        trigger: "click",
        clickToClose: false,
        position,
        disabled,
        popupAlign: {
          bottom: 4
        },
        getPopupContainer,
        onVisibleChange,
        popupVisible: mergedPopupVisible,
        classNames: "slideDynamicOrigin",
        unmountOnExit: !!unmountOnExit
      }, triggerProps),
      children: triggerElement || (isRangePicker ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(inputRange, {
        ...__assign({}, baseInputProps, {
          ref: refInput,
          placeholder: rangePickerPlaceholder,
          value: is.isArray(valueShow) && valueShow.length ? valueShow : mergedValue,
          onChange: onChangeInput,
          inputValue,
          changeFocusedInputIndex,
          focusedInputIndex,
          onPressTab
        })
      }) : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(input, {
        ...__assign({}, baseInputProps, {
          ref: refInput,
          placeholder: inputPlaceHolder,
          value: valueShow || mergedValue,
          inputValue,
          onChange: onChangeInput
        })
      }))
    })
  });
};
const Picker$1 = Picker;
module.exports = Picker$1;
//# sourceMappingURL=picker.js.map
