"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const clsx = require("clsx");
require("react");
const getSize = require("../../core/utils/get-size/get-size.cjs");
const createVarsResolver = require("../../core/styles-api/create-vars-resolver/create-vars-resolver.cjs");
const useResolvedStylesApi = require("../../core/styles-api/use-resolved-styles-api/use-resolved-styles-api.cjs");
const useStyles = require("../../core/styles-api/use-styles/use-styles.cjs");
const useProps = require("../../core/MantineProvider/use-props/use-props.cjs");
const extractStyleProps = require("../../core/Box/style-props/extract-style-props/extract-style-props.cjs");
const factory = require("../../core/factory/factory.cjs");
const ActionIcon = require("../ActionIcon/ActionIcon.cjs");
const Input = require("../Input/Input.cjs");
const InputBase = require("../InputBase/InputBase.cjs");
const PasswordToggleIcon = require("./PasswordToggleIcon.cjs");
const PasswordInput_module_css = require("./PasswordInput.module.css.cjs");
const useId = require("../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-id/use-id.cjs");
const useUncontrolled = require("../../../../../../../@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-uncontrolled/use-uncontrolled.cjs");
const defaultProps = {
  visibilityToggleIcon: PasswordToggleIcon.PasswordToggleIcon
};
const varsResolver = createVarsResolver.createVarsResolver((_, { size }) => ({
  root: {
    "--psi-icon-size": getSize.getSize(size, "psi-icon-size"),
    "--psi-button-size": getSize.getSize(size, "psi-button-size")
  }
}));
const PasswordInput = factory.factory((_props, ref) => {
  const props = useProps.useProps("PasswordInput", defaultProps, _props);
  const {
    classNames,
    className,
    style,
    styles,
    unstyled,
    vars,
    required,
    error,
    leftSection,
    disabled,
    id,
    variant,
    inputContainer,
    description,
    label,
    size,
    errorProps,
    descriptionProps,
    labelProps,
    withAsterisk,
    inputWrapperOrder,
    wrapperProps,
    radius,
    rightSection,
    rightSectionWidth,
    rightSectionPointerEvents,
    leftSectionWidth,
    visible,
    defaultVisible,
    onVisibilityChange,
    visibilityToggleIcon,
    visibilityToggleButtonProps,
    rightSectionProps,
    leftSectionProps,
    leftSectionPointerEvents,
    withErrorStyles,
    mod,
    ...others
  } = props;
  const uuid = useId.useId(id);
  const [_visible, setVisibility] = useUncontrolled.useUncontrolled({
    value: visible,
    defaultValue: defaultVisible,
    finalValue: false,
    onChange: onVisibilityChange
  });
  const toggleVisibility = () => setVisibility(!_visible);
  const getStyles = useStyles.useStyles({
    name: "PasswordInput",
    classes: PasswordInput_module_css.default,
    props,
    className,
    style,
    classNames,
    styles,
    unstyled,
    vars,
    varsResolver
  });
  const { resolvedClassNames, resolvedStyles } = useResolvedStylesApi.useResolvedStylesApi({
    classNames,
    styles,
    props
  });
  const { styleProps, rest } = extractStyleProps.extractStyleProps(others);
  const VisibilityToggleIcon = visibilityToggleIcon;
  const visibilityToggleButton = /* @__PURE__ */ jsxRuntime.jsx(
    ActionIcon.ActionIcon,
    {
      ...getStyles("visibilityToggle"),
      disabled,
      radius,
      "aria-hidden": !visibilityToggleButtonProps,
      tabIndex: -1,
      ...visibilityToggleButtonProps,
      variant: (visibilityToggleButtonProps == null ? void 0 : visibilityToggleButtonProps.variant) ?? "subtle",
      color: "gray",
      unstyled,
      onTouchEnd: (event) => {
        var _a;
        event.preventDefault();
        (_a = visibilityToggleButtonProps == null ? void 0 : visibilityToggleButtonProps.onTouchEnd) == null ? void 0 : _a.call(visibilityToggleButtonProps, event);
        toggleVisibility();
      },
      onMouseDown: (event) => {
        var _a;
        event.preventDefault();
        (_a = visibilityToggleButtonProps == null ? void 0 : visibilityToggleButtonProps.onMouseDown) == null ? void 0 : _a.call(visibilityToggleButtonProps, event);
        toggleVisibility();
      },
      onKeyDown: (event) => {
        var _a;
        (_a = visibilityToggleButtonProps == null ? void 0 : visibilityToggleButtonProps.onKeyDown) == null ? void 0 : _a.call(visibilityToggleButtonProps, event);
        if (event.key === " ") {
          event.preventDefault();
          toggleVisibility();
        }
      },
      children: /* @__PURE__ */ jsxRuntime.jsx(VisibilityToggleIcon, { reveal: _visible })
    }
  );
  return /* @__PURE__ */ jsxRuntime.jsx(
    Input.Input.Wrapper,
    {
      required,
      id: uuid,
      label,
      error,
      description,
      size,
      classNames: resolvedClassNames,
      styles: resolvedStyles,
      __staticSelector: "PasswordInput",
      errorProps,
      descriptionProps,
      unstyled,
      withAsterisk,
      inputWrapperOrder,
      inputContainer,
      variant,
      labelProps: { ...labelProps, htmlFor: uuid },
      mod,
      ...getStyles("root"),
      ...styleProps,
      ...wrapperProps,
      children: /* @__PURE__ */ jsxRuntime.jsx(
        Input.Input,
        {
          component: "div",
          error,
          leftSection,
          size,
          classNames: { ...resolvedClassNames, input: clsx(PasswordInput_module_css.default.input, resolvedClassNames.input) },
          styles: resolvedStyles,
          radius,
          disabled,
          __staticSelector: "PasswordInput",
          rightSectionWidth,
          rightSection: rightSection ?? visibilityToggleButton,
          variant,
          unstyled,
          leftSectionWidth,
          rightSectionPointerEvents: rightSectionPointerEvents || "all",
          rightSectionProps,
          leftSectionProps,
          leftSectionPointerEvents,
          withAria: false,
          withErrorStyles,
          children: /* @__PURE__ */ jsxRuntime.jsx(
            "input",
            {
              required,
              "data-invalid": !!error || void 0,
              "data-with-left-section": !!leftSection || void 0,
              ...getStyles("innerInput"),
              disabled,
              id: uuid,
              ref,
              ...rest,
              autoComplete: rest.autoComplete || "off",
              type: _visible ? "text" : "password"
            }
          )
        }
      )
    }
  );
});
PasswordInput.classes = { ...InputBase.InputBase.classes, ...PasswordInput_module_css.default };
PasswordInput.displayName = "@mantine/core/PasswordInput";
exports.PasswordInput = PasswordInput;
