var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/user-action/input/Input.tsx
var Input_exports = {};
__export(Input_exports, {
  Input: () => Input,
  InputUncontrolled: () => InputUncontrolled
});
module.exports = __toCommonJS(Input_exports);
var import_react4 = require("react");
var import_clsx = __toESM(require("clsx"), 1);

// src/hooks/useDelay.ts
var import_react = require("react");
var defaultOptions = {
  delay: 3e3,
  disabled: false
};
function useDelay(options) {
  const [timer, setTimer] = (0, import_react.useState)(void 0);
  const { delay, disabled } = {
    ...defaultOptions,
    ...options
  };
  const clearTimer = () => {
    clearTimeout(timer);
    setTimer(void 0);
  };
  const restartTimer = (onDelayFinish) => {
    if (disabled) {
      return;
    }
    clearTimeout(timer);
    setTimer(setTimeout(() => {
      onDelayFinish();
      setTimer(void 0);
    }, delay));
  };
  (0, import_react.useEffect)(() => {
    return () => {
      clearTimeout(timer);
    };
  }, [timer]);
  (0, import_react.useEffect)(() => {
    if (disabled) {
      clearTimeout(timer);
      setTimer(void 0);
    }
  }, [disabled, timer]);
  return { restartTimer, clearTimer, hasActiveTimer: !!timer };
}

// src/hooks/focus/useFocusManagement.ts
var import_react2 = require("react");
function useFocusManagement() {
  const getFocusableElements = (0, import_react2.useCallback)(() => {
    return Array.from(
      document.querySelectorAll(
        'input, button, select, textarea, a[href], [tabindex]:not([tabindex="-1"])'
      )
    ).filter(
      (el) => el instanceof HTMLElement && !el.hasAttribute("disabled") && !el.hasAttribute("hidden") && el.tabIndex !== -1
    );
  }, []);
  const getNextFocusElement = (0, import_react2.useCallback)(() => {
    const elements = getFocusableElements();
    if (elements.length === 0) {
      return void 0;
    }
    let nextElement = elements[0];
    if (document.activeElement instanceof HTMLElement) {
      const currentIndex = elements.indexOf(document.activeElement);
      nextElement = elements[(currentIndex + 1) % elements.length];
    }
    return nextElement;
  }, [getFocusableElements]);
  const focusNext = (0, import_react2.useCallback)(() => {
    const nextElement = getNextFocusElement();
    nextElement?.focus();
  }, [getNextFocusElement]);
  const getPreviousFocusElement = (0, import_react2.useCallback)(() => {
    const elements = getFocusableElements();
    if (elements.length === 0) {
      return void 0;
    }
    let previousElement = elements[0];
    if (document.activeElement instanceof HTMLElement) {
      const currentIndex = elements.indexOf(document.activeElement);
      if (currentIndex === 0) {
        previousElement = elements[elements.length - 1];
      } else {
        previousElement = elements[currentIndex - 1];
      }
    }
    return previousElement;
  }, [getFocusableElements]);
  const focusPrevious = (0, import_react2.useCallback)(() => {
    const previousElement = getPreviousFocusElement();
    if (previousElement) previousElement.focus();
  }, [getPreviousFocusElement]);
  return {
    getFocusableElements,
    getNextFocusElement,
    getPreviousFocusElement,
    focusNext,
    focusPrevious
  };
}

// src/hooks/useOverwritableState.ts
var import_react3 = require("react");

// src/utils/resolveSetState.ts
function resolveSetState(action, prev) {
  return typeof action === "function" ? action(prev) : action;
}

// src/hooks/useOverwritableState.ts
var useOverwritableState = (initialValue, onChange) => {
  const [state, setState] = (0, import_react3.useState)(initialValue);
  (0, import_react3.useEffect)(() => {
    setState(initialValue);
  }, [initialValue]);
  const onChangeWrapper = (action) => {
    const resolved = resolveSetState(action, state);
    setState(resolved);
    onChange?.(state);
  };
  return [state, onChangeWrapper];
};

// src/components/user-action/input/Input.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var defaultEditCompleteOptions = {
  allowEnterComplete: false,
  onBlur: true,
  afterDelay: true,
  delay: 2500
};
var Input = (0, import_react4.forwardRef)(function Input2({
  value,
  onChange,
  onChangeText,
  onEditCompleted,
  editCompleteOptions,
  disabled = false,
  invalid = false,
  defaultStyle = true,
  className,
  ...props
}, forwardedRef) {
  const {
    onBlur: allowEditCompleteOnBlur,
    afterDelay,
    delay,
    allowEnterComplete
  } = { ...defaultEditCompleteOptions, ...editCompleteOptions };
  const {
    restartTimer,
    clearTimer
  } = useDelay({ delay, disabled: !afterDelay });
  const innerRef = (0, import_react4.useRef)(null);
  (0, import_react4.useImperativeHandle)(forwardedRef, () => innerRef.current);
  const { focusNext } = useFocusManagement();
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    "input",
    {
      ...props,
      ref: innerRef,
      value,
      disabled,
      className: defaultStyle ? (0, import_clsx.default)(
        "px-3 py-2 rounded-md text-sm h-10 border-2 border-transparent focus-style-none",
        {
          "bg-input-background text-input-text hover:border-primary focus:border-primary": !disabled && !invalid,
          "bg-negative/20 text-negative hover:border-negative focus-visible:border-negative": !disabled && invalid,
          "bg-disabled-background text-disabled border-disabled-border": disabled
        },
        className
      ) : className,
      onKeyDown: (event) => {
        props.onKeyDown?.(event);
        if (!allowEnterComplete) {
          return;
        }
        if (event.key === "Enter" && !event.shiftKey) {
          event.preventDefault();
          innerRef.current?.blur();
          onEditCompleted?.(event.target.value);
          focusNext();
        }
      },
      onBlur: (event) => {
        props.onBlur?.(event);
        if (allowEditCompleteOnBlur) {
          onEditCompleted?.(event.target.value);
          clearTimer();
        }
      },
      onChange: (event) => {
        onChange?.(event);
        const value2 = event.target.value;
        restartTimer(() => {
          innerRef.current?.blur();
          onEditCompleted?.(value2);
        });
        onChangeText?.(value2);
      },
      "aria-invalid": props["aria-invalid"] ?? invalid,
      "aria-disabled": props["aria-disabled"] ?? disabled
    }
  );
});
var InputUncontrolled = ({
  value = "",
  onChangeText,
  ...props
}) => {
  const [usedValue, setUsedValue] = useOverwritableState(value, onChangeText);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    Input,
    {
      ...props,
      value: usedValue,
      onChangeText: setUsedValue
    }
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Input,
  InputUncontrolled
});
//# sourceMappingURL=Input.cjs.map