"use strict";
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);
var useDebouncedCallback_stories_exports = {};
__export(useDebouncedCallback_stories_exports, {
  Test: () => Test,
  default: () => useDebouncedCallback_stories_default
});
module.exports = __toCommonJS(useDebouncedCallback_stories_exports);
var import_react = __toESM(require("react"));
var import_test = require("@storybook/test");
var import__ = require(".");
const callback1 = (0, import_test.fn)();
const callback2 = (0, import_test.fn)();
const meta = {
  component: Template,
  title: "PreLdls/Hooks/useDebouncedCallback",
  tags: ["!autodocs"],
  args: {
    delay: 100,
    callback1,
    callback2
  }
};
var useDebouncedCallback_stories_default = meta;
const Test = {
  name: "useDebouncedCallback",
  play: async ({ canvasElement, step }) => {
    const canvas = (0, import_test.within)(canvasElement);
    const input = canvas.getByRole("textbox");
    const callback2Btn = canvas.getByText("Use Callback 2");
    await step("Debounce callback calls", async () => {
      await import_test.userEvent.type(input, "Write something");
      (0, import_test.expect)(input).toHaveValue("Write something");
      (0, import_test.expect)(callback1).not.toHaveBeenCalled();
      await (0, import_test.waitFor)(() => (0, import_test.expect)(callback1).toHaveBeenCalledTimes(1), { timeout: 200 });
      (0, import_test.expect)(callback1).toHaveBeenCalledWith("Write something");
    });
    await step("Switch callback function", async () => {
      await import_test.userEvent.type(input, " else");
      await import_test.userEvent.click(callback2Btn);
      await import_test.userEvent.type(input, ".");
      await (0, import_test.waitFor)(() => (0, import_test.expect)(callback2).toHaveBeenCalledTimes(1), { timeout: 200 });
      (0, import_test.expect)(callback1).not.toHaveBeenCalledWith(/^Write something else.*/);
      (0, import_test.expect)(callback2).toHaveBeenCalledWith("Write something else.");
    });
  }
};
function Template({ callback1: callback12, callback2: callback22, delay }) {
  const [callback, setCallback] = (0, import_react.useState)(() => callback12);
  const debouncedCallback = (0, import__.useDebouncedCallback)(callback, delay);
  const [value, setValue] = (0, import_react.useState)("");
  return /* @__PURE__ */ import_react.default.createElement("div", null, /* @__PURE__ */ import_react.default.createElement(
    "input",
    {
      type: "text",
      value,
      onChange: (event) => {
        const value2 = event.target.value;
        setValue(value2);
        debouncedCallback == null ? void 0 : debouncedCallback(value2);
      }
    }
  ), " ", /* @__PURE__ */ import_react.default.createElement("button", { onClick: () => setCallback(() => callback12) }, "Use Callback 1"), " ", /* @__PURE__ */ import_react.default.createElement("button", { onClick: () => setCallback(() => callback22) }, "Use Callback 2"));
}
//# sourceMappingURL=useDebouncedCallback.stories.js.map
