import { jsx as _jsx } from "react/jsx-runtime";
import { fireEvent, render } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { FormProvider, useForm } from "react-hook-form";
import { describe, expect, it, vi } from "vitest";
import { ValueTransformer } from "./value-transformer.js";
import { withFieldValidation } from "../with-field-validation/with-field-validation.js";
const TestWrapper = ({ children }) => {
    const methods = useForm();
    return _jsx(FormProvider, { ...methods, children: children });
};
const TestInputRaw = ({ onChange, onBlur, value = "", name = "test", }) => {
    return (_jsx("input", { "data-testid": "test-input", onChange: onChange, onBlur: onBlur, value: value, name: name }));
};
const TestInput = withFieldValidation(TestInputRaw);
describe("ValueTransformer", () => {
    it("should apply transformer on blur by default", () => {
        const onChange = vi.fn();
        const { getByTestId } = render(_jsx(TestWrapper, { children: _jsx(ValueTransformer, { transformers: [
                    {
                        transformer: (value) => value?.trim(),
                    },
                ], children: _jsx(TestInput, { onChange: onChange, name: "test" }) }) }));
        const input = getByTestId("test-input");
        fireEvent.change(input, { target: { value: "  test  " } });
        fireEvent.blur(input);
        expect(input.value).toBe("test");
    });
    it("should apply transformer on change when specified", async () => {
        const onChange = vi.fn();
        const user = userEvent.setup();
        const { getByTestId } = render(_jsx(TestWrapper, { children: _jsx(ValueTransformer, { transformers: [
                    {
                        transformer: (value) => value?.toUpperCase(),
                        options: {
                            trigger: "change",
                        },
                    },
                ], children: _jsx(TestInput, { onChange: onChange, name: "test" }) }) }));
        const input = getByTestId("test-input");
        await user.type(input, "test");
        expect(input.value).toBe("TEST");
    });
    it("should not apply transformer when if condition is false", () => {
        const { getByTestId } = render(_jsx(TestWrapper, { children: _jsx(ValueTransformer, { transformers: [
                    {
                        transformer: (value) => value.toUpperCase(),
                        options: {
                            if: false,
                            trigger: "change",
                        },
                    },
                ], children: _jsx(TestInput, { name: "test" }) }) }));
        const input = getByTestId("test-input");
        fireEvent.change(input, { target: { value: "test" } });
        expect(input.value).toBe("test");
    });
    it("should apply transformers on mount", () => {
        const { getByTestId } = render(_jsx(TestWrapper, { children: _jsx(ValueTransformer, { transformers: [
                    {
                        transformer: (value) => value?.toUpperCase(),
                    },
                ], children: _jsx(TestInput, { value: "test", name: "test" }) }) }));
        const input = getByTestId("test-input");
        expect(input.value).toBe("TEST");
    });
    it("should handle array of transformer functions", () => {
        const { getByTestId } = render(_jsx(TestWrapper, { children: _jsx(ValueTransformer, { transformers: [
                    (value) => value?.trim(),
                    (value) => value?.toUpperCase(),
                ], children: _jsx(TestInput, { name: "test" }) }) }));
        const input = getByTestId("test-input");
        fireEvent.change(input, { target: { value: "  test  " } });
        fireEvent.blur(input);
        expect(input.value).toBe("TEST");
    });
    it("should not trigger change event if transformed value is the same", () => {
        const onChange = vi.fn();
        const { getByTestId } = render(_jsx(TestWrapper, { children: _jsx(ValueTransformer, { transformers: [
                    {
                        transformer: (value) => value, // Identity transformer
                    },
                ], children: _jsx(TestInput, { onChange: onChange, name: "test" }) }) }));
        const input = getByTestId("test-input");
        fireEvent.change(input, { target: { value: "test" } });
        fireEvent.blur(input);
        expect(onChange).toHaveBeenCalledTimes(1); // Only the initial change
    });
    it("should preserve original event handlers", async () => {
        const onChangeMock = vi.fn();
        const onBlurMock = vi.fn();
        const { getByTestId } = render(_jsx(TestWrapper, { children: _jsx(ValueTransformer, { transformers: [
                    {
                        transformer: (value) => value?.toUpperCase(),
                        options: { trigger: "change" },
                    },
                ], children: _jsx(TestInput, { onChange: onChangeMock, onBlur: onBlurMock, name: "test", value: "" }) }) }));
        const input = getByTestId("test-input");
        await userEvent.type(input, "test");
        await userEvent.tab(); // to trigger blur
        expect(onChangeMock).toHaveBeenCalled();
        expect(onBlurMock).toHaveBeenCalled();
    });
});
//# sourceMappingURL=value-transformer.test.js.map