"use strict";

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;
exports.useFormLabel = useFormLabel;

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

var _InlineError = _interopRequireDefault(require("../InlineError/InlineError"));

var _classnames = _interopRequireDefault(require("classnames"));

var _uniqueId = _interopRequireDefault(require("lodash/uniqueId"));

var _flags = require("../flags");

var _excluded = ["className", "label", "labelClassName", "labelComponent", "errorMessage", "errorMessageClassName", "errorPlacement", "hint", "requirementLabel", "inversed", "wrapperIsFieldset"];

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 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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

/**
 * Takes a component's props and generates the props for its label, field,
 */
function useFormLabel(props) {
  var _props$id, _props$labelId, _props$errorId;

  // TODO: Once we're on React 18, we can use the `useId` hook
  var generatedId = (0, _react.useRef)((0, _uniqueId.default)('field_')).current;
  var id = (_props$id = props.id) !== null && _props$id !== void 0 ? _props$id : generatedId;
  var labelId = (_props$labelId = props.labelId) !== null && _props$labelId !== void 0 ? _props$labelId : "".concat(id, "-label");
  var errorId = (_props$errorId = props.errorId) !== null && _props$errorId !== void 0 ? _props$errorId : "".concat(id, "-error");

  var className = props.className,
      label = props.label,
      labelClassName = props.labelClassName,
      labelComponent = props.labelComponent,
      errorMessage = props.errorMessage,
      errorMessageClassName = props.errorMessageClassName,
      _props$errorPlacement = props.errorPlacement,
      errorPlacement = _props$errorPlacement === void 0 ? (0, _flags.errorPlacementDefault)() : _props$errorPlacement,
      hint = props.hint,
      requirementLabel = props.requirementLabel,
      inversed = props.inversed,
      wrapperIsFieldset = props.wrapperIsFieldset,
      remainingProps = _objectWithoutProperties(props, _excluded); // Bottom placed errors are handled in FormControl instead of FormLabel


  var bottomError = errorPlacement === 'bottom' && errorMessage ? /*#__PURE__*/_react.default.createElement(_InlineError.default, {
    id: errorId,
    inversed: inversed,
    className: errorMessageClassName
  }, errorMessage) : undefined; // Bottom placed errors cannot be linked to Choices in ChoiceList,
  // so we add a hidden error message to the label

  var showHiddenError = wrapperIsFieldset && bottomError;
  var labelProps = {
    children: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, label, showHiddenError && /*#__PURE__*/_react.default.createElement("div", {
      className: "ds-u-visibility--screen-reader"
    }, errorMessage)),
    className: labelClassName,
    component: labelComponent,
    errorMessage: bottomError ? undefined : errorMessage,
    errorMessageClassName: bottomError ? undefined : errorMessageClassName,
    errorId: errorId,
    // Avoid using `for` attribute for components with multiple inputs
    // i.e. ChoiceList, DateField, and other components that use `fieldset`
    fieldId: wrapperIsFieldset ? undefined : id,
    hint: hint,
    id: labelId,
    requirementLabel: requirementLabel,
    inversed: inversed
  };

  var fieldProps = _objectSpread(_objectSpread({}, remainingProps), {}, {
    id: id,
    errorId: errorId,
    errorMessage: errorMessage,
    errorPlacement: errorPlacement,
    inversed: inversed
  });

  var wrapperClassNames = (0, _classnames.default)({
    'ds-c-fieldset': wrapperIsFieldset
  }, className); // Use `aria-invalid` attribute on errored fieldsets
  // Errored form components without fieldsets must handle `aria-invalid` in their own component

  var ariaInvalid = wrapperIsFieldset && errorMessage ? true : undefined;
  var wrapperProps = {
    className: wrapperClassNames,
    'aria-invalid': ariaInvalid
  };
  return {
    labelProps: labelProps,
    fieldProps: fieldProps,
    wrapperProps: wrapperProps,
    bottomError: bottomError
  };
}

var _default = useFormLabel;
exports.default = _default;