import { jsx, jsxs, Fragment } from "react/jsx-runtime";
import clsx from "clsx";
import { HiddenDatesInput } from "../HiddenDatesInput/HiddenDatesInput.mjs";
import classes from "./PickerInputBase.module.css.mjs";
import { factory } from "../../../../../../../@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/factory/factory.mjs";
import { useInputProps } from "../../../../../../../@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Input/use-input-props.mjs";
import { CloseButton } from "../../../../../../../@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/CloseButton/CloseButton.mjs";
import { Modal } from "../../../../../../../@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Modal/Modal.mjs";
import { Input } from "../../../../../../../@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Input/Input.mjs";
import { Popover } from "../../../../../../../@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Popover/Popover.mjs";
const defaultProps = {};
const PickerInputBase = factory((_props, ref) => {
  const {
    inputProps,
    wrapperProps,
    placeholder,
    classNames,
    styles,
    unstyled,
    popoverProps,
    modalProps,
    dropdownType,
    children,
    formattedValue,
    dropdownHandlers,
    dropdownOpened,
    onClick,
    clearable,
    onClear,
    clearButtonProps,
    rightSection,
    shouldClear,
    readOnly,
    disabled,
    value,
    name,
    form,
    type,
    ...others
  } = useInputProps("PickerInputBase", defaultProps, _props);
  const _rightSection = rightSection || (clearable && shouldClear && !readOnly && !disabled ? /* @__PURE__ */ jsx(
    CloseButton,
    {
      variant: "transparent",
      onClick: onClear,
      unstyled,
      size: inputProps.size || "sm",
      ...clearButtonProps
    }
  ) : null);
  const handleClose = () => {
    const isInvalidRangeValue = type === "range" && Array.isArray(value) && value[0] && !value[1];
    if (isInvalidRangeValue) {
      onClear();
    }
    dropdownHandlers.close();
  };
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    dropdownType === "modal" && !readOnly && /* @__PURE__ */ jsx(
      Modal,
      {
        opened: dropdownOpened,
        onClose: handleClose,
        withCloseButton: false,
        size: "auto",
        "data-dates-modal": true,
        unstyled,
        ...modalProps,
        children
      }
    ),
    /* @__PURE__ */ jsx(Input.Wrapper, { ...wrapperProps, children: /* @__PURE__ */ jsxs(
      Popover,
      {
        position: "bottom-start",
        opened: dropdownOpened,
        trapFocus: true,
        returnFocus: false,
        unstyled,
        ...popoverProps,
        disabled: (popoverProps == null ? void 0 : popoverProps.disabled) || dropdownType === "modal" || readOnly,
        onChange: (_opened) => {
          var _a;
          if (!_opened) {
            (_a = popoverProps == null ? void 0 : popoverProps.onClose) == null ? void 0 : _a.call(popoverProps);
            handleClose();
          }
        },
        children: [
          /* @__PURE__ */ jsx(Popover.Target, { children: /* @__PURE__ */ jsx(
            Input,
            {
              "data-dates-input": true,
              "data-read-only": readOnly || void 0,
              disabled,
              component: "button",
              type: "button",
              multiline: true,
              onClick: (event) => {
                onClick == null ? void 0 : onClick(event);
                dropdownHandlers.toggle();
              },
              rightSection: _rightSection,
              ...inputProps,
              ref,
              classNames: { ...classNames, input: clsx(classes.input, classNames == null ? void 0 : classNames.input) },
              ...others,
              children: formattedValue || /* @__PURE__ */ jsx(
                Input.Placeholder,
                {
                  error: inputProps.error,
                  unstyled,
                  className: classNames == null ? void 0 : classNames.placeholder,
                  style: styles == null ? void 0 : styles.placeholder,
                  children: placeholder
                }
              )
            }
          ) }),
          /* @__PURE__ */ jsx(Popover.Dropdown, { "data-dates-dropdown": true, children })
        ]
      }
    ) }),
    /* @__PURE__ */ jsx(HiddenDatesInput, { value, name, form, type })
  ] });
});
PickerInputBase.classes = classes;
PickerInputBase.displayName = "@mantine/dates/PickerInputBase";
export {
  PickerInputBase
};
