"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fieldTypeValues = exports.Field = void 0;
var _FormControl2 = _interopRequireDefault(require("@mui/material/FormControl"));
var _FormLabel2 = _interopRequireDefault(require("@mui/material/FormLabel"));
var _react = require("react");
var _FieldError = require("./FieldError.cjs");
var _FieldHint = require("./FieldHint.cjs");
var _FieldLabel = require("./FieldLabel.cjs");
var _FieldsetContext = require("./FieldsetContext.cjs");
var _i18n = require("./i18n.generated/i18n.cjs");
var _Typography = require("./Typography.cjs");
var _useUniqueId = require("./useUniqueId.cjs");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/*!
 * Copyright (c) 2022-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

const fieldTypeValues = exports.fieldTypeValues = ["single", "group"];
const Field = ({
  ariaDescribedBy,
  errorMessage,
  errorMessageList,
  fieldType,
  hasVisibleLabel,
  hint,
  HintLinkComponent,
  id: idOverride,
  isDisabled: isDisabledProp = false,
  isFullWidth = false,
  isRadioGroup = false,
  isOptional = false,
  isReadOnly = false,
  label,
  renderFieldComponent
}) => {
  const {
    t
  } = (0, _i18n.useTranslation)();
  const id = (0, _useUniqueId.useUniqueId)(idOverride);
  const hintId = hint ? `${id}-hint` : undefined;
  const errorMessageElementId = errorMessage || errorMessageList ? `${id}-error` : undefined;
  const labelElementId = `${id}-label`;
  const localAriaDescribedBy = (0, _react.useMemo)(() => [hintId, errorMessageElementId, ariaDescribedBy].join(" ").trim() || undefined, [ariaDescribedBy, errorMessageElementId, hintId]);
  const {
    isDisabled: isFieldsetDisabled
  } = (0, _FieldsetContext.useFieldset)();
  const isDisabled = (0, _react.useMemo)(() => isDisabledProp || isFieldsetDisabled, [isDisabledProp, isFieldsetDisabled]);
  return (0, _jsxRuntime.jsxs)(_FormControl2.default, {
    component: fieldType === "group" ? "fieldset" : "div",
    disabled: isDisabled,
    error: Boolean(errorMessage) || Array.isArray(errorMessageList) && errorMessageList.length > 0,
    fullWidth: isFullWidth,
    role: isRadioGroup ? "radiogroup" : undefined,
    children: [fieldType === "group" ? (0, _jsxRuntime.jsxs)(_FormLabel2.default, {
      component: "legend",
      id: labelElementId,
      children: [label, " ", isOptional && label && (0, _jsxRuntime.jsxs)(_Typography.Typography, {
        color: "textSecondary",
        component: "span",
        children: ["(", t("fieldlabel.optional.text"), ")"]
      })]
    }) : (0, _jsxRuntime.jsx)(_FieldLabel.FieldLabel, {
      hasVisibleLabel: hasVisibleLabel,
      id: labelElementId,
      inputId: id,
      isOptional: isOptional,
      text: label
    }), hint && (0, _jsxRuntime.jsx)(_FieldHint.FieldHint, {
      id: hintId,
      LinkComponent: HintLinkComponent,
      text: hint
    }), renderFieldComponent({
      ariaDescribedBy: localAriaDescribedBy,
      errorMessageElementId,
      id,
      labelElementId,
      isReadOnly
    }), (errorMessage || errorMessageList) && (0, _jsxRuntime.jsx)(_FieldError.FieldError, {
      id: errorMessageElementId,
      message: errorMessage,
      messageList: errorMessageList
    })]
  });
};
const MemoizedField = exports.Field = (0, _react.memo)(Field);
MemoizedField.displayName = "Field";
//# sourceMappingURL=Field.cjs.map