import {
  cliFetchRequestSchema
} from "./chunk-WUNL7OYW.js";
import "./chunk-BQRFNAMV.js";
import {
  executeFetch
} from "./chunk-Q5SPIJGE.js";
import "./chunk-GKYODP7W.js";
import "./chunk-IKPLMFAK.js";
import "./chunk-LNJIXYCU.js";
import "./chunk-DJLERI6R.js";
import {
  getWalletOrExit
} from "./chunk-QOYH6AOG.js";
import "./chunk-HFEYSQRZ.js";
import "./chunk-NPJV7AMV.js";
import "./chunk-LNQRIXFK.js";
import "./chunk-Y4VZACTC.js";
import "./chunk-EMQMCXIC.js";
import "./chunk-VDBUUHJF.js";
import "./chunk-ITCDZXBZ.js";
import {
  safeParseResponse
} from "./chunk-QZBSRYSW.js";
import "./chunk-NROPRHIC.js";
import "./chunk-FB5CMO3J.js";
import {
  errorResponse,
  fromNeverthrowError,
  outputAndExit,
  successResponse
} from "./chunk-7EBJ4BCH.js";
import "./chunk-C7KWYVS3.js";

// src/cli/commands/fetch.ts
var SURFACE = "cli:fetch";
var fetchCommand = async (input) => {
  const requestInput = cliFetchRequestSchema.safeParse(input);
  if (!requestInput.success) {
    return outputAndExit(
      errorResponse({
        code: "INVALID_INPUT",
        message: requestInput.error.message,
        surface: SURFACE,
        cause: "validation"
      }),
      input
    );
  }
  const wallets = await getWalletOrExit(input);
  const fetchResult = await executeFetch(requestInput.data, {
    surface: SURFACE,
    wallets,
    flags: input,
    params: input
  });
  if (fetchResult.isErr()) {
    return outputAndExit(fromNeverthrowError(fetchResult), input);
  }
  const { response, paymentInfo } = fetchResult.value;
  if (!response.ok) {
    const parseResult = await safeParseResponse(SURFACE, response);
    return outputAndExit(
      errorResponse({
        code: "HTTP_ERROR",
        message: response.statusText,
        surface: SURFACE,
        cause: "http",
        details: parseResult.match(
          (data2) => ({
            statusCode: response.status,
            type: data2.type,
            body: data2.type === "json" || data2.type === "text" ? data2.data : void 0
          }),
          () => ({ statusCode: response.status })
        )
      }),
      input
    );
  }
  const parseResponseResult = await safeParseResponse(SURFACE, response);
  if (parseResponseResult.isErr()) {
    return outputAndExit(fromNeverthrowError(parseResponseResult), input);
  }
  const parsedResponse = parseResponseResult.value;
  const data = parsedResponse.type === "json" || parsedResponse.type === "text" ? parsedResponse.data : { type: parsedResponse.type };
  outputAndExit(successResponse(data, paymentInfo), input);
};
export {
  fetchCommand
};
//# sourceMappingURL=fetch-5H6NJNO5.js.map