"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.TextField = void 0;
var _LabelMask = _interopRequireDefault(require("./LabelMask"));
var _Mask = _interopRequireDefault(require("./Mask"));
var _TextInput = _interopRequireDefault(require("./TextInput"));
var _classnames = _interopRequireDefault(require("classnames"));
var _cleanFieldProps = _interopRequireDefault(require("../utilities/cleanFieldProps"));
var _describeField = _interopRequireDefault(require("../utilities/describeField"));
var _useId = _interopRequireDefault(require("../utilities/useId"));
var _Label = require("../Label");
var _useLabelProps = require("../Label/useLabelProps");
var _useHint = require("../Hint/useHint");
var _useInlineError = require("../InlineError/useInlineError");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/text-field/).
 */
const TextField = props => {
  const {
    id: originalId,
    mask,
    labelMask,
    className,
    ...remainingProps
  } = props;
  const id = (0, _useId.default)('text-field--', originalId);
  if (process.env.NODE_ENV !== 'production') {
    if (props.type === 'number') {
      console.warn(`Please use the 'numeric' prop instead of 'type="number"' unless your user research suggests otherwise.`);
    }
    if (mask && labelMask) {
      console.warn("The 'mask' and 'labelMask' fields are mutually exclusive. Please use either one or the other.");
    }
  }
  const {
    errorId,
    topError,
    bottomError,
    invalid
  } = (0, _useInlineError.useInlineError)({
    ...props,
    id
  });
  const {
    hintId,
    hintElement
  } = (0, _useHint.useHint)({
    ...props,
    id
  });
  const labelProps = (0, _useLabelProps.useLabelProps)({
    ...props,
    id
  });
  let input = /*#__PURE__*/(0, _jsxRuntime.jsx)(_TextInput.default
  // TypeScript doesn't know we set this in .defaultProps
  , {
    type: TextField.defaultProps.type,
    ...(0, _cleanFieldProps.default)(remainingProps),
    id: id,
    "aria-invalid": invalid,
    "aria-describedby": (0, _describeField.default)({
      ...props,
      errorId,
      hintId
    })
  });

  // See if we need to wrap the input based on some props
  if (mask) {
    input = /*#__PURE__*/(0, _jsxRuntime.jsx)(_Mask.default, {
      mask: mask,
      children: input
    });
  } else if (labelMask) {
    input = /*#__PURE__*/(0, _jsxRuntime.jsx)(_LabelMask.default, {
      labelMask: labelMask,
      children: input
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: (0, _classnames.default)('ds-u-clearfix',
    // fixes issue where the label's margin is collapsed
    className),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Label.Label, {
      ...labelProps,
      fieldId: id
    }), hintElement, topError, input, bottomError]
  });
};
exports.TextField = TextField;
TextField.defaultProps = {
  type: 'text'
};
var _default = exports.default = TextField;