import { jest } from "@jest/globals";
import { act } from "@testing-library/react";
import { DEFAULT_DESKTOP_LARGE_MIN_WIDTH, DEFAULT_DESKTOP_MIN_WIDTH, DEFAULT_PHONE_MAX_WIDTH, DEFAULT_TABLET_MIN_WIDTH } from "../media-queries/appSize.js";
const noop = ()=>{
// do nothing
};
/**
 * @see {@link spyOnMatchMedia} instead
 * @internal
 * @since 6.0.0
 */ export const BASE_MEDIA_QUERY_LIST = {
    media: "",
    matches: false,
    onchange: noop,
    addListener: noop,
    addEventListener: noop,
    removeEventListener: noop,
    removeListener: noop,
    dispatchEvent: ()=>false
};
/**
 * @see {@link spyOnMatchMedia} for usage
 * @since 6.0.0
 * @returns `true` for phone media queries
 */ export const matchPhone = (query)=>query.includes(`${DEFAULT_PHONE_MAX_WIDTH}`);
/**
 * @see {@link spyOnMatchMedia} for usage
 * @since 6.0.0
 * @returns `true` for tablet media queries
 */ export const matchTablet = (query)=>query.includes(`${DEFAULT_TABLET_MIN_WIDTH}`);
/**
 * @see {@link spyOnMatchMedia} for usage
 * @since 6.0.0
 * @returns `true` for desktop media queries
 */ export const matchDesktop = (query)=>query.includes(`${DEFAULT_DESKTOP_MIN_WIDTH}`);
/**
 * @see {@link spyOnMatchMedia} for usage
 * @since 6.0.0
 * @returns `true` for large desktop media queries
 */ export const matchLargeDesktop = (query)=>query.includes(`${DEFAULT_DESKTOP_LARGE_MIN_WIDTH}`);
/**
 * @see {@link spyOnMatchMedia} for usage
 * @since 6.0.0
 * @returns `true` for both desktop and large desktop media queries
 */ export const matchAnyDesktop = (query)=>matchDesktop(query) || matchLargeDesktop(query);
/**
 * @example Default Behavior
 * ```tsx
 * import { matchPhone, render, spyOnMatchMedia } from "@react-md/test-utils";
 *
 * const matchMedia = spyOnMatchMedia();
 * render(<Test />);
 *
 * // expect desktop results
 *
 * matchMedia.changeViewport(matchPhone);
 * // expect phone results
 * ```
 *
 * @example Set Default Media
 * ```tsx
 * import { matchPhone, render, spyOnMatchMedia } from "@react-md/test-utils";
 *
 * const matchMedia = spyOnMatchMedia(matchPhone);
 * render(<Test />);
 *
 * // expect phone results
 * ```
 *
 * @since 6.0.0
 */ export function spyOnMatchMedia(defaultMatch = matchDesktop) {
    const listeners = new Map();
    const matchMedia = jest.spyOn(window, "matchMedia").mockImplementation((query)=>({
            ...BASE_MEDIA_QUERY_LIST,
            addEventListener (type, listener) {
                /* c8 ignore start */ if (typeof listener !== "function" || type !== "change") {
                    return;
                }
                /* c8 ignore stop */ listeners.set(query, listener);
            },
            removeEventListener (type, listener) {
                /* c8 ignore start */ if (typeof listener !== "function" || type !== "change") {
                    return;
                }
                /* c8 ignore stop */ listeners.delete(query);
            },
            matches: defaultMatch(query)
        }));
    const changeViewport = (matcher, disableAct = false)=>{
        const update = ()=>{
            window.dispatchEvent(new Event("resize"));
            const event = new Event("change");
            listeners.forEach((listener, query)=>{
                listener({
                    ...event,
                    media: "",
                    matches: matcher(query)
                });
            });
        };
        if (disableAct) {
            update();
        } else {
            act(update);
        }
    };
    const mock = matchMedia;
    mock.changeViewport = changeViewport;
    return mock;
}

//# sourceMappingURL=matchMedia.js.map