import React, { forwardRef, useRef, useState, useMemo, useEffect, useImperativeHandle } from "react";
import { Picker } from "..";
import { useUpdateEffect } from "../hooks";
import { padZero, range } from "../utils";
import { times } from "./utils";
const TimePicker = /* @__PURE__ */ forwardRef((props, ref) => {
  const formatValue = (value) => {
    const {
      minHour,
      maxHour,
      minMinute,
      maxMinute
    } = props;
    if (!value) {
      value = `${padZero(minHour)}:${padZero(minHour)}`;
    }
    let [hour, minute] = value.split(":");
    hour = padZero(range(+hour, +minHour, +maxHour));
    minute = padZero(range(+minute, +minMinute, +maxMinute));
    return `${hour}:${minute}`;
  };
  const picker = useRef(null);
  const [currentDate, setCurrentDate] = useState(formatValue(props.value));
  const currentDateRef = useRef(null);
  currentDateRef.current = currentDate;
  const ranges = 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 = useMemo(() => ranges.map(({
    type,
    range: rangeArr
  }) => {
    let values = times(rangeArr[1] - rangeArr[0] + 1, (index) => padZero(rangeArr[0] + index));
    if (props.filter) {
      values = props.filter(type, values);
    }
    return {
      type,
      values
    };
  }), [ranges, props.filter]);
  const columns = 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);
  };
  useEffect(() => {
    updateColumnValue();
  }, [columns]);
  useUpdateEffect(() => {
    updateInnerValue();
  }, [props.filter, props.maxHour, props.minMinute, props.maxMinute]);
  useUpdateEffect(() => {
    setTimeout(updateInnerValue, 0);
  }, [props.minHour]);
  useUpdateEffect(() => {
    const value = formatValue(props.value);
    if (value !== currentDate) {
      setCurrentDate(value);
      updateColumnValue(value);
    }
  }, [props.value]);
  useImperativeHandle(ref, () => ({
    getPicker: () => picker.current
  }));
  return /* @__PURE__ */ React.createElement(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;
export {
  stdin_default as default
};
