// packages/components/src/input-control/input-field.tsx
import { useDrag } from "@use-gesture/react";
import { forwardRef, useRef } from "@wordpress/element";
import { useDragCursor } from "./utils.js";
import { Input } from "./styles/input-control-styles.js";
import { useInputControlStateReducer } from "./reducer/reducer.js";
import { withIgnoreIMEEvents } from "../utils/with-ignore-ime-events.js";
import { jsx as _jsx } from "react/jsx-runtime";
var noop = () => {
};
function InputField({
  disabled = false,
  dragDirection = "n",
  dragThreshold = 10,
  id,
  isDragEnabled = false,
  isPressEnterToChange = false,
  onBlur = noop,
  onChange = noop,
  onDrag = noop,
  onDragEnd = noop,
  onDragStart = noop,
  onKeyDown = noop,
  onValidate = noop,
  size = "default",
  stateReducer = (state) => state,
  value: valueProp,
  type,
  ...props
}, ref) {
  const {
    // State.
    state,
    // Actions.
    change,
    commit,
    drag,
    dragEnd,
    dragStart,
    invalidate,
    pressDown,
    pressEnter,
    pressUp,
    reset
  } = useInputControlStateReducer(stateReducer, {
    isDragEnabled,
    value: valueProp,
    isPressEnterToChange
  }, onChange);
  const {
    value,
    isDragging,
    isDirty
  } = state;
  const wasDirtyOnBlur = useRef(false);
  const dragCursor = useDragCursor(isDragging, dragDirection);
  const handleOnBlur = (event) => {
    onBlur(event);
    if (isDirty || !event.target.validity.valid) {
      wasDirtyOnBlur.current = true;
      handleOnCommit(event);
    }
  };
  const handleOnChange = (event) => {
    const nextValue = event.target.value;
    change(nextValue, event);
  };
  const handleOnCommit = (event) => {
    const nextValue = event.currentTarget.value;
    try {
      onValidate(nextValue);
      commit(nextValue, event);
    } catch (err) {
      invalidate(err, event);
    }
  };
  const handleOnKeyDown = (event) => {
    const {
      key
    } = event;
    onKeyDown(event);
    switch (key) {
      case "ArrowUp":
        pressUp(event);
        break;
      case "ArrowDown":
        pressDown(event);
        break;
      case "Enter":
        pressEnter(event);
        if (isPressEnterToChange) {
          event.preventDefault();
          handleOnCommit(event);
        }
        break;
      case "Escape":
        if (isPressEnterToChange && isDirty) {
          event.preventDefault();
          reset(valueProp, event);
        }
        break;
    }
  };
  const dragGestureProps = useDrag((dragProps2) => {
    const {
      distance,
      dragging,
      event,
      target
    } = dragProps2;
    dragProps2.event = {
      ...dragProps2.event,
      target
    };
    if (!distance) {
      return;
    }
    event.stopPropagation();
    if (!dragging) {
      onDragEnd(dragProps2);
      dragEnd(dragProps2);
      return;
    }
    onDrag(dragProps2);
    drag(dragProps2);
    if (!isDragging) {
      onDragStart(dragProps2);
      dragStart(dragProps2);
    }
  }, {
    axis: dragDirection === "e" || dragDirection === "w" ? "x" : "y",
    threshold: dragThreshold,
    enabled: isDragEnabled,
    pointer: {
      capture: false
    }
  });
  const dragProps = isDragEnabled ? dragGestureProps() : {};
  return /* @__PURE__ */ _jsx(Input, {
    ...props,
    ...dragProps,
    className: "components-input-control__input",
    disabled,
    dragCursor,
    isDragging,
    id,
    onBlur: handleOnBlur,
    onChange: handleOnChange,
    onKeyDown: withIgnoreIMEEvents(handleOnKeyDown),
    ref,
    inputSize: size,
    value: value !== null && value !== void 0 ? value : "",
    type
  });
}
var ForwardedComponent = forwardRef(InputField);
var input_field_default = ForwardedComponent;
export {
  input_field_default as default
};
//# sourceMappingURL=input-field.js.map
