"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.FormFieldLabel = FormFieldLabel;
exports.getFormFieldLabelId = getFormFieldLabelId;
var _react = _interopRequireDefault(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _fontAwesomeIcon = require("../font-awesome-icon");
var _tooltip = require("../tooltip");
var _useTranslation = require("../hooks/use-translation");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function getFormFieldLabelId(id) {
  return `label-for-${id}`;
}
function FormFieldLabel({
  id,
  label,
  labelTestId = 'dr-field-label',
  labelIcon = null,
  labelIconTestId = '',
  labelIconTooltipText = '',
  labelIconAriaLabel,
  isDisabled = false,
  isOptional = false,
  optionalLabelText,
  isRequired = false,
  isReadOnly
}) {
  const labelId = getFormFieldLabelId(id);
  const {
    t
  } = (0, _useTranslation.useTranslation)();
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("label", {
      id: labelId,
      className: "dr-form-field-label",
      htmlFor: id,
      "test-id": labelTestId,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: (0, _classnames.default)('label-text', {
          disabled: isDisabled,
          required: isRequired,
          readonly: isReadOnly
        }),
        children: label
      }), labelIcon && /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.Tooltip, {
        content: labelIconTooltipText,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontAwesomeIcon.FontAwesomeIcon, {
          role: labelIconTooltipText ? 'button' : undefined,
          "aria-describedby": `${labelId}-sr-only-description`,
          "aria-hidden": !labelIconTooltipText,
          tabIndex: labelIconTooltipText ? 0 : undefined // always should be focusable when navigating with a keyboard because imporant explanation is provided.
          ,
          icon: labelIcon,
          "test-id": labelIconTestId
        })
      }), isOptional && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        className: "optional-label",
        "test-id": "form-field-optional-label",
        children: optionalLabelText ?? t('Optional')
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
      id: `${labelId}-sr-only-description`,
      className: "sr-only",
      "aria-label": labelIconTooltipText ? labelIconAriaLabel ?? t('Extra information in a tooltip') : undefined,
      children: labelIconTooltipText
    })]
  });
}