"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

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 _getRequireWildcardCache(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; }

var TimePicker = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var formatValue = function formatValue(value) {
    var minHour = props.minHour,
        maxHour = props.maxHour,
        maxMinute = props.maxMinute,
        minMinute = props.minMinute;

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

    var _value$split = value.split(':'),
        hour = _value$split[0],
        minute = _value$split[1];

    hour = (0, _utils2.padZero)((0, _utils2.range)(hour, +minHour, +maxHour));
    minute = (0, _utils2.padZero)((0, _utils2.range)(minute, +minMinute, +maxMinute));
    return hour + ":" + minute;
  };

  var picker = (0, _react.useRef)(null);

  var _useState = (0, _react.useState)(function () {
    return formatValue(props.value);
  }),
      currentDate = _useState[0],
      setCurrentDate = _useState[1];

  var currentDateRef = (0, _react.useRef)();
  currentDateRef.current = currentDate;
  var ranges = (0, _react.useMemo)(function () {
    return [{
      type: 'hour',
      range: [+props.minHour, +props.maxHour]
    }, {
      type: 'minute',
      range: [+props.minMinute, +props.maxMinute]
    }];
  }, [props.minHour, props.maxHour, props.minMinute, props.maxMinute]);
  var originColumns = (0, _react.useMemo)(function () {
    return ranges.map(function (_ref) {
      var type = _ref.type,
          rangeArr = _ref.range;
      var values = (0, _utils.times)(rangeArr[1] - rangeArr[0] + 1, function (index) {
        return (0, _utils2.padZero)(rangeArr[0] + index);
      });

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

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

  var updateColumnValue = function updateColumnValue() {
    var pair = currentDateRef.current.split(':');
    var values = [props.formatter('hour', pair[0]), props.formatter('minute', pair[1])];
    setTimeout(function () {
      picker.current.setValues(values);
    }, 0);
  };

  var updateInnerValue = function updateInnerValue() {
    var _picker$current$getIn = picker.current.getIndexes(),
        hourIndex = _picker$current$getIn[0],
        minuteIndex = _picker$current$getIn[1];

    var hourColumn = originColumns[0],
        minuteColumn = originColumns[1];
    var hour = hourColumn.values[hourIndex] || hourColumn.values[0];
    var minute = minuteColumn.values[minuteIndex] || minuteColumn.values[0];
    setCurrentDate(formatValue(hour + ":" + minute));
    updateColumnValue();
  };

  var onConfirm = function onConfirm() {
    if (props.onConfirm) props.onConfirm(currentDateRef.current);
  };

  var onCancel = function onCancel() {
    if (props.onCancel) props.onCancel();
  };

  var onChange = function onChange() {
    updateInnerValue();

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

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

    if (value !== currentDate) {
      setCurrentDate(value);
      updateColumnValue();
    }
  }, [props.value]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getPicker: function getPicker() {
        return picker.current;
      }
    };
  });
  return /*#__PURE__*/_react.default.createElement(_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: function formatter(type, value) {
    return value;
  }
};
var _default = TimePicker;
exports.default = _default;