import _FormHelperText from "@mui/material/FormHelperText";
import _FormControlLabel from "@mui/material/FormControlLabel";
import _Checkbox from "@mui/material/Checkbox";
/*!
 * 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 { useTranslation } from "../i18n.generated/i18n.js";
import { ComponentControlledState, getControlState } from "../inputUtils.js";
import { useOdysseyDesignTokens } from "../OdysseyDesignTokensContext.js";
import { Typography } from "../Typography.js";
import { useUniqueId } from "../useUniqueId.js";
import { HintContainerWithInlineStartSpacing } from "./Hint.js";
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
export const checkboxValidityValues = ["valid", "invalid", "inherit"];
const Checkbox = ({
  ariaLabel,
  ariaLabelledBy,
  id: idOverride,
  inputRef,
  isChecked,
  isDefaultChecked,
  isDisabled,
  isIndeterminate,
  isReadOnly = false,
  isRequired,
  label: labelProp,
  hint,
  name: nameOverride,
  onChange: onChangeProp,
  onBlur: onBlurProp,
  testId,
  translate,
  validity = "inherit",
  value
}) => {
  const {
    t
  } = useTranslation();
  const odysseyDesignTokens = useOdysseyDesignTokens();
  const id = useUniqueId(idOverride);
  const hintId = hint ? `${id}-hint` : undefined;
  const controlledStateRef = useRef(getControlState({
    controlledValue: isChecked,
    uncontrolledValue: isDefaultChecked
  }));
  const inputValues = useMemo(() => {
    if (controlledStateRef.current === ComponentControlledState.CONTROLLED) {
      return {
        checked: isChecked
      };
    }
    return {
      defaultChecked: isDefaultChecked
    };
  }, [isDefaultChecked, isChecked]);
  const localInputRef = useRef(null);
  useImperativeHandle(inputRef, () => ({
    focus: () => {
      localInputRef.current?.focus();
    }
  }), []);
  const label = useMemo(() => {
    return _jsxs(_Fragment, {
      children: [_jsx(Typography, {
        component: "span",
        children: labelProp
      }), isRequired && _jsxs(_Fragment, {
        children: [" ", _jsxs(Typography, {
          color: "textSecondary",
          component: "span",
          children: ["(", t("fieldlabel.required.text"), ")"]
        })]
      })]
    });
  }, [isRequired, labelProp, t]);
  const onChange = useCallback((event, checked) => {
    onChangeProp?.(event, checked);
  }, [onChangeProp]);
  const onClick = useCallback(event => {
    if (isReadOnly) {
      event.stopPropagation();
      event.preventDefault();
    }
  }, [isReadOnly]);
  const onBlur = useCallback(event => {
    onBlurProp?.(event);
  }, [onBlurProp]);
  const checkboxStyles = useMemo(() => ({
    alignItems: "flex-start",
    ...(isReadOnly && {
      cursor: "default",
      "& .MuiTypography-root": {
        cursor: "default"
      }
    }),
    ...(hint && {
      ":not(:last-child)": {
        marginBlockEnd: "0"
      }
    })
  }), [hint, isReadOnly]);
  const checkboxInputStyles = useMemo(() => ({
    marginBlockStart: "2px"
  }), []);
  const inputProps = useMemo(() => ({
    "aria-describedby": hintId,
    "aria-readonly": isReadOnly,
    "data-se": testId,
    readOnly: isReadOnly
  }), [hintId, isReadOnly, testId]);
  return _jsxs(_Fragment, {
    children: [_jsx(_FormControlLabel, {
      "aria-label": ariaLabel,
      "aria-labelledby": ariaLabelledBy,
      className: validity === "invalid" ? "Mui-error" : validity === "valid" ? "Mui-valid" : "",
      control: _jsx(_Checkbox, {
        ...inputValues,
        disabled: isDisabled,
        indeterminate: isIndeterminate,
        inputProps: inputProps,
        inputRef: localInputRef,
        onChange: onChange,
        onClick: onClick,
        required: isRequired,
        sx: checkboxInputStyles
      }),
      disabled: isDisabled,
      id: idOverride,
      label: label,
      name: nameOverride ?? idOverride,
      onBlur: onBlur,
      required: isRequired,
      sx: checkboxStyles,
      translate: translate,
      value: value
    }), hint && _jsx(HintContainerWithInlineStartSpacing, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: _jsx(_FormHelperText, {
        id: hintId,
        translate: translate,
        children: hint
      })
    })]
  });
};
const MemoizedCheckbox = memo(Checkbox);
MemoizedCheckbox.displayName = "Checkbox";
export { MemoizedCheckbox as Checkbox };
//# sourceMappingURL=Checkbox.js.map