"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.checkboxValidityValues = exports.Checkbox = void 0;
var _FormHelperText2 = _interopRequireDefault(require("@mui/material/FormHelperText"));
var _FormControlLabel2 = _interopRequireDefault(require("@mui/material/FormControlLabel"));
var _Checkbox2 = _interopRequireDefault(require("@mui/material/Checkbox"));
var _react = require("react");
var _i18n = require("../i18n.generated/i18n.cjs");
var _inputUtils = require("../inputUtils.cjs");
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 checkboxValidityValues = exports.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
  } = (0, _i18n.useTranslation)();
  const odysseyDesignTokens = (0, _OdysseyDesignTokensContext.useOdysseyDesignTokens)();
  const id = (0, _useUniqueId.useUniqueId)(idOverride);
  const hintId = hint ? `${id}-hint` : undefined;
  const controlledStateRef = (0, _react.useRef)((0, _inputUtils.getControlState)({
    controlledValue: isChecked,
    uncontrolledValue: isDefaultChecked
  }));
  const inputValues = (0, _react.useMemo)(() => {
    if (controlledStateRef.current === _inputUtils.ComponentControlledState.CONTROLLED) {
      return {
        checked: isChecked
      };
    }
    return {
      defaultChecked: isDefaultChecked
    };
  }, [isDefaultChecked, isChecked]);
  const localInputRef = (0, _react.useRef)(null);
  (0, _react.useImperativeHandle)(inputRef, () => ({
    focus: () => {
      localInputRef.current?.focus();
    }
  }), []);
  const label = (0, _react.useMemo)(() => {
    return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [(0, _jsxRuntime.jsx)(_Typography.Typography, {
        component: "span",
        children: labelProp
      }), isRequired && (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [" ", (0, _jsxRuntime.jsxs)(_Typography.Typography, {
          color: "textSecondary",
          component: "span",
          children: ["(", t("fieldlabel.required.text"), ")"]
        })]
      })]
    });
  }, [isRequired, labelProp, t]);
  const onChange = (0, _react.useCallback)((event, checked) => {
    onChangeProp?.(event, checked);
  }, [onChangeProp]);
  const onClick = (0, _react.useCallback)(event => {
    if (isReadOnly) {
      event.stopPropagation();
      event.preventDefault();
    }
  }, [isReadOnly]);
  const onBlur = (0, _react.useCallback)(event => {
    onBlurProp?.(event);
  }, [onBlurProp]);
  const checkboxStyles = (0, _react.useMemo)(() => ({
    alignItems: "flex-start",
    ...(isReadOnly && {
      cursor: "default",
      "& .MuiTypography-root": {
        cursor: "default"
      }
    }),
    ...(hint && {
      ":not(:last-child)": {
        marginBlockEnd: "0"
      }
    })
  }), [hint, isReadOnly]);
  const checkboxInputStyles = (0, _react.useMemo)(() => ({
    marginBlockStart: "2px"
  }), []);
  const inputProps = (0, _react.useMemo)(() => ({
    "aria-describedby": hintId,
    "aria-readonly": isReadOnly,
    "data-se": testId,
    readOnly: isReadOnly
  }), [hintId, isReadOnly, testId]);
  return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [(0, _jsxRuntime.jsx)(_FormControlLabel2.default, {
      "aria-label": ariaLabel,
      "aria-labelledby": ariaLabelledBy,
      className: validity === "invalid" ? "Mui-error" : validity === "valid" ? "Mui-valid" : "",
      control: (0, _jsxRuntime.jsx)(_Checkbox2.default, {
        ...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 && (0, _jsxRuntime.jsx)(_Hint.HintContainerWithInlineStartSpacing, {
      odysseyDesignTokens: odysseyDesignTokens,
      children: (0, _jsxRuntime.jsx)(_FormHelperText2.default, {
        id: hintId,
        translate: translate,
        children: hint
      })
    })]
  });
};
const MemoizedCheckbox = exports.Checkbox = (0, _react.memo)(Checkbox);
MemoizedCheckbox.displayName = "Checkbox";
//# sourceMappingURL=Checkbox.cjs.map