// src/components/user-action/ToggleableInput.tsx
import { useEffect as useEffect2, useRef, useState as useState2 } from "react";
import { Pencil } from "lucide-react";
import clsx from "clsx";

// src/hooks/useDelay.ts
import { useEffect, useState } from "react";
var defaultOptions = {
  delay: 3e3,
  disabled: false
};
function useDelay(options) {
  const [timer, setTimer] = 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));
  };
  useEffect(() => {
    return () => {
      clearTimeout(timer);
    };
  }, [timer]);
  useEffect(() => {
    if (disabled) {
      clearTimeout(timer);
      setTimer(void 0);
    }
  }, [disabled, timer]);
  console.log(timer);
  return { restartTimer, clearTimer, hasActiveTimer: !!timer };
}

// src/util/noop.ts
var noop = () => void 0;

// src/components/user-action/ToggleableInput.tsx
import { jsx, jsxs } from "react/jsx-runtime";
var ToggleableInput = ({
  type = "text",
  value,
  onChange = noop,
  onChangeText = noop,
  onEditCompleted = noop,
  labelClassName = "",
  initialState = "display",
  size = 16,
  disclaimer,
  onBlur,
  saveDelayOptions,
  ...restProps
}) => {
  const [isEditing, setIsEditing] = useState2(initialState !== "display");
  const { restartTimer, clearTimer } = useDelay(saveDelayOptions);
  const ref = useRef(null);
  const onEditCompletedWrapper = (text) => {
    onEditCompleted(text);
    clearTimer();
  };
  useEffect2(() => {
    if (isEditing) {
      ref.current?.focus();
    }
  }, [isEditing]);
  return /* @__PURE__ */ jsxs("div", { children: [
    /* @__PURE__ */ jsxs(
      "div",
      {
        className: clsx("row items-center w-full gap-x-2 overflow-hidden", { "cursor-pointer": !isEditing }),
        onClick: () => !isEditing ? setIsEditing(!isEditing) : void 0,
        children: [
          /* @__PURE__ */ jsx("div", { className: clsx("row overflow-hidden", { "flex-1": isEditing }), children: isEditing ? /* @__PURE__ */ jsx(
            "input",
            {
              ref,
              ...restProps,
              value,
              type,
              onChange: (event) => {
                const value2 = event.target.value;
                restartTimer(() => {
                  onEditCompletedWrapper(value2);
                });
                onChangeText(value2);
                onChange(event);
              },
              onBlur: (event) => {
                if (onBlur) {
                  onBlur(event);
                }
                onEditCompletedWrapper(value);
                setIsEditing(false);
              },
              onKeyDown: (event) => {
                if (event.key === "Enter") {
                  setIsEditing(false);
                  onEditCompletedWrapper(value);
                }
              },
              className: clsx(`w-full border-none rounded-none ring-0 outline-0 text-inherit bg-inherit shadow-transparent decoration-primary p-0 underline-offset-4`, {
                underline: isEditing
              }, labelClassName),
              onFocus: (event) => event.target.select()
            }
          ) : /* @__PURE__ */ jsx("span", { className: clsx("max-w-xs break-words overflow-hidden", labelClassName), children: value }) }),
          /* @__PURE__ */ jsx(
            Pencil,
            {
              className: clsx(`cursor-pointer`, { "text-transparent": isEditing }),
              size,
              style: { minWidth: `${size}px` }
            }
          )
        ]
      }
    ),
    isEditing && disclaimer && /* @__PURE__ */ jsx("label", { className: "text-negative", children: disclaimer })
  ] });
};
var ToggleableInputUncontrolled = ({
  value: initialValue,
  onChangeText = noop,
  ...restProps
}) => {
  const [value, setValue] = useState2(initialValue);
  useEffect2(() => {
    setValue(initialValue);
  }, [initialValue]);
  return /* @__PURE__ */ jsx(
    ToggleableInput,
    {
      value,
      onChangeText: (text) => {
        setValue(text);
        onChangeText(text);
      },
      ...restProps
    }
  );
};
export {
  ToggleableInput,
  ToggleableInputUncontrolled
};
//# sourceMappingURL=ToggleableInput.mjs.map