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

import React, { useState, useRef } from 'react';
import classNames from 'classnames';
import uniqueId from 'lodash/uniqueId';
export var RE_DATE = /^(\d{1,2})[\D]?(\d{1,2})?[\D]?(\d{1,4})?/;
/**
 * Takes the string value from an input and returns a string
 * with appropriate date format masking applied concatenated
 * with the date hint text, MM/DD/YYYY.
 *
 * `valueOnly` defaults to false. If true, returns formatted
 * string without additional hint text.
 */

export var DATE_MASK = function DATE_MASK() {
  var rawInput = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  var valueOnly = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  var match = RE_DATE.exec(rawInput);
  var formattedDate = '';

  if (match) {
    var _match$slice = match.slice(1),
        _match$slice2 = _slicedToArray(_match$slice, 3),
        month = _match$slice2[0],
        day = _match$slice2[1],
        year = _match$slice2[2];

    formattedDate = [// We treat all non-numeric characters as a delimiter. If they're using a
    // delimiter after a month or day, we interpret that as the user supplying
    // a single digit for month or day, which we will automatically pad for them.
    month && month.padStart(2, '0'), day && day.padStart(2, '0'), year].filter(function (s) {
      return s;
    }).join('/');
  }

  if (valueOnly) {
    return formattedDate;
  }

  var hint = 'MM/DD/YYYY';
  var hintSub = hint.substring(formattedDate.length);
  return formattedDate + hintSub;
};
export function useLabelMask(maskFn, originalInputProps) {
  var _originalInputProps$v, _originalInputProps$v2;

  // TODO: Once we're on React 18, we can use the `useId` hook
  var labelMaskId = useRef(uniqueId('labelmask_')).current;

  var _useState = useState(false),
      _useState2 = _slicedToArray(_useState, 2),
      focused = _useState2[0],
      setFocused = _useState2[1];

  var _onFocus = originalInputProps.onFocus,
      _onBlur = originalInputProps.onBlur,
      onChange = originalInputProps.onChange;
  var value = (_originalInputProps$v = (_originalInputProps$v2 = originalInputProps.value) === null || _originalInputProps$v2 === void 0 ? void 0 : _originalInputProps$v2.toString()) !== null && _originalInputProps$v !== void 0 ? _originalInputProps$v : '';

  var inputProps = _objectSpread(_objectSpread({}, originalInputProps), {}, {
    defaultValue: undefined,
    onFocus: function onFocus(e) {
      if (_onFocus) {
        _onFocus(e);
      }

      setFocused(true);
    },
    onBlur: function onBlur(e) {
      var maskedValue = maskFn(value.toString(), true);
      e.currentTarget.value = maskedValue;
      e.target.value = maskedValue;

      if (onChange) {
        onChange(e);
      }

      if (_onBlur) {
        _onBlur(e);
      }

      setFocused(false);
    },
    type: 'text',
    inputMode: 'numeric',
    'aria-describedby': classNames(originalInputProps['aria-describedby'], labelMaskId)
  });

  return {
    labelMask: /*#__PURE__*/React.createElement("div", {
      className: "ds-c-label-mask",
      id: labelMaskId
    }, /*#__PURE__*/React.createElement("span", {
      className: classNames(focused && 'ds-u-visibility--screen-reader')
    }, maskFn('')), /*#__PURE__*/React.createElement("span", {
      className: classNames(!focused && 'ds-u-display--none'),
      "aria-hidden": "true"
    }, maskFn(value))),
    inputProps: inputProps
  };
}
export default useLabelMask;