import classNames from 'classnames';
import { Hint } from "../Hint";
import { InlineError } from "../InlineError";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "preact/compat/jsx-runtime";
/**
 * The Label component describes individual form fields (as a `<label>`) or fieldsets (as
 * a `<legend>`). They are built in to all form fields in the design system, but they can
 * also be used on their own to create custom fields.
 *
 * For information about how and when to use this component,
 * [refer to its full documentation page](https://design.cms.gov/components/label/).
 */
export const Label = props => {
  const {
    fieldId,
    id,
    children,
    component,
    hint,
    hintId,
    className,
    inversed,
    errorMessage,
    errorId,
    requirementLabel,
    ...labelProps
  } = props;
  if (process.env.NODE_ENV !== 'production' && (hint || hintId)) {
    console.warn("[Deprecated]: Hints are now their own component called 'Hint'. Please render your 'Hint' directly instead of passing them to 'Label'.");
  }
  if (process.env.NODE_ENV !== 'production' && errorMessage) {
    console.warn("[Deprecated]: Passing 'errorMessage' to the 'Label' component is now deprecated. Please render your error message directly with the 'InlineError' component instead.");
  }
  let hintElement;
  if (hint || requirementLabel) {
    hintElement = /*#__PURE__*/_jsx(Hint, {
      requirementLabel: requirementLabel,
      inversed: inversed,
      id: hintId,
      children: hint
    });
  }
  let errorElement = errorMessage;
  if (typeof errorMessage === 'string') {
    errorElement = /*#__PURE__*/_jsx(InlineError, {
      id: errorId,
      children: errorMessage
    });
  }
  let htmlFor = fieldId;
  if (component === 'legend' && fieldId) {
    console.warn('The `for` attribute is invalid for legends. Omitting `fieldId` from rendered element.');
    htmlFor = undefined;
  }
  const ComponentType = component;
  const classes = classNames('ds-c-label', className, inversed && 'ds-c-label--inverse');
  return /*#__PURE__*/_jsxs(_Fragment, {
    children: [/*#__PURE__*/_jsx(ComponentType, {
      className: classes,
      htmlFor: htmlFor,
      id: id,
      ...labelProps,
      children: children
    }), hintElement, errorElement]
  });
};
Label.defaultProps = {
  component: 'label'
};
export default Label;