"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }"use client";

// src/hooks/use-self-fetch.ts
var _react = require('react');
function useSelfFetch(url, options) {
  const initialData = _optionalChain([options, 'optionalAccess', _ => _.initialData]);
  const [data, setData] = _react.useState.call(void 0, _nullishCoalesce(initialData, () => ( null)));
  const [isLoading, setIsLoading] = _react.useState.call(void 0, initialData === void 0 && url !== null);
  const [error, setError] = _react.useState.call(void 0, false);
  const [reloadKey, setReloadKey] = _react.useState.call(void 0, 0);
  const dataUrlRef = _react.useRef.call(void 0, initialData !== void 0 ? url : null);
  _react.useEffect.call(void 0, () => {
    if (initialData !== void 0) setData(initialData);
  }, [initialData]);
  _react.useEffect.call(void 0, () => {
    if (url === null) {
      setIsLoading(false);
      return;
    }
    if (dataUrlRef.current === url) return;
    const ctrl = new AbortController();
    let cancelled = false;
    async function load() {
      try {
        setIsLoading(true);
        setError(false);
        const res = await fetch(url, { signal: ctrl.signal });
        if (!res.ok) throw new Error(`Request failed (${res.status})`);
        const json = await res.json();
        if (!cancelled) {
          setData(json);
          dataUrlRef.current = url;
        }
      } catch (err) {
        if (cancelled || _optionalChain([err, 'optionalAccess', _2 => _2.name]) === "AbortError") return;
        setError(true);
        console.error("useSelfFetch:", url, err);
      } finally {
        if (!cancelled) setIsLoading(false);
      }
    }
    load();
    return () => {
      cancelled = true;
      ctrl.abort();
    };
  }, [url, reloadKey]);
  return {
    data,
    setData,
    isLoading,
    error,
    // Force a re-fetch of the current url: clear the held-url ref so the skip above
    // doesn't short-circuit, then bump the effect key.
    reload: () => {
      dataUrlRef.current = null;
      setReloadKey((k) => k + 1);
    }
  };
}



exports.useSelfFetch = useSelfFetch;
//# sourceMappingURL=chunk-T7AJBKZP.cjs.map