"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SelectTime = void 0;
var _react = _interopRequireDefault(require("react"));
var _moment = _interopRequireDefault(require("moment"));
var _timeUtils = require("../../utils/timeUtils");
var _Select = require("../Select/Select");
var _SelectTrigger = require("../SelectTrigger");
var _excluded = ["mode", "selected", "min", "max", "onChange", "target", "triggerMode", "step"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _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 renderOption = function renderOption(option) {
  return /*#__PURE__*/_react.default.createElement("div", {
    id: option.id
  }, option.name);
};
var SelectTime = exports.SelectTime = function SelectTime(_ref) {
  var _ref$mode = _ref.mode,
    mode = _ref$mode === void 0 ? "24" : _ref$mode,
    selected = _ref.selected,
    _ref$min = _ref.min,
    min = _ref$min === void 0 ? "00:00" : _ref$min,
    _ref$max = _ref.max,
    max = _ref$max === void 0 ? "23:45" : _ref$max,
    onChange = _ref.onChange,
    target = _ref.target,
    triggerMode = _ref.triggerMode,
    _ref$step = _ref.step,
    step = _ref$step === void 0 ? 15 : _ref$step,
    rest = _objectWithoutProperties(_ref, _excluded);
  var generateTimeOptions = function generateTimeOptions() {
    var minTime = (0, _moment.default)((0, _timeUtils.isValidTime)(min) ? min : "00:00", "HH:mm");
    var maxTime = (0, _moment.default)((0, _timeUtils.isValidTime)(max) ? max : "23:45", "HH:mm");
    var options = [];
    var currentTime = minTime.clone();
    while (currentTime.isSameOrBefore(maxTime)) {
      options.push({
        id: currentTime.format("HH:mm"),
        name: currentTime.format(mode === "12" ? "hh:mm A" : "HH:mm")
      });
      currentTime.add(step, "minutes");
    }
    return options;
  };
  var timeOptions = generateTimeOptions();
  var displaySelectedTime = (0, _moment.default)(selected, "HH:mm").format(mode === "12" ? "hh:mm A" : "HH:mm");
  var handleScrollToSelected = function handleScrollToSelected() {
    if (selected) {
      setTimeout(function () {
        var _document$getElementB;
        var selectedItem = (_document$getElementB = document.getElementById(selected)) === null || _document$getElementB === void 0 ? void 0 : _document$getElementB.parentElement;
        selectedItem === null || selectedItem === void 0 || selectedItem.scrollIntoView({
          behavior: "smooth",
          block: "center"
        });
      }, 50);
    }
  };
  return /*#__PURE__*/_react.default.createElement(_Select.Select, {
    options: timeOptions,
    disabledInternalSort: true,
    selected: selected,
    disableSearch: true,
    keepSameOptionsOrder: true,
    target: target ? target : /*#__PURE__*/_react.default.createElement(_SelectTrigger.SelectTrigger, _extends({
      mode: triggerMode
    }, rest), displaySelectedTime),
    mode: "tiny",
    renderOption: renderOption,
    onChange: onChange,
    forceCloseMenu: true,
    onSelectOpen: handleScrollToSelected
  });
};
//# sourceMappingURL=SelectTime.js.map