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(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  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_picker = require("../picker");
var import_hooks = require("../hooks");
var import_utils = require("../utils");
var import_utils2 = require("./utils");
var import_use_ref_state = __toESM(require("../hooks/use-ref-state"));
var import_raf = require("../utils/raf");
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, currentDateRef] = (0, import_use_ref_state.default)(formatValue(props.value));
  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 = (0, import_react.useCallback)(() => {
    const pair = currentDate.split(":");
    const values = [props.formatter("hour", pair[0]), props.formatter("minute", pair[1])];
    (0, import_raf.doubleRaf)(() => {
      var _a;
      (_a = picker.current) == null ? void 0 : _a.setValues(values);
    });
  }, [currentDate, props.formatter]);
  const updateInnerValue = (indexes) => {
    const [hourIndex, minuteIndex] = indexes;
    const [hourColumn, minuteColumn] = originColumns;
    const hour = hourColumn.values[hourIndex] || hourColumn.values[0];
    const minute = minuteColumn.values[minuteIndex] || minuteColumn.values[0];
    return formatValue(`${hour}:${minute}`);
  };
  const onConfirm = () => {
    var _a;
    return (_a = props.onConfirm) == null ? void 0 : _a.call(props, currentDate);
  };
  const onCancel = () => props.onCancel();
  const onChange = (_) => {
    var _a;
    if (picker.current) {
      const indexes = picker.current.getIndexes();
      const nextValue = updateInnerValue(indexes);
      setCurrentDate(nextValue);
      (_a = props.onChange) == null ? void 0 : _a.call(props, nextValue);
    }
  };
  (0, import_react.useEffect)(() => {
    updateColumnValue();
  }, [updateColumnValue]);
  (0, import_hooks.useUpdateEffect)(() => {
    const nextValue = formatValue(props.value);
    if (nextValue !== currentDate) {
      setCurrentDate(nextValue);
    }
  }, [props.value, props.filter, props.minHour, props.maxHour, props.minMinute, props.maxMinute]);
  (0, import_react.useImperativeHandle)(ref, () => ({
    getPicker: () => picker.current
  }));
  return /* @__PURE__ */ import_react.default.createElement(import_picker.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;
