import {
  TOOLPAD_COMPONENT
} from "./chunk-YVCDODUK.js";
import {
  __spreadProps,
  __spreadValues
} from "./chunk-Z67LV52D.js";

// ../toolpad-core/dist/useDataQuery.js
import * as React from "react";
import { useQuery } from "react-query";
async function fetchData(dataUrl, queryId, params) {
  const url = new URL(`./${encodeURIComponent(queryId)}`, new URL(dataUrl, window.location.href));
  url.searchParams.set("params", JSON.stringify(params));
  const res = await fetch(String(url));
  if (!res.ok) {
    throw new Error(`HTTP ${res.status} while fetching "${url}"`);
  }
  return res.json();
}
var INITIAL_DATA_QUERY = {
  isLoading: false,
  isFetching: false,
  error: null,
  data: null,
  rows: [],
  refetch: () => {
  }
};
var EMPTY_ARRAY = [];
var EMPTY_OBJECT = {};
function useDataQuery(dataUrl, queryId, params, options) {
  const { isLoading, isFetching, error, data: responseData = EMPTY_OBJECT, refetch } = useQuery([dataUrl, queryId, params], () => queryId && fetchData(dataUrl, queryId, params), __spreadProps(__spreadValues({}, options), {
    enabled: !!queryId
  }));
  const { data } = responseData;
  const rows = Array.isArray(data) ? data : EMPTY_ARRAY;
  const result = React.useMemo(() => ({
    isLoading,
    isFetching,
    error,
    data,
    rows,
    refetch
  }), [isLoading, isFetching, error, data, rows, refetch]);
  return result;
}

// ../toolpad-core/dist/useUrlQueryState.js
import * as React2 from "react";
function getLocationUrl() {
  var _a;
  return new URL(((_a = window.top) != null ? _a : window).location.href);
}
function useUrlQueryState(param, defaultValue = "") {
  const [value, setValue] = React2.useState(defaultValue);
  const isInitialized = React2.useRef(false);
  React2.useEffect(() => {
    const url = getLocationUrl();
    const currentUrlValue = url.searchParams.get(param);
    if (isInitialized.current) {
      if (currentUrlValue !== value) {
        if (currentUrlValue === defaultValue) {
          url.searchParams.delete(param);
        } else {
          url.searchParams.set(param, value);
        }
        window.history.pushState(null, "", String(url));
      }
    } else {
      if (typeof currentUrlValue === "string") {
        setValue(currentUrlValue);
      }
      isInitialized.current = true;
    }
  }, [param, value, defaultValue]);
  React2.useEffect(() => {
    const handlePopState = () => {
      const url = getLocationUrl();
      const urlQueryValue = url.searchParams.get(param);
      setValue(urlQueryValue != null ? urlQueryValue : defaultValue);
    };
    window.addEventListener("popstate", handlePopState);
    return () => {
      window.removeEventListener("popstate", handlePopState);
    };
  }, [param, defaultValue]);
  return [value, setValue];
}

// ../toolpad-core/dist/index.js
function createComponent(Component, config) {
  return Object.assign(Component, {
    [TOOLPAD_COMPONENT]: config || { argTypes: {} }
  });
}

export {
  INITIAL_DATA_QUERY,
  useDataQuery,
  useUrlQueryState,
  createComponent
};
