"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const index = require("../shared/dist-index.cjs");
const index$1 = require("../../uploadthing/client-index.cjs");
const React = require("react");
var peerDependencies = {
  "uploadthing": "^7.2.0"
};
const noop$1 = () => void 0;
const useInsertionEffect = typeof window !== "undefined" ? React.useInsertionEffect : noop$1;
function useEvent(callback) {
  const latestRef = React.useRef(useEvent_shouldNotBeInvokedBeforeMount);
  useInsertionEffect(() => {
    latestRef.current = callback;
  }, [callback]);
  const stableRef = React.useRef(null);
  stableRef.current ??= function() {
    return latestRef.current.apply(this, arguments);
  };
  return stableRef.current;
}
function useEvent_shouldNotBeInvokedBeforeMount() {
  throw new Error("INVALID_USEEVENT_INVOCATION: the callback from useEvent cannot be invoked before the component has mounted.");
}
function useFetch(fetch, url, options) {
  const cache = React.useRef({});
  const cancelRequest = React.useRef(false);
  const initialState$1 = {
    error: void 0,
    data: void 0
  };
  const fetchReducer = (state$1, action) => {
    switch (action.type) {
      case "loading":
        return { ...initialState$1 };
      case "fetched":
        return {
          ...initialState$1,
          data: action.payload
        };
      case "error":
        return {
          ...initialState$1,
          error: action.payload
        };
      default:
        return state$1;
    }
  };
  const [state, dispatch] = React.useReducer(fetchReducer, initialState$1);
  React.useEffect(() => {
    if (!url) return;
    cancelRequest.current = false;
    const fetchData = async () => {
      dispatch({ type: "loading" });
      if (cache.current[url]) {
        dispatch({
          type: "fetched",
          payload: cache.current[url]
        });
        return;
      }
      try {
        const response = await fetch(url, options);
        if (!response.ok) throw new Error(response.statusText);
        const dataOrError = await index.safeParseJSON(response);
        if (dataOrError instanceof Error) throw dataOrError;
        cache.current[url] = dataOrError;
        if (cancelRequest.current) return;
        dispatch({
          type: "fetched",
          payload: dataOrError
        });
      } catch (error) {
        if (cancelRequest.current) return;
        dispatch({
          type: "error",
          payload: error
        });
      }
    };
    fetchData();
    return () => {
      cancelRequest.current = true;
    };
  }, [url]);
  return state;
}
var useFetch_default = useFetch;
const useRouteConfig = (fetch, url, endpoint) => {
  const maybeServerData = globalThis.__UPLOADTHING;
  const { data } = useFetch_default(fetch, maybeServerData ? void 0 : url.href);
  return (maybeServerData ?? data)?.find((x) => x.slug === endpoint)?.config;
};
function useUploadThingInternal(url, endpoint, fetch, opts) {
  const progressGranularity = opts?.uploadProgressGranularity ?? "coarse";
  const { uploadFiles, routeRegistry } = index$1.genUploader({
    fetch,
    url,
    package: "@uploadthing/react"
  });
  const [isUploading, setUploading] = React.useState(false);
  const uploadProgress = React.useRef(0);
  const fileProgress = React.useRef(/* @__PURE__ */ new Map());
  const startUpload = useEvent(async (...args) => {
    const files = await opts?.onBeforeUploadBegin?.(args[0]) ?? args[0];
    const input = args[1];
    setUploading(true);
    files.forEach((f) => fileProgress.current.set(f, 0));
    opts?.onUploadProgress?.(0);
    try {
      const res = await uploadFiles(endpoint, {
        signal: opts?.signal,
        headers: opts?.headers,
        files,
        onUploadProgress: (progress) => {
          if (!opts?.onUploadProgress) return;
          fileProgress.current.set(progress.file, progress.progress);
          let sum = 0;
          fileProgress.current.forEach((p) => {
            sum += p;
          });
          const averageProgress = index.roundProgress(Math.min(100, sum / fileProgress.current.size), progressGranularity);
          if (averageProgress !== uploadProgress.current) {
            opts.onUploadProgress(averageProgress);
            uploadProgress.current = averageProgress;
          }
        },
        onUploadBegin({ file }) {
          if (!opts?.onUploadBegin) return;
          opts.onUploadBegin(file);
        },
        input
      });
      await opts?.onClientUploadComplete?.(res);
      return res;
    } catch (e) {
      if (e instanceof index.UploadAbortedError) throw e;
      let error;
      if (e instanceof index.UploadThingError) error = e;
      else {
        error = index.INTERNAL_DO_NOT_USE__fatalClientError(e);
        console.error("Something went wrong. Please contact UploadThing and provide the following cause:", error.cause instanceof Error ? error.cause.toString() : error.cause);
      }
      await opts?.onUploadError?.(error);
    } finally {
      setUploading(false);
      fileProgress.current = /* @__PURE__ */ new Map();
      uploadProgress.current = 0;
    }
  });
  const _endpoint = index.unwrap(endpoint, routeRegistry);
  const routeConfig = useRouteConfig(fetch, url, _endpoint);
  return {
    startUpload,
    isUploading,
    routeConfig
  };
}
const __useUploadThingInternal = useUploadThingInternal;
const generateReactHelpers = (initOpts) => {
  index.warnIfInvalidPeerDependency("@uploadthing/react", peerDependencies.uploadthing, index$1.version);
  const fetch = initOpts?.fetch ?? globalThis.fetch;
  const url = index.resolveMaybeUrlArg(initOpts?.url);
  const clientHelpers = index$1.genUploader({
    fetch,
    url,
    package: "@uploadthing/react"
  });
  function useUploadThing(endpoint, opts) {
    return __useUploadThingInternal(url, endpoint, fetch, opts);
  }
  function getRouteConfig(slug) {
    const maybeServerData = globalThis.__UPLOADTHING;
    const endpoint = index.unwrap(slug, clientHelpers.routeRegistry);
    const config = maybeServerData?.find((x) => x.slug === endpoint)?.config;
    if (!config) throw new Error(`No config found for endpoint "${endpoint.toString()}". Please make sure to use the NextSSRPlugin in your Next.js app.`);
    return config;
  }
  return {
    useUploadThing,
    ...clientHelpers,
    getRouteConfig
  };
};
exports.__useUploadThingInternal = __useUploadThingInternal;
exports.generateReactHelpers = generateReactHelpers;
exports.peerDependencies = peerDependencies;
exports.useEvent = useEvent;
//# sourceMappingURL=dist-use-uploadthing-pxkJ3LFs.cjs.map
