'use strict';

var react = require('react');

// src/shared/hooks/useMediaContents.ts
function useMediaContents(media, transform, cleanup) {
  const [data, setData] = react.useState(void 0);
  const [loading, setLoading] = react.useState(true);
  const [error, setError] = react.useState(void 0);
  const cleanupRef = react.useRef(cleanup);
  cleanupRef.current = cleanup;
  const dataRef = react.useRef(void 0);
  dataRef.current = data;
  react.useEffect(function fetchMediaContents() {
    let cancelled = false;
    setLoading(true);
    setError(void 0);
    setData((prev) => {
      if (prev !== void 0) {
        cleanupRef.current?.(prev);
      }
      return void 0;
    });
    media.fetchContents().then((response) => transform(response)).then((result) => {
      if (!cancelled) {
        setData(result);
        setLoading(false);
      } else {
        cleanupRef.current?.(result);
      }
    }).catch((err) => {
      if (!cancelled) {
        setError(err instanceof Error ? err : new Error(String(err)));
        setLoading(false);
      }
    });
    return () => {
      cancelled = true;
    };
  }, [media, transform]);
  react.useEffect(() => {
    return () => {
      if (dataRef.current !== void 0) {
        cleanupRef.current?.(dataRef.current);
      }
    };
  }, []);
  return {
    data,
    loading,
    error
  };
}

exports.useMediaContents = useMediaContents;
//# sourceMappingURL=chunk-MVQKV5BJ.cjs.map
//# sourceMappingURL=chunk-MVQKV5BJ.cjs.map