var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import__ = require("..");
var import_hooks = require("../hooks");
var import_utils = require("../utils");
var import_utils2 = require("./utils");
const TimePicker = /* @__PURE__ */ (0, import_react.forwardRef)((props, ref) => {
  const formatValue = (value) => {
    const {
      minHour,
      maxHour,
      minMinute,
      maxMinute
    } = props;
    if (!value) {
      value = `${(0, import_utils.padZero)(minHour)}:${(0, import_utils.padZero)(minHour)}`;
    }
    let [hour, minute] = value.split(":");
    hour = (0, import_utils.padZero)((0, import_utils.range)(+hour, +minHour, +maxHour));
    minute = (0, import_utils.padZero)((0, import_utils.range)(+minute, +minMinute, +maxMinute));
    return `${hour}:${minute}`;
  };
  const picker = (0, import_react.useRef)(null);
  const [currentDate, setCurrentDate] = (0, import_react.useState)(formatValue(props.value));
  const currentDateRef = (0, import_react.useRef)(null);
  currentDateRef.current = currentDate;
  const ranges = (0, import_react.useMemo)(() => [{
    type: "hour",
    range: [+props.minHour, +props.maxHour]
  }, {
    type: "minute",
    range: [+props.minMinute, +props.maxMinute]
  }], [props.minHour, props.maxHour, props.minMinute, props.maxMinute]);
  const originColumns = (0, import_react.useMemo)(() => ranges.map(({
    type,
    range: rangeArr
  }) => {
    let values = (0, import_utils2.times)(rangeArr[1] - rangeArr[0] + 1, (index) => (0, import_utils.padZero)(rangeArr[0] + index));
    if (props.filter) {
      values = props.filter(type, values);
    }
    return {
      type,
      values
    };
  }), [ranges, props.filter]);
  const columns = (0, import_react.useMemo)(() => originColumns.map((column) => ({
    values: column.values.map((value) => props.formatter(column.type, value))
  })), [originColumns]);
  const updateColumnValue = (date) => {
    const pair = date ? date.split(":") : currentDate.split(":");
    const values = [props.formatter("hour", pair[0]), props.formatter("minute", pair[1])];
    setTimeout(() => {
      var _a;
      (_a = picker.current) == null ? void 0 : _a.setValues(values);
    }, 0);
  };
  const updateInnerValue = () => {
    const [hourIndex, minuteIndex] = picker.current.getIndexes();
    const [hourColumn, minuteColumn] = originColumns;
    const hour = hourColumn.values[hourIndex] || hourColumn.values[0];
    const minute = minuteColumn.values[minuteIndex] || minuteColumn.values[0];
    setCurrentDate(formatValue(`${hour}:${minute}`));
    updateColumnValue(formatValue(`${hour}:${minute}`));
  };
  const onConfirm = () => {
    var _a;
    return (_a = props.onConfirm) == null ? void 0 : _a.call(props, currentDate);
  };
  const onCancel = () => props.onCancel();
  const onChange = (_) => {
    updateInnerValue();
    setTimeout(() => {
      var _a;
      (_a = props.onChange) == null ? void 0 : _a.call(props, currentDateRef);
    }, 0);
  };
  (0, import_react.useEffect)(() => {
    updateColumnValue();
  }, [columns]);
  (0, import_hooks.useUpdateEffect)(() => {
    updateInnerValue();
  }, [props.filter, props.maxHour, props.minMinute, props.maxMinute]);
  (0, import_hooks.useUpdateEffect)(() => {
    setTimeout(updateInnerValue, 0);
  }, [props.minHour]);
  (0, import_hooks.useUpdateEffect)(() => {
    const value = formatValue(props.value);
    if (value !== currentDate) {
      setCurrentDate(value);
      updateColumnValue(value);
    }
  }, [props.value]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    getPicker: () => picker.current
  }));
  return /* @__PURE__ */ import_react.default.createElement(import__.Picker, {
    ref: picker,
    columns,
    readonly: props.readonly,
    onChange,
    onCancel,
    onConfirm
  });
});
TimePicker.defaultProps = {
  minHour: 0,
  maxHour: 23,
  minMinute: 0,
  maxMinute: 59,
  formatter: (type, value) => value
};
var stdin_default = TimePicker;
