"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useMediaQuery_exports = {};
__export(useMediaQuery_exports, {
  useMediaQuery: () => useMediaQuery
});
module.exports = __toCommonJS(useMediaQuery_exports);
var import_react = require("react");
var import_controls = require("@makeswift/controls");
var import_use_breakpoints = require("../../runtimes/react/hooks/use-breakpoints");
const getDeviceQueries = (breakpoints) => breakpoints.map((device) => ({
  id: device.id,
  query: (0, import_controls.getBreakpointMediaQuery)(device).replace("@media", "")
}));
function useMediaQuery(responsiveValue) {
  const breakpoints = (0, import_use_breakpoints.useBreakpoints)();
  const baseBreakpointId = (0, import_controls.getBaseBreakpoint)(breakpoints).id;
  const subscribe = (0, import_react.useCallback)(
    (notify) => {
      const cleanUpFns = getDeviceQueries(breakpoints).map((q) => {
        const mediaQueryList = window.matchMedia(q.query);
        mediaQueryList.addEventListener("change", notify);
        return () => mediaQueryList.removeEventListener("change", notify);
      });
      return () => cleanUpFns.forEach((fn) => fn());
    },
    [breakpoints]
  );
  const getServerSnapshot = () => (0, import_controls.findBreakpointOverride)(breakpoints, responsiveValue, baseBreakpointId)?.value;
  function getSnapshot() {
    const deviceId = getDeviceQueries(breakpoints).reduce(
      (matchedDevice, deviceQueries) => {
        if (window.matchMedia(deviceQueries.query).matches) {
          return deviceQueries.id;
        }
        return matchedDevice;
      },
      baseBreakpointId
    );
    return (0, import_controls.findBreakpointOverride)(breakpoints, responsiveValue, deviceId)?.value;
  }
  return (0, import_react.useSyncExternalStore)(subscribe, getSnapshot, getServerSnapshot);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useMediaQuery
});
//# sourceMappingURL=useMediaQuery.js.map