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

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

import React, { useRef } from 'react';
import InlineError from '../InlineError/InlineError';
import classNames from 'classnames';
import uniqueId from 'lodash/uniqueId';
import { errorPlacementDefault } from '../flags'; // TODO: Reimplement focusTrigger in another place, like another hook
// Some of the FormLabel's props we pass on to the component prop definitions.
// TODO: This could use a better name

/**
 * Takes a component's props and generates the props for its label, field,
 */
export 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 = useRef(uniqueId('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 ? 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.createElement(InlineError, {
    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.createElement(React.Fragment, null, label, showHiddenError && /*#__PURE__*/React.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 = classNames({
    '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
  };
}
export default useFormLabel;