import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  require_index_node,
  require_react,
  require_verror
} from "./chunk-ESTYQVMX.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-4EFJZ3GQ.js";

// ../cli-debug-log/src/index.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
var import_verror2 = __toESM(require_verror(), 1);
import { dirname } from "node:path";
import { appendFileSync, mkdirSync } from "node:fs";

// ../cli-http-client/src/extract-error.ts
init_esm_shims();
var import_http_client = __toESM(require_index_node(), 1);
var import_verror = __toESM(require_verror(), 1);
function extractHttpError(error) {
  if (import_http_client.HttpClient.isHttpError(error)) {
    return error;
  }
  const nestedError = error instanceof Error ? import_verror.default.cause(error) : null;
  return nestedError ? extractHttpError(nestedError) : null;
}

// ../cli-http-client/src/error-info.ts
init_esm_shims();
function getHttpErrorInfo(httpError) {
  return {
    request: {
      baseURL: httpError.config.baseURL,
      url: httpError.config.url,
      method: httpError.config.method,
      headers: httpError.config.headers,
      data: httpError.config.data
    },
    response: {
      code: httpError.code,
      message: httpError.message,
      status: httpError.response?.status,
      headers: httpError.response?.headers,
      data: httpError.response?.data
    }
  };
}

// ../cli-debug-log/src/index.tsx
function writeCrashReport(reportPath, error, occurredAt) {
  try {
    let content = `----- CLI crash report at ${occurredAt.toISOString()}
`;
    if (error instanceof Error) {
      content += `Error Trace
`;
      content += `${import_verror2.default.fullStack(error)}
`;
      content += `Error Info
`;
      content += `${JSON.stringify(import_verror2.default.info(error), null, 2)}
`;
      const httpError = extractHttpError(error);
      if (httpError) {
        content += `Request Info
`;
        content += `${JSON.stringify(getHttpErrorInfo(httpError), null, 2)}
`;
      }
    }
    mkdirSync(dirname(reportPath), { recursive: true });
    appendFileSync(reportPath, content);
  } catch {
  }
}
var DebugLogContext = (0, import_react.createContext)(null);
var DebugLogProvider = ({ filePath, children }) => {
  const writeToDebugLog = (0, import_react.useCallback)(
    (error) => writeCrashReport(filePath, error, /* @__PURE__ */ new Date()),
    [filePath]
  );
  return /* @__PURE__ */ import_react.default.createElement(DebugLogContext.Provider, { value: writeToDebugLog }, children);
};
var useDebugLog = () => {
  const context = (0, import_react.useContext)(DebugLogContext);
  if (context == null) {
    throw new Error(
      "useDebugLog() cannot be used without an <DebugLogProvider>"
    );
  }
  return context;
};

export {
  DebugLogProvider,
  useDebugLog
};
//# sourceMappingURL=chunk-IIF6P6XG.js.map