import _FormHelperText from "@mui/material/FormHelperText";
import _FormControlLabel from "@mui/material/FormControlLabel";
import _Radio from "@mui/material/Radio";
/*!
 * 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.
 */
import { memo, useCallback, useImperativeHandle, useMemo, useRef } from "react";
import { useOdysseyDesignTokens } from "../OdysseyDesignTokensContext.js";
import { Typography } from "../Typography.js";
import { useUniqueId } from "../useUniqueId.js";
import { HintContainerWithInlineStartSpacing } from "./Hint.js";
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
const Radio = ({
  hint,
  id: idOverride,
  inputRef,
  isChecked,
  isDisabled,
  isInvalid,
  label: labelProp,
  name,
  testId,
  translate,
  value,
  isReadOnly,
  onChange: onChangeProp,
  onBlur: onBlurProp,
  onClick
}) => {
  const localInputRef = useRef(null);
  const odysseyDesignTokens = useOdysseyDesignTokens();
  const id = useUniqueId(idOverride);
  const hintId = hint ? `${id}-hint` : undefined;
  useImperativeHandle(inputRef, () => {
    return {
      focus: () => {
        localInputRef.current?.focus();
      }
    };
  }, []);
  const label = useMemo(() => _jsx(Typography, {
    component: "span",
    children: labelProp
  }), [labelProp]);
  const onChange = useCallback((event, checked) => {
    if (isReadOnly) {
      event.preventDefault();
    } else {
      onChangeProp?.(event, checked);
    }
  }, [onChangeProp, isReadOnly]);
  const handleClick = useCallback(event => {
    if (isReadOnly) {
      event.stopPropagation();
      event.preventDefault();
    }
  }, [isReadOnly]);
  const onBlur = useCallback(event => {
    onBlurProp?.(event);
  }, [onBlurProp]);
  const radioStyles = useMemo(() => ({
    ...(isReadOnly && {
      cursor: "default",
      "& .MuiTypography-root": {
        cursor: "default"
      }
    }),
    ...(hint && {
      ":not(:last-child)": {
        marginBlockEnd: "0"
      }
    })
  }), [hint, isReadOnly]);
  const inputProps = useMemo(() => ({
    "aria-describedby": hintId,
    "aria-disabled": isDisabled || isReadOnly,
    "data-se": testId,
    readOnly: isReadOnly,
    tabIndex: isReadOnly ? 0 : undefined
  }), [hintId, isDisabled, isReadOnly, testId]);
  return _jsxs(_Fragment, {
    children: [_jsx(_FormControlLabel, {
      checked: isChecked,
      className: isInvalid ? "Mui-error" : "",
      control: _jsx(_Radio, {
        disabled: isDisabled,
        inputProps: inputProps,
        inputRef: localInputRef,
        onChange: onChange,
        onClick: onClick || handleClick
      }),
      disabled: isDisabled,
      label: label,
      name: name,
      onBlur: onBlur,
      sx: radioStyles,
      translate: translate,
      value: value
    }), hint && _jsx(HintContainerWithInlineStartSpacing, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: _jsx(_FormHelperText, {
        id: hintId,
        translate: translate,
        children: hint
      })
    })]
  });
};
const MemoizedRadio = memo(Radio);
MemoizedRadio.displayName = "Radio";
export { MemoizedRadio as Radio };
//# sourceMappingURL=Radio.js.map