"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

var _picker = _interopRequireDefault(require("../picker"));

var _utils = require("./utils");

var _utils2 = require("../utils");

var _hooks = require("../hooks");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const TimePicker = (0, _react().forwardRef)((props, ref) => {
  const formatValue = value => {
    const {
      minHour,
      maxHour,
      maxMinute,
      minMinute
    } = props;

    if (!value) {
      value = `${(0, _utils2.padZero)(minHour)}:${(0, _utils2.padZero)(minMinute)}`;
    }

    let [hour, minute] = value.split(':');
    hour = (0, _utils2.padZero)((0, _utils2.range)(hour, +minHour, +maxHour));
    minute = (0, _utils2.padZero)((0, _utils2.range)(minute, +minMinute, +maxMinute));
    return `${hour}:${minute}`;
  };

  const picker = (0, _react().useRef)(null);
  const [currentDate, setCurrentDate] = (0, _react().useState)(() => formatValue(props.value));
  const currentDateRef = (0, _react().useRef)();
  currentDateRef.current = currentDate;
  const ranges = (0, _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, _react().useMemo)(() => ranges.map(({
    type,
    range: rangeArr
  }) => {
    let values = (0, _utils.times)(rangeArr[1] - rangeArr[0] + 1, index => (0, _utils2.padZero)(rangeArr[0] + index));

    if (props.filter) {
      values = props.filter(type, values);
    }

    return {
      type,
      values
    };
  }), [ranges]);
  const columns = (0, _react().useMemo)(() => originColumns.map(column => ({
    values: column.values.map(value => props.formatter(column.type, value))
  })), [originColumns]);

  const updateColumnValue = () => {
    const pair = currentDateRef.current.split(':');
    const values = [props.formatter('hour', pair[0]), props.formatter('minute', pair[1])];
    setTimeout(() => {
      picker.current.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();
  };

  const onConfirm = () => {
    if (props.onConfirm) props.onConfirm(currentDateRef.current);
  };

  const onCancel = () => {
    if (props.onCancel) props.onCancel();
  };

  const onChange = () => {
    updateInnerValue();

    if (props.onChange) {
      props.onChange(currentDateRef.current);
    }
  };

  (0, _react().useEffect)(() => {
    updateColumnValue();
  }, [columns]);
  (0, _hooks.useUpdateEffect)(() => {
    updateInnerValue();
  }, [props.filter, props.maxHour, props.minMinute, props.maxMinute]);
  (0, _hooks.useUpdateEffect)(() => {
    setTimeout(updateInnerValue, 0);
  }, [props.minHour]);
  (0, _hooks.useUpdateEffect)(() => {
    const value = formatValue(props.value);

    if (value !== currentDate) {
      setCurrentDate(value);
      updateColumnValue();
    }
  }, [props.value]);
  (0, _react().useImperativeHandle)(ref, () => ({
    getPicker: () => picker.current
  }));
  return (0, _jsxRuntime().jsx)(_picker.default, {
    ref: picker,
    columns: columns,
    readonly: props.readonly,
    onChange: onChange,
    onConfirm: onConfirm,
    onCancel: onCancel,
    title: props.title,
    loading: props.loading,
    showToolbar: props.showToolbar,
    cancelButtonText: props.cancelButtonText,
    confirmButtonText: props.confirmButtonText,
    itemHeight: props.itemHeight,
    visibleItemCount: props.visibleItemCount,
    swipeDuration: props.swipeDuration
  });
});
TimePicker.defaultProps = {
  minHour: 0,
  maxHour: 23,
  minMinute: 0,
  maxMinute: 59,
  formatter: (type, value) => value
};
var _default = TimePicker;
exports.default = _default;