"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Radio = void 0;
var _FormHelperText2 = _interopRequireDefault(require("@mui/material/FormHelperText"));
var _FormControlLabel2 = _interopRequireDefault(require("@mui/material/FormControlLabel"));
var _Radio2 = _interopRequireDefault(require("@mui/material/Radio"));
var _react = require("react");
var _OdysseyDesignTokensContext = require("../OdysseyDesignTokensContext.cjs");
var _Typography = require("../Typography.cjs");
var _useUniqueId = require("../useUniqueId.cjs");
var _Hint = require("./Hint.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 Radio = ({
  hint,
  id: idOverride,
  inputRef,
  isChecked,
  isDisabled,
  isInvalid,
  label: labelProp,
  name,
  testId,
  translate,
  value,
  isReadOnly,
  onChange: onChangeProp,
  onBlur: onBlurProp,
  onClick
}) => {
  const localInputRef = (0, _react.useRef)(null);
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const id = (0, _useUniqueId.useUniqueId)(idOverride);
  const hintId = hint ? `${id}-hint` : undefined;
  (0, _react.useImperativeHandle)(inputRef, () => {
    return {
      focus: () => {
        localInputRef.current?.focus();
      }
    };
  }, []);
  const label = (0, _react.useMemo)(() => (0, _jsxRuntime.jsx)(_Typography.Typography, {
    component: "span",
    children: labelProp
  }), [labelProp]);
  const onChange = (0, _react.useCallback)((event, checked) => {
    if (isReadOnly) {
      event.preventDefault();
    } else {
      onChangeProp?.(event, checked);
    }
  }, [onChangeProp, isReadOnly]);
  const handleClick = (0, _react.useCallback)(event => {
    if (isReadOnly) {
      event.stopPropagation();
      event.preventDefault();
    }
  }, [isReadOnly]);
  const onBlur = (0, _react.useCallback)(event => {
    onBlurProp?.(event);
  }, [onBlurProp]);
  const radioStyles = (0, _react.useMemo)(() => ({
    ...(isReadOnly && {
      cursor: "default",
      "& .MuiTypography-root": {
        cursor: "default"
      }
    }),
    ...(hint && {
      ":not(:last-child)": {
        marginBlockEnd: "0"
      }
    })
  }), [hint, isReadOnly]);
  const inputProps = (0, _react.useMemo)(() => ({
    "aria-describedby": hintId,
    "aria-disabled": isDisabled || isReadOnly,
    "data-se": testId,
    readOnly: isReadOnly,
    tabIndex: isReadOnly ? 0 : undefined
  }), [hintId, isDisabled, isReadOnly, testId]);
  return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [(0, _jsxRuntime.jsx)(_FormControlLabel2.default, {
      checked: isChecked,
      className: isInvalid ? "Mui-error" : "",
      control: (0, _jsxRuntime.jsx)(_Radio2.default, {
        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 && (0, _jsxRuntime.jsx)(_Hint.HintContainerWithInlineStartSpacing, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: (0, _jsxRuntime.jsx)(_FormHelperText2.default, {
        id: hintId,
        translate: translate,
        children: hint
      })
    })]
  });
};
const MemoizedRadio = exports.Radio = (0, _react.memo)(Radio);
MemoizedRadio.displayName = "Radio";
//# sourceMappingURL=Radio.cjs.map