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/ToggleableInput.tsx
var ToggleableInput_exports = {};
__export(ToggleableInput_exports, {
  ToggleableInput: () => ToggleableInput,
  ToggleableInputUncontrolled: () => ToggleableInputUncontrolled
});
module.exports = __toCommonJS(ToggleableInput_exports);
var import_react2 = require("react");
var import_lucide_react = require("lucide-react");
var import_clsx = __toESM(require("clsx"));

// 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]);
  console.log(timer);
  return { restartTimer, clearTimer, hasActiveTimer: !!timer };
}

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

// src/components/user-action/ToggleableInput.tsx
var import_jsx_runtime = require("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] = (0, import_react2.useState)(initialState !== "display");
  const { restartTimer, clearTimer } = useDelay(saveDelayOptions);
  const ref = (0, import_react2.useRef)(null);
  const onEditCompletedWrapper = (text) => {
    onEditCompleted(text);
    clearTimer();
  };
  (0, import_react2.useEffect)(() => {
    if (isEditing) {
      ref.current?.focus();
    }
  }, [isEditing]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
      "div",
      {
        className: (0, import_clsx.default)("row items-center w-full gap-x-2 overflow-hidden", { "cursor-pointer": !isEditing }),
        onClick: () => !isEditing ? setIsEditing(!isEditing) : void 0,
        children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: (0, import_clsx.default)("row overflow-hidden", { "flex-1": isEditing }), children: isEditing ? /* @__PURE__ */ (0, import_jsx_runtime.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: (0, import_clsx.default)(`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__ */ (0, import_jsx_runtime.jsx)("span", { className: (0, import_clsx.default)("max-w-xs break-words overflow-hidden", labelClassName), children: value }) }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
            import_lucide_react.Pencil,
            {
              className: (0, import_clsx.default)(`cursor-pointer`, { "text-transparent": isEditing }),
              size,
              style: { minWidth: `${size}px` }
            }
          )
        ]
      }
    ),
    isEditing && disclaimer && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("label", { className: "text-negative", children: disclaimer })
  ] });
};
var ToggleableInputUncontrolled = ({
  value: initialValue,
  onChangeText = noop,
  ...restProps
}) => {
  const [value, setValue] = (0, import_react2.useState)(initialValue);
  (0, import_react2.useEffect)(() => {
    setValue(initialValue);
  }, [initialValue]);
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    ToggleableInput,
    {
      value,
      onChangeText: (text) => {
        setValue(text);
        onChangeText(text);
      },
      ...restProps
    }
  );
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ToggleableInput,
  ToggleableInputUncontrolled
});
//# sourceMappingURL=ToggleableInput.js.map