"use strict";

Liferay.Loader.define("@frontend-taglib-clay$clayui/time-picker@3.58.0/lib/index", ['module', 'exports', 'require', '@frontend-taglib-clay$clayui/button', '@frontend-taglib-clay$clayui/form', '@frontend-taglib-clay$clayui/icon', '@frontend-taglib-clay$clayui/shared', 'frontend-taglib-clay$classnames', 'frontend-js-react-web$react'], function (module, exports, require) {
  var define = undefined;
  var global = window;
  {
    function _typeof(obj) {
      "@babel/helpers - typeof";
      if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
        _typeof = function _typeof(obj) {
          return typeof obj;
        };
      } else {
        _typeof = function _typeof(obj) {
          return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
        };
      }return _typeof(obj);
    }

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

    var _button = _interopRequireDefault(require("@frontend-taglib-clay$clayui/button"));

    var _form = require("@frontend-taglib-clay$clayui/form");

    var _icon = _interopRequireDefault(require("@frontend-taglib-clay$clayui/icon"));

    var _shared = require("@frontend-taglib-clay$clayui/shared");

    var _classnames = _interopRequireDefault(require("frontend-taglib-clay$classnames"));

    var _react = _interopRequireWildcard(require("frontend-js-react-web$react"));

    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 _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
    }

    function ownKeys(object, enumerableOnly) {
      var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {
        var symbols = Object.getOwnPropertySymbols(object);if (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 = arguments[i] != null ? arguments[i] : {};if (i % 2) {
          ownKeys(Object(source), true).forEach(function (key) {
            _defineProperty(target, key, source[key]);
          });
        } else if (Object.getOwnPropertyDescriptors) {
          Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
        } else {
          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 _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 TimeType;

    (function (TimeType) {
      TimeType["minutes"] = "minutes";
      TimeType["hours"] = "hours";
      TimeType["ampm"] = "ampm";
    })(TimeType || (TimeType = {}));

    var DEFAULT_VALUE = '--';
    var DEFAULT_CONFIG = {
      use12Hours: {
        ampm: {
          am: 'AM',
          pm: 'PM'
        },
        hours: {
          max: 12,
          min: 1
        },
        minutes: {
          max: 59,
          min: 0
        }
      },
      use24Hours: {
        ampm: {
          am: 'AM',
          pm: 'PM'
        },
        hours: {
          max: 23,
          min: 0
        },
        minutes: {
          max: 59,
          min: 0
        }
      }
    };
    var regex = /^\d+$/;

    var ClayTimePicker = function ClayTimePicker(_ref) {
      var _ref$ariaLabels = _ref.ariaLabels,
          ariaLabels = _ref$ariaLabels === void 0 ? {
        ampm: 'Select time of day (AM/PM) using up (PM) and down (AM) arrow keys',
        clear: 'Delete the entered time',
        hours: 'Enter the hour in 00:00 format',
        minutes: 'Enter the minutes in 00:00 format',
        timeDown: 'Time down',
        timeUp: 'Time up'
      } : _ref$ariaLabels,
          _ref$config = _ref.config,
          config = _ref$config === void 0 ? DEFAULT_CONFIG : _ref$config,
          _ref$disabled = _ref.disabled,
          disabled = _ref$disabled === void 0 ? false : _ref$disabled,
          _ref$defaultValue = _ref.defaultValue,
          defaultValue = _ref$defaultValue === void 0 ? {
        hours: DEFAULT_VALUE,
        minutes: DEFAULT_VALUE
      } : _ref$defaultValue,
          _ref$icon = _ref.icon,
          icon = _ref$icon === void 0 ? false : _ref$icon,
          name = _ref.name,
          onChange = _ref.onChange,
          onInputChange = _ref.onInputChange,
          spritemap = _ref.spritemap,
          timezone = _ref.timezone,
          _ref$use12Hours = _ref.use12Hours,
          use12Hours = _ref$use12Hours === void 0 ? false : _ref$use12Hours,
          value = _ref.value,
          values = _ref.values;

      var _useInternalState = (0, _shared.useInternalState)({
        defaultName: 'defaultValue',
        defaultValue: defaultValue,
        handleName: 'onChange',
        name: 'value',
        onChange: onChange !== null && onChange !== void 0 ? onChange : onInputChange,
        value: value !== null && value !== void 0 ? value : values
      }),
          _useInternalState2 = _slicedToArray(_useInternalState, 2),
          internalValue = _useInternalState2[0],
          setValue = _useInternalState2[1];

      var useConfig = config[use12Hours ? 'use12Hours' : 'use24Hours'];

      var _useState = (0, _react.useState)(false),
          _useState2 = _slicedToArray(_useState, 2),
          actionVisible = _useState2[0],
          setActionVisible = _useState2[1];

      var _useState3 = (0, _react.useState)(false),
          _useState4 = _slicedToArray(_useState3, 2),
          isFocused = _useState4[0],
          setIsFocused = _useState4[1];

      var elementRef = (0, _react.useRef)(null);
      var defaultFocused = {
        configName: TimeType.hours,
        focused: false
      };

      var _useState5 = (0, _react.useState)(defaultFocused),
          _useState6 = _slicedToArray(_useState5, 2),
          currentInputFocused = _useState6[0],
          setCurrentInputFocused = _useState6[1];

      var handleMaxAndMin = function handleMaxAndMin(value, config) {
        var newValue = value.substring(value.length - 2, value.length);
        var intrinsicValue = Number(newValue);

        if (intrinsicValue > config.max) {
          return String(config.min);
        } else if (intrinsicValue < config.min) {
          return String(config.max);
        }

        return newValue;
      };

      var handleKeyDown = function handleKeyDown(event, value, configName) {
        var config = useConfig[configName];
        var intrinsicValue = Number(value);

        var onValue = function onValue(newValue) {
          var newVal = configName === TimeType.ampm ? newValue : handleMaxAndMin(String(newValue), config);
          return setValue(_objectSpread(_objectSpread({}, internalValue), {}, _defineProperty({}, configName, String(newVal).padStart(2, '0'))));
        };

        switch (event.key) {
          case 'Backspace':
            onValue(DEFAULT_VALUE);
            break;

          case 'ArrowUp':
            event.preventDefault();
            event.stopPropagation();

            if (configName === TimeType.ampm) {
              onValue(config.pm);
            } else {
              onValue(value !== DEFAULT_VALUE ? intrinsicValue + 1 : config.min);
            }

            break;

          case 'ArrowDown':
            event.preventDefault();
            event.stopPropagation();

            if (configName === TimeType.ampm) {
              onValue(config.am);
            } else {
              onValue(value !== DEFAULT_VALUE ? intrinsicValue - 1 : config.max);
            }

            break;

          default:
            if (regex.test(event.key) && configName !== TimeType.ampm) {
              var maxSecondDigit = Math.floor(config.max / 10);
              var minFirstDigit = config.min;
              var keyVal = intrinsicValue < minFirstDigit ? minFirstDigit : event.key;
              var newVal = Number(value) > maxSecondDigit ? "0".concat(keyVal) : (value && value !== DEFAULT_VALUE ? value : '') + keyVal;
              onValue(newVal);
            } else if (configName === TimeType.ampm && (event.key === 'a' || event.key === 'p')) {
              onValue(event.key === 'a' ? config.am : config.pm);
            }

            break;
        }
      };

      var handleAction = function handleAction(direction) {
        var configName = currentInputFocused.configName;
        var config = useConfig[configName];
        var prevValue = internalValue[configName];
        var value;

        if (configName === TimeType.ampm) {
          value = direction === 1 ? config.pm : config.am;
        } else {
          value = handleMaxAndMin(String(prevValue !== DEFAULT_VALUE ? Number(prevValue) + direction : direction === -1 ? config.max : 0), config);
        }

        setCurrentInputFocused({
          configName: configName,
          focused: true
        });
        setValue(_objectSpread(_objectSpread({}, internalValue), {}, _defineProperty({}, configName, String(value).padStart(2, '0'))));
      };

      var handleDocumentClick = function handleDocumentClick(event) {
        if (elementRef.current && event.target !== null && !elementRef.current.contains(event.target)) {
          setActionVisible(false);
          setCurrentInputFocused(defaultFocused);
          setIsFocused(false);
        }
      };

      var handleInputFocus = function handleInputFocus(configName) {
        setActionVisible(true);
        setCurrentInputFocused({
          configName: configName,
          focused: true
        });
        setIsFocused(true);
      };

      (0, _react.useEffect)(function () {
        document.addEventListener('click', handleDocumentClick);
        return function () {
          document.removeEventListener('click', handleDocumentClick);
        };
      }, []);
      var visibleActionReset = actionVisible && (internalValue.ampm !== DEFAULT_VALUE && internalValue.ampm != null || internalValue.hours !== DEFAULT_VALUE || internalValue.minutes !== DEFAULT_VALUE);
      return (/*#__PURE__*/_react.default.createElement("div", {
          className: "clay-time"
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput.Group, null, icon && /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, {
          shrink: true
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupText, null, /*#__PURE__*/_react.default.createElement(_icon.default, {
          spritemap: spritemap,
          symbol: "time"
        }))), /*#__PURE__*/_react.default.createElement(_shared.FocusScope, {
          arrowKeysLeftRight: true,
          arrowKeysUpDown: false
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, {
          shrink: true
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: (0, _classnames.default)('form-control', {
            disabled: disabled,
            focus: isFocused
          }),
          onMouseEnter: function onMouseEnter() {
            if (!disabled) {
              setActionVisible(true);
            }
          },
          onMouseLeave: function onMouseLeave() {
            if (!currentInputFocused.focused && !disabled) {
              setActionVisible(false);
            }
          },
          ref: elementRef
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "clay-time-edit"
        }, /*#__PURE__*/_react.default.createElement("input", {
          "aria-label": ariaLabels.hours,
          className: (0, _classnames.default)('clay-time-hours form-control-inset', {
            focus: currentInputFocused.configName === TimeType.hours && currentInputFocused.focused
          }),
          disabled: disabled,
          maxLength: 2,
          onFocus: function onFocus() {
            return handleInputFocus(TimeType.hours);
          },
          onKeyDown: function onKeyDown(event) {
            return handleKeyDown(event, internalValue.hours, TimeType.hours);
          },
          readOnly: true,
          type: "text",
          value: internalValue.hours
        }), /*#__PURE__*/_react.default.createElement("span", {
          className: "clay-time-divider"
        }, ":"), /*#__PURE__*/_react.default.createElement("input", {
          "aria-label": ariaLabels.minutes,
          className: (0, _classnames.default)('clay-time-minutes form-control-inset', {
            focus: currentInputFocused.configName === TimeType.minutes
          }),
          disabled: disabled,
          maxLength: 2,
          onFocus: function onFocus() {
            return handleInputFocus(TimeType.minutes);
          },
          onKeyDown: function onKeyDown(event) {
            return handleKeyDown(event, internalValue.minutes, TimeType.minutes);
          },
          readOnly: true,
          type: "text",
          value: internalValue.minutes
        }), use12Hours && /*#__PURE__*/_react.default.createElement("input", {
          "aria-label": ariaLabels.ampm,
          className: (0, _classnames.default)('clay-time-ampm form-control-inset', {
            focus: currentInputFocused.configName === TimeType.ampm
          }),
          disabled: disabled,
          "max-length": "2",
          onFocus: function onFocus() {
            return handleInputFocus(TimeType.ampm);
          },
          onKeyDown: function onKeyDown(event) {
            return handleKeyDown(event, internalValue.ampm, TimeType.ampm);
          },
          readOnly: true,
          type: "text",
          value: internalValue.ampm || DEFAULT_VALUE
        }), name && /*#__PURE__*/_react.default.createElement("input", {
          name: name,
          type: "hidden",
          value: "".concat(internalValue.hours, ":").concat(internalValue.minutes)
        })), /*#__PURE__*/_react.default.createElement("div", {
          className: "clay-time-action-group"
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "clay-time-action-group-item",
          style: {
            opacity: visibleActionReset ? 1 : 0,
            pointerEvents: visibleActionReset ? 'auto' : 'none'
          }
        }, /*#__PURE__*/_react.default.createElement(_button.default, {
          "aria-label": ariaLabels.clear,
          className: "clay-time-clear-btn",
          disabled: disabled,
          displayType: "unstyled",
          onClick: function onClick() {
            return setValue(use12Hours ? {
              ampm: DEFAULT_VALUE,
              hours: DEFAULT_VALUE,
              minutes: DEFAULT_VALUE
            } : {
              hours: DEFAULT_VALUE,
              minutes: DEFAULT_VALUE
            });
          }
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          spritemap: spritemap,
          symbol: "times-circle"
        }))), /*#__PURE__*/_react.default.createElement("div", {
          className: "clay-time-action-group-item",
          style: {
            opacity: actionVisible ? 1 : 0
          }
        }, /*#__PURE__*/_react.default.createElement("div", {
          className: "btn-group-vertical clay-time-inner-spin",
          role: "group"
        }, /*#__PURE__*/_react.default.createElement(_button.default, {
          "aria-label": ariaLabels.timeUp,
          className: "clay-time-inner-spin-btn clay-time-inner-spin-btn-inc",
          disabled: disabled,
          displayType: "secondary",
          onClick: function onClick() {
            return handleAction(1);
          }
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          spritemap: spritemap,
          symbol: "angle-up"
        })), /*#__PURE__*/_react.default.createElement(_button.default, {
          "aria-label": ariaLabels.timeDown,
          className: "clay-time-inner-spin-btn clay-time-inner-spin-btn-dec",
          disabled: disabled,
          displayType: "secondary",
          onClick: function onClick() {
            return handleAction(-1);
          }
        }, /*#__PURE__*/_react.default.createElement(_icon.default, {
          spritemap: spritemap,
          symbol: "angle-down"
        })))))))), timezone && /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupItem, {
          shrink: true
        }, /*#__PURE__*/_react.default.createElement(_form.ClayInput.GroupText, null, "(".concat(timezone, ")")))))
      );
    };

    var _default = ClayTimePicker;
    exports.default = _default;
  }
});
//# sourceMappingURL=index.js.map