import { renderHook, act } from "testUtilities";
import { useTouchedState } from "./use-touched-state";
describe("useTouchedState", function () {
  it("returns initial touched state as false by default", function () {
    var _renderHook = renderHook(function () {
        return useTouchedState({});
      }),
      result = _renderHook.result;
    var _result$current = result.current,
      touchedState = _result$current[0];
    expect(touchedState).toBe(false);
  });
  it("returns custom default state", function () {
    var _renderHook2 = renderHook(function () {
        return useTouchedState({
          defaultState: true
        });
      }),
      result = _renderHook2.result;
    var _result$current2 = result.current,
      touchedState = _result$current2[0];
    expect(touchedState).toBe(true);
  });
  it("sets touched state to true on first blur", function () {
    var _renderHook3 = renderHook(function () {
        return useTouchedState({});
      }),
      result = _renderHook3.result;
    var _result$current3 = result.current,
      handleBlur = _result$current3[1];
    act(function () {
      handleBlur({
        type: "blur"
      });
    });
    expect(result.current[0]).toBe(true);
  });
  it("calls onBlur callback when provided", function () {
    var onBlur = jest.fn();
    var _renderHook4 = renderHook(function () {
        return useTouchedState({
          onBlur: onBlur
        });
      }),
      result = _renderHook4.result;
    var _result$current4 = result.current,
      handleBlur = _result$current4[1];
    var mockEvent = {
      type: "blur"
    };
    act(function () {
      handleBlur(mockEvent);
    });
    expect(onBlur).toHaveBeenCalledWith(mockEvent);
  });
  it("does not update touched state when already touched", function () {
    var onBlur = jest.fn();
    var _renderHook5 = renderHook(function () {
        return useTouchedState({
          onBlur: onBlur,
          defaultState: true
        });
      }),
      result = _renderHook5.result;
    var _result$current5 = result.current,
      handleBlur = _result$current5[1];
    act(function () {
      handleBlur({
        type: "blur"
      });
    });
    expect(result.current[0]).toBe(true);
    expect(onBlur).toHaveBeenCalledTimes(1);
  });
  it("allows manual control of touched state", function () {
    var _renderHook6 = renderHook(function () {
        return useTouchedState({});
      }),
      result = _renderHook6.result;
    var _result$current6 = result.current,
      setTouchedState = _result$current6[2];
    act(function () {
      setTouchedState(true);
    });
    expect(result.current[0]).toBe(true);
    act(function () {
      setTouchedState(false);
    });
    expect(result.current[0]).toBe(false);
  });
  it("maintains stable function references", function () {
    var _renderHook7 = renderHook(function () {
        return useTouchedState({});
      }),
      result = _renderHook7.result,
      rerender = _renderHook7.rerender;
    var _result$current7 = result.current,
      initialHandleBlur = _result$current7[1],
      initialSetTouchedState = _result$current7[2];
    rerender();
    var _result$current8 = result.current,
      newHandleBlur = _result$current8[1],
      newSetTouchedState = _result$current8[2];
    expect(newHandleBlur).toBe(initialHandleBlur);
    expect(newSetTouchedState).toBe(initialSetTouchedState);
  });
  it("updates callback when onBlur dependency changes", function () {
    var onBlur = jest.fn();
    var _renderHook8 = renderHook(function (_ref) {
        var onBlur = _ref.onBlur;
        return useTouchedState({
          onBlur: onBlur
        });
      }, {
        initialProps: {
          onBlur: onBlur
        }
      }),
      result = _renderHook8.result,
      rerender = _renderHook8.rerender;
    var newOnBlur = jest.fn();
    rerender({
      onBlur: newOnBlur
    });
    var _result$current9 = result.current,
      handleBlur = _result$current9[1];
    act(function () {
      handleBlur({
        type: "blur"
      });
    });
    expect(onBlur).not.toHaveBeenCalled();
    expect(newOnBlur).toHaveBeenCalledTimes(1);
  });
  it("handles multiple blur events correctly", function () {
    var onBlur = jest.fn();
    var _renderHook9 = renderHook(function () {
        return useTouchedState({
          onBlur: onBlur
        });
      }),
      result = _renderHook9.result;
    var _result$current0 = result.current,
      handleBlur = _result$current0[1];
    act(function () {
      handleBlur({
        type: "blur"
      });
    });
    act(function () {
      handleBlur({
        type: "blur"
      });
    });
    expect(result.current[0]).toBe(true);
    expect(onBlur).toHaveBeenCalledTimes(2);
  });
});