"use strict";

exports.__esModule = true;
exports.default = exports.MinsSelect = exports.HoursSelect = exports.AMPMSelect = exports.parseTime = void 0;

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

var _styles = require("@material-ui/core/styles");

var _Select = _interopRequireDefault(require("./Select"));

var _SelectProps = _interopRequireDefault(require("./SelectProps"));

var _timezoneHelper = require("../../../utils/timezoneHelper");

var _metadata = require("../../../selectors/metadata");

var _reactRedux = require("react-redux");

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 () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};

var useStyles = (0, _styles.makeStyles)(function (theme) {
  return {
    timeWrapper: {
      margin: "auto",
      display: "flex",
      flexDirection: "column",
      alignItems: "center",
      textAlign: "center"
    },
    timeZoneWrapper: {
      marginLeft: theme.spacing(0.5),
      fontFamily: theme.fontFamily,
      fontSize: theme.fontSize
    },
    timePickerWrapper: {
      display: "flex",
      flexWrap: "wrap",
      alignItems: "center",
      paddingBottom: theme.spacing(2)
    },
    timePickerSegmentWrapper: {
      fontFamily: theme.fontFamily,
      fontSize: theme.fontSize,
      color: "#333333",
      marginLeft: theme.spacing(0.5),
      marginRight: theme.spacing(0.5),
      borderRadius: theme.shape.borderRadius,
      "&:first-child": {
        marginLeft: 0
      },
      "& .MuiInputBase-root": {
        "& .MuiSelect-select": {
          minWidth: theme.spacing(3)
        }
      }
    }
  };
});
var hourOptionsAMPM = [];

for (var i = 1; i <= 12; i++) {
  hourOptionsAMPM.push({
    value: i,
    label: i
  });
}

var hourOptions = [];

for (var j = 0; j <= 23; j++) {
  hourOptions.push({
    value: j,
    label: j
  });
}

var minOptions = [{
  value: "00",
  label: "00"
}, {
  value: "15",
  label: "15"
}, {
  value: "30",
  label: "30"
}, {
  value: "45",
  label: "45"
}];
var ampmOptions = [{
  value: "AM",
  label: "AM"
}, {
  value: "PM",
  label: "PM"
}];

var isBrowserUsingAMPM = function isBrowserUsingAMPM() {
  return !!new Date(Date.UTC(2020, 7, 30, 3, 0, 0)).toLocaleTimeString().match(/am|a.m|pm|p.m/i);
};

var parseTime = function parseTime(timeStr) {
  var time = timeStr.match(/(\d+)(?::(\d\d))?\s*(p?)/i);

  if (!time) {
    return new Date();
  }

  var hours = parseInt(time[1]);

  if (time[3] === "p" && hours < 12) {
    hours += 12;
  }

  return new Date(0, 0, 0, hours, parseInt(time[2]) || 0, 0);
};

exports.parseTime = parseTime;

var leftFillNum = function leftFillNum(num, targetLength) {
  return num.toString().padStart(targetLength, 0);
};

var isAM = function isAM(time) {
  return time.getHours() < 12;
};

var calculateHours = function calculateHours(time, showAMPM) {
  if (!showAMPM) return time.getHours();
  return time.getHours() === 0 || time.getHours() === 12 ? 12 : time.getHours() % 12;
};

var calculateMins = function calculateMins(time) {
  return time.getMinutes() >= 45 ? "45" : time.getMinutes() >= 30 ? "30" : time.getMinutes() >= 15 ? "15" : "00";
};

var calculateAMPM = function calculateAMPM(time) {
  return isAM(time) ? "AM" : "PM";
};

var AMPMSelect = function AMPMSelect(_ref) {
  var showAMPM = _ref.showAMPM,
      updateTimeOptions = _ref.updateTimeOptions,
      time = _ref.time;
  var classes = useStyles();
  if (!showAMPM) return null;
  var selectProps = new _SelectProps.default();
  selectProps.set(_SelectProps.default.propNames.update, updateTimeOptions("ampm"));
  selectProps.set(_SelectProps.default.propNames.value, calculateAMPM(time));
  selectProps.set(_SelectProps.default.propNames.native, true);
  selectProps.set(_SelectProps.default.propNames.inputProps, {
    name: "ampm",
    id: "ampm"
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.timePickerSegmentWrapper
  }, /*#__PURE__*/_react.default.createElement(_Select.default, {
    selectProps: selectProps
  }, ampmOptions.map(function (option) {
    return /*#__PURE__*/_react.default.createElement("option", {
      key: "ampm" + option.value,
      value: option.value
    }, option.label);
  })));
};

exports.AMPMSelect = AMPMSelect;

__signature__(AMPMSelect, "useStyles{classes}", function () {
  return [useStyles];
});

__signature__(AMPMSelect, "useStyles{classes}", function () {
  return [useStyles];
});

var HoursSelect = function HoursSelect(_ref2) {
  var updateTimeOptions = _ref2.updateTimeOptions,
      time = _ref2.time,
      showAMPM = _ref2.showAMPM;
  var classes = useStyles();
  var selectProps = new _SelectProps.default();
  var properHourOptions = showAMPM ? hourOptionsAMPM : hourOptions;
  selectProps.set(_SelectProps.default.propNames.update, updateTimeOptions("hours"));
  selectProps.set(_SelectProps.default.propNames.value, calculateHours(time, showAMPM));
  selectProps.set(_SelectProps.default.propNames.native, true);
  selectProps.set(_SelectProps.default.propNames.inputProps, {
    name: "hours",
    id: "hours"
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.timePickerSegmentWrapper
  }, /*#__PURE__*/_react.default.createElement(_Select.default, {
    selectProps: selectProps
  }, properHourOptions.map(function (option) {
    return /*#__PURE__*/_react.default.createElement("option", {
      key: "hour" + option.value,
      value: option.value
    }, option.label);
  })));
};

exports.HoursSelect = HoursSelect;

__signature__(HoursSelect, "useStyles{classes}", function () {
  return [useStyles];
});

__signature__(HoursSelect, "useStyles{classes}", function () {
  return [useStyles];
});

var MinsSelect = function MinsSelect(_ref3) {
  var updateTimeOptions = _ref3.updateTimeOptions,
      time = _ref3.time;
  var classes = useStyles();
  var selectProps = new _SelectProps.default();
  selectProps.set(_SelectProps.default.propNames.update, updateTimeOptions("mins"));
  selectProps.set(_SelectProps.default.propNames.value, calculateMins(time));
  selectProps.set(_SelectProps.default.propNames.native, true);
  selectProps.set(_SelectProps.default.propNames.inputProps, {
    name: "mins",
    id: "mins"
  });
  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.timePickerSegmentWrapper
  }, /*#__PURE__*/_react.default.createElement(_Select.default, {
    selectProps: selectProps
  }, minOptions.map(function (option) {
    return /*#__PURE__*/_react.default.createElement("option", {
      key: "min" + option.value,
      value: option.value
    }, option.label);
  })));
};

exports.MinsSelect = MinsSelect;

__signature__(MinsSelect, "useStyles{classes}", function () {
  return [useStyles];
});

__signature__(MinsSelect, "useStyles{classes}", function () {
  return [useStyles];
});

var TimePicker = function TimePicker(_ref4) {
  var _showAMPM;

  var value = _ref4.value,
      onChange = _ref4.onChange,
      showTimeZone = _ref4.showTimeZone,
      showAMPM = _ref4.showAMPM,
      requestedTimeZone = _ref4.requestedTimeZone;
  var classes = useStyles();
  showAMPM = (_showAMPM = showAMPM) != null ? _showAMPM : isBrowserUsingAMPM();

  var _useState = (0, _react.useState)(parseTime(value || "00:00")),
      _useState2 = _slicedToArray(_useState, 2),
      time = _useState2[0],
      setTime = _useState2[1];

  var userTimeZone = (0, _timezoneHelper.getTimeZoneName)();
  var localizedTimeZoneName = (0, _reactRedux.useSelector)((0, _metadata.namedLookupLocalizedSelector)("customer", "TimeZone", userTimeZone, null));

  var onTimeChange = function onTimeChange(datetime) {
    if (onChange) {
      // DatePicker expects 24 hour time format, or else things go wonky!
      var _time = "".concat(leftFillNum(datetime.getHours(), 2), ":").concat(leftFillNum(datetime.getMinutes(), 2));

      onChange(_time);
    }
  };

  var setHours = function setHours(hours, isAMTime) {
    if (!showAMPM) {
      time.setHours(hours);
    } else if (parseInt(hours) === 12) {
      time.setHours(isAMTime ? 0 : 12);
    } else {
      time.setHours(parseInt(hours) + parseInt(isAMTime ? 0 : 12));
    }
  };

  var updateTimeOptions = function updateTimeOptions(id) {
    return function (newValue) {
      var value = newValue;

      if (id === "hours") {
        setHours(value, isAM(time));
      } else if (id === "ampm") {
        setHours(calculateHours(time, showAMPM), value === "AM");
      } else {
        time.setMinutes(parseInt(value));
      }

      setTime(time);
      onTimeChange(time);
    };
  };

  var getTimeZone = function getTimeZone(requestedTimeZone) {
    if (requestedTimeZone) return requestedTimeZone;

    if (!localizedTimeZoneName) {
      var timezone = new Date().toString().match(/GMT(\S+) \(([^)]+)\)/i);
      return "".concat(timezone[2], " (GMT").concat(timezone[1], ")");
    }

    return localizedTimeZoneName;
  };

  return /*#__PURE__*/_react.default.createElement("div", {
    className: classes.timeWrapper
  }, /*#__PURE__*/_react.default.createElement("span", {
    className: classes.timePickerWrapper
  }, /*#__PURE__*/_react.default.createElement(HoursSelect, {
    updateTimeOptions: updateTimeOptions,
    time: time,
    showAMPM: showAMPM
  }), /*#__PURE__*/_react.default.createElement("label", null, " : "), /*#__PURE__*/_react.default.createElement(MinsSelect, {
    updateTimeOptions: updateTimeOptions,
    time: time
  }), /*#__PURE__*/_react.default.createElement(AMPMSelect, {
    showAMPM: showAMPM,
    updateTimeOptions: updateTimeOptions,
    time: time
  })), showTimeZone && /*#__PURE__*/_react.default.createElement("label", {
    className: classes.timeZoneWrapper
  }, showTimeZone && getTimeZone(requestedTimeZone)));
};

__signature__(TimePicker, "useStyles{classes}\nuseState{[time, setTime](parseTime(value || \"00:00\"))}\nuseSelector{localizedTimeZoneName}", function () {
  return [useStyles, _reactRedux.useSelector];
});

__signature__(TimePicker, "useStyles{classes}\nuseState{[time, setTime](parseTime(value || \"00:00\"))}\nuseSelector{localizedTimeZoneName}", function () {
  return [useStyles, _reactRedux.useSelector];
});

var _default = TimePicker;
var _default2 = _default;
var _default3 = _default2;
exports.default = _default3;
;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(hourOptionsAMPM, "hourOptionsAMPM", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(i, "i", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(hourOptions, "hourOptions", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(j, "j", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(minOptions, "minOptions", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(ampmOptions, "ampmOptions", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(isBrowserUsingAMPM, "isBrowserUsingAMPM", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(parseTime, "parseTime", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(leftFillNum, "leftFillNum", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(isAM, "isAM", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(calculateHours, "calculateHours", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(calculateMins, "calculateMins", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(calculateAMPM, "calculateAMPM", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(AMPMSelect, "AMPMSelect", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(HoursSelect, "HoursSelect", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(MinsSelect, "MinsSelect", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(TimePicker, "TimePicker", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(_default, "default", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();

;

(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;

  if (!reactHotLoader) {
    return;
  }

  reactHotLoader.register(useStyles, "useStyles", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(hourOptionsAMPM, "hourOptionsAMPM", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(i, "i", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(hourOptions, "hourOptions", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(j, "j", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(minOptions, "minOptions", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(ampmOptions, "ampmOptions", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(isBrowserUsingAMPM, "isBrowserUsingAMPM", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(parseTime, "parseTime", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(leftFillNum, "leftFillNum", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(isAM, "isAM", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(calculateHours, "calculateHours", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(calculateMins, "calculateMins", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(calculateAMPM, "calculateAMPM", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(AMPMSelect, "AMPMSelect", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(HoursSelect, "HoursSelect", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(MinsSelect, "MinsSelect", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(TimePicker, "TimePicker", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
  reactHotLoader.register(_default2, "default", "/home/vsts/work/1/s/src/components/MaterialUI/Inputs/TimePicker.js");
})();

;

(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();