"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

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

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

var _classnames = _interopRequireDefault(require("classnames"));

var _popup = _interopRequireDefault(require("../popup"));

var _scrollbar = _interopRequireDefault(require("../scrollbar"));

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

var _excluded = ["children", "popupStyle", "timeData", "timeSetting", "visibleSetting"];

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var PICKERITEM_HEIGHT = 32;

var TimePickerSelector = function TimePickerSelector(props) {
  var children = props.children,
      popupStyle = props.popupStyle,
      _props$timeData = props.timeData,
      hours = _props$timeData.hours,
      minutes = _props$timeData.minutes,
      _props$timeSetting = props.timeSetting,
      hour = _props$timeSetting.hour,
      minute = _props$timeSetting.minute,
      setTime = _props$timeSetting.setTime,
      _props$visibleSetting = props.visibleSetting,
      visible = _props$visibleSetting.visible,
      setVisible = _props$visibleSetting.setVisible,
      restProps = _objectWithoutProperties(props, _excluded); // const pickerMenuRef = useRef<HTMLDivElement>() as React.RefObject<HTMLDivElement>;


  var pickerMenuItemActiveIndexRef = (0, _react.useRef)(0);
  var scrollbarRef = (0, _react.useRef)();
  var timerRef = (0, _react.useRef)();

  var onItemClickHandler = function onItemClickHandler(time) {
    // setHour(hour);
    // setMinute(minute);
    setTime(time);
    setVisible(false);
  };

  var renderContent = function renderContent() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(_utils.TimePickerClass, "-menu")
    }, /*#__PURE__*/_react["default"].createElement(_scrollbar["default"], {
      ref: scrollbarRef,
      style: {
        maxHeight: 192
      }
    }, /*#__PURE__*/_react["default"].createElement("p", {
      className: "".concat(_utils.TimePickerClass, "-menu-item--sticky top")
    }), hours.map(function (h, hIndex) {
      return minutes.map(function (m, mIndex) {
        var isCurrentTime = Number(h) === Number(hour) && Number(m) === Number(minute);
        var scrollToEightClock = Number(h) === Number(8) && Number(m) === Number(0);

        if (scrollToEightClock) {
          pickerMenuItemActiveIndexRef.current = hIndex + mIndex + 1 + hIndex * (minutes.length - 1);
        }

        var time = "".concat(h, ":").concat(m);
        return (
          /*#__PURE__*/
          // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
          _react["default"].createElement("p", {
            className: (0, _classnames["default"])("".concat(_utils.TimePickerClass, "-menu-item"), {
              active: isCurrentTime
            }),
            key: m,
            onClick: function onClick() {
              return onItemClickHandler(time);
            }
          }, time)
        );
      });
    }), /*#__PURE__*/_react["default"].createElement("p", {
      className: "".concat(_utils.TimePickerClass, "-menu-item--sticky bottom")
    })));
  };

  (0, _react.useEffect)(function () {
    timerRef.current = setTimeout(function () {
      if (visible && scrollbarRef.current) {
        scrollbarRef.current.scrollElement.scrollTop = pickerMenuItemActiveIndexRef.current * PICKERITEM_HEIGHT - PICKERITEM_HEIGHT; // 32: pickerItem height, 23: pickerMenu paddingTop + pickerItem paddingTop
      }
    }, 10);
    return function () {
      clearTimeout(timerRef.current);
    };
  }, [visible]);
  return /*#__PURE__*/_react["default"].createElement(_popup["default"], _extends({
    popup: renderContent(),
    popupVisible: visible,
    onPopupVisibleChange: setVisible,
    popupStyle: _objectSpread(_objectSpread({}, popupStyle), {}, {
      zIndex: 2000
    })
  }, restProps), children);
};

TimePickerSelector.defaultProps = {
  children: null,
  popupStyle: {},
  popupAlign: {
    offset: [0, 0],
    overflow: {
      adjustX: true,
      adjustY: true
    },
    targetOffset: [0, 0]
  }
};
var _default = TimePickerSelector;
exports["default"] = _default;