"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.TimePickerBase = void 0;
var _kind = _interopRequireDefault(require("@enact/core/kind"));
var _react = require("react");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _$L = _interopRequireDefault(require("../internal/$L"));
var _DateComponentPicker = require("../internal/DateComponentPicker");
var _DateTime = _interopRequireDefault(require("../internal/DateTime"));
var _TimePickerModule = _interopRequireDefault(require("./TimePicker.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["hasMeridiem"],
  _excluded2 = ["disabled", "hasMeridiem", "hour", "hourAriaLabel", "meridiem", "meridiemAriaLabel", "meridiemPickerWidth", "meridiems", "minute", "minuteAriaLabel", "onHourChange", "onMeridiemChange", "onMinuteChange", "onSpotlightDisappear", "order", "spotlightDisabled"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } // values to use in hour picker for 24 and 12 hour locales
var hours24 = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23'];
var hours12 = ['12', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'];

/**
 * {@link agate/TimePicker/TimePickerBase.HourPicker} is a utility component to prevent the
 * animation of the picker when the display text doesn't change for 12-hour locales.
 *
 * @class HourPicker
 * @memberof agate/TimePicker/TimePickerBase
 * @ui
 * @private
 */
var HourPicker = /*#__PURE__*/function (_Component) {
  function HourPicker(props) {
    var _this;
    _classCallCheck(this, HourPicker);
    _this = _callSuper(this, HourPicker, [props]);
    _this.state = {
      noAnimation: false,
      prevValue: props.value
    };
    return _this;
  }
  _inherits(HourPicker, _Component);
  return _createClass(HourPicker, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
        hasMeridiem = _this$props.hasMeridiem,
        rest = _objectWithoutProperties(_this$props, _excluded);
      var hours = hasMeridiem ? hours12 : hours24;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DateComponentPicker.DateComponentPicker, _objectSpread(_objectSpread({}, rest), {}, {
        noAnimation: this.state.noAnimation,
        children: hours
      }));
    }
  }], [{
    key: "getDerivedStateFromProps",
    value: function getDerivedStateFromProps(props, state) {
      if (state.prevValue !== props.value) {
        var hours = props.hasMeridiem ? hours12 : hours24;
        return {
          noAnimation: hours[state.prevValue] === hours[props.value],
          prevValue: props.value
        };
      }
      return null;
    }
  }]);
}(_react.Component);
/**
 * {@link agate/TimePicker.TimePickerBase|TimePickerBase} is the stateless functional time picker
 * component. Should not be used directly but may be composed within another component as it is
 * within {@link agate/TimePicker.TimePicker|TimePicker}.
 *
 * @class TimePickerBase
 * @memberof agate/TimePicker
 * @ui
 * @public
 */
HourPicker.propTypes = {
  hasMeridiem: _propTypes["default"].bool,
  value: _propTypes["default"].number
};
var TimePickerBase = exports.TimePickerBase = (0, _kind["default"])({
  name: 'TimePickerBase',
  propTypes: /** @lends agate/TimePicker.TimePickerBase.prototype */{
    /**
     * The `hour` component of the time.
     *
     * @type {Number}
     * @required
     * @public
     */
    hour: _propTypes["default"].number.isRequired,
    /**
     * The `meridiem` component of the time.
     *
     * @type {Number}
     * @required
     * @public
     */
    meridiem: _propTypes["default"].number.isRequired,
    /**
     * The `minute` component of the time.
     *
     * @type {Number}
     * @required
     * @public
     */
    minute: _propTypes["default"].number.isRequired,
    /**
     * The order in which the component pickers are displayed.
     *
     * Should be an array of 2 or 3 strings containing one of `'h'`, `'k'`, `'m'`, and `'a'`.
     *
     * @type {String[]}
     * @required
     * @public
     */
    order: _propTypes["default"].arrayOf(_propTypes["default"].oneOf(['h', 'k', 'm', 'a'])).isRequired,
    /**
     * Disables the `TimePicker`.
     *
     * @type {Boolean}
     * @default false
     * @public
     */
    disabled: _propTypes["default"].bool,
    /**
     * The "aria-label" for the hour picker
     *
     * If not specified, the "aria-label" for the hour picker will be
     * a combination of the current value and 'hour change a value with up down button'.
     *
     * @type {String}
     * @public
     */
    hourAriaLabel: _propTypes["default"].string,
    /**
     * The "aria-label" for the meridiem picker.
     *
     * If not specified, the "aria-label" for the meridiem picker will be
     * a combination of the current value and 'change a value with up down button'.
     *
     * @type {String}
     * @public
     */
    meridiemAriaLabel: _propTypes["default"].string,
    /**
     * Array of meridiem labels to display.
     *
     * @type {String[]}
     * @required
     * @public
     */
    meridiems: _propTypes["default"].arrayOf(_propTypes["default"].string),
    /**
     * The "aria-label" for the minute picker.
     *
     * If not specified, the "aria-label" for the minute picker will be
     * a combination of the current value and 'minute change a value with up down button'.
     *
     * @type {String}
     * @public
     */
    minuteAriaLabel: _propTypes["default"].string,
    /**
     * Called on changes in the `hour` component of the time.
     *
     * @type {Function}
     * @public
     */
    onHourChange: _propTypes["default"].func,
    /**
     * Called on changes in the `meridiem` component of the time.
     *
     * @type {Function}
     * @public
     */
    onMeridiemChange: _propTypes["default"].func,
    /**
     * Called on changes in the `minute` component of the time.
     *
     * @type {Function}
     * @public
     */
    onMinuteChange: _propTypes["default"].func,
    /**
     * Called when the component is removed while retaining focus.
     *
     * @type {Function}
     * @param {Object} event
     * @public
     */
    onSpotlightDisappear: _propTypes["default"].func,
    /**
     * Set content to RTL.
     *
     * @type {Boolean}
     * @private
     */
    rtl: _propTypes["default"].bool,
    /**
     * Disables 5-way spotlight from navigating into the component.
     *
     * @type {Boolean}
     * @public
     */
    spotlightDisabled: _propTypes["default"].bool
  },
  defaultProps: {
    disabled: false
  },
  styles: {
    css: _TimePickerModule["default"],
    className: 'timePicker',
    publicClassNames: true
  },
  computed: {
    hasMeridiem: function hasMeridiem(_ref) {
      var order = _ref.order;
      return order && order.indexOf('a') >= 0;
    },
    meridiemPickerWidth: function meridiemPickerWidth(_ref2) {
      var meridiem = _ref2.meridiem,
        meridiems = _ref2.meridiems;
      return meridiems && meridiems[meridiem].length * 1.5;
    }
  },
  render: function render(_ref3) {
    var disabled = _ref3.disabled,
      hasMeridiem = _ref3.hasMeridiem,
      hour = _ref3.hour,
      hourAriaLabel = _ref3.hourAriaLabel,
      meridiem = _ref3.meridiem,
      meridiemAriaLabel = _ref3.meridiemAriaLabel,
      meridiemPickerWidth = _ref3.meridiemPickerWidth,
      meridiems = _ref3.meridiems,
      minute = _ref3.minute,
      minuteAriaLabel = _ref3.minuteAriaLabel,
      onHourChange = _ref3.onHourChange,
      onMeridiemChange = _ref3.onMeridiemChange,
      onMinuteChange = _ref3.onMinuteChange,
      onSpotlightDisappear = _ref3.onSpotlightDisappear,
      order = _ref3.order,
      spotlightDisabled = _ref3.spotlightDisabled,
      rest = _objectWithoutProperties(_ref3, _excluded2);
    var hourAccessibilityHint = (0, _$L["default"])('hour');
    var minuteAccessibilityHint = (0, _$L["default"])('minute');
    delete rest.rtl;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DateTime["default"], _objectSpread(_objectSpread({}, rest), {}, {
      css: _TimePickerModule["default"],
      disabled: disabled,
      children: order && order.map(function (picker) {
        switch (picker) {
          case 'h':
          case 'k':
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(_react.Fragment, {
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(HourPicker, {
                accessibilityHint: hourAccessibilityHint,
                "aria-label": hourAriaLabel,
                className: _TimePickerModule["default"].hourPicker,
                disabled: disabled,
                hasMeridiem: hasMeridiem,
                onChange: onHourChange,
                onSpotlightDisappear: onSpotlightDisappear,
                spotlightDisabled: spotlightDisabled,
                value: hour,
                width: 2,
                wrap: true
              })
            }, "hour-picker");
          case 'm':
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DateComponentPicker.DateComponentRangePicker, {
              accessibilityHint: minuteAccessibilityHint,
              "aria-label": minuteAriaLabel,
              className: _TimePickerModule["default"].minutePicker,
              disabled: disabled,
              max: 59,
              min: 0,
              onChange: onMinuteChange,
              onSpotlightDisappear: onSpotlightDisappear,
              spotlightDisabled: spotlightDisabled,
              value: minute,
              width: 2,
              wrap: true
            }, "minute-picker");
          case 'a':
            return /*#__PURE__*/(0, _jsxRuntime.jsx)(_DateComponentPicker.DateComponentPicker, {
              "aria-label": meridiemAriaLabel,
              "aria-valuetext": meridiems ? meridiems[meridiem] : null,
              className: _TimePickerModule["default"].meridiemPicker,
              disabled: disabled,
              onChange: onMeridiemChange,
              onSpotlightDisappear: onSpotlightDisappear,
              spotlightDisabled: spotlightDisabled,
              value: meridiem,
              width: meridiemPickerWidth,
              reverseTransition: null,
              children: meridiems
            }, "meridiem-picker");
        }
        return null;
      })
    }));
  }
});
var _default = exports["default"] = TimePickerBase;