import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  ProjectModelProvider,
  loadProjectModel
} from "./chunk-RDF2RA4E.js";
import {
  HttpClientProvider,
  buildHttpClientHeaders
} from "./chunk-WFQIXGJV.js";
import {
  PanoramaProvider,
  createCommandTransaction
} from "./chunk-ECBO3UFC.js";
import {
  addUserFields,
  reportCommandStartEvent
} from "./chunk-VC5Q2AI5.js";
import {
  AuthProvider,
  BiProvider,
  DebugLogProvider,
  ErrorReporterProvider
} from "./chunk-HWT3LUJJ.js";
import {
  render
} from "./chunk-YL4347CI.js";
import {
  require_react
} from "./chunk-TODKQSCF.js";
import {
  getDebugLogFilePath
} from "./chunk-YT5BPQDJ.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-EXLZF52D.js";

// ../cli-astro-commands/src/commands/render-command.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
import { cwd } from "node:process";
function getFlow(model) {
  return model.config.projectType === "App" ? "Astro_App" : "Headless";
}
async function initServices({
  model,
  command,
  flow,
  cliVersion,
  services
}) {
  const { biLogger, errorReporter, panorama } = services;
  errorReporter.setTag("project", flow);
  errorReporter.setTag("project.app_id", model.config.appId);
  panorama.setDefaultClientData({
    cliProject: flow,
    cliAppId: model.config.appId
  });
  if (model.config.projectType === "App" && model.config.projectId != null) {
    errorReporter.setTag("project.id", model.config.projectId);
    panorama.setDefaultClientData({ cliProjectId: model.config.projectId });
  }
  if (model.config.projectType === "Site") {
    errorReporter.setTag("project.site_id", model.config.siteId);
    panorama.setDefaultClientData({ cliSiteId: model.config.siteId });
  }
  biLogger.updateDefaults({
    flow,
    cliVersion,
    command: command.name(),
    _appId: model.config.appId
  });
}
async function renderCommand(command, services, cliVersion, tree) {
  await runCommand(command, services, cliVersion, async (model) => {
    const { biLogger, errorReporter, panorama } = services;
    const debugLogFilePath = getDebugLogFilePath(model.projectFolder);
    const handleLogin = (userInfo) => {
      errorReporter.setUser({ id: userInfo.userId });
      addUserFields(biLogger, userInfo.userId);
    };
    const headers = buildHttpClientHeaders({
      flow: getFlow(model),
      command: command.name(),
      cliVersion
    });
    await render(
      /* @__PURE__ */ import_react.default.createElement(BiProvider, { value: biLogger }, /* @__PURE__ */ import_react.default.createElement(ErrorReporterProvider, { value: errorReporter }, /* @__PURE__ */ import_react.default.createElement(PanoramaProvider, { value: panorama }, /* @__PURE__ */ import_react.default.createElement(AuthProvider, { onLogin: handleLogin }, /* @__PURE__ */ import_react.default.createElement(HttpClientProvider, { headers }, /* @__PURE__ */ import_react.default.createElement(DebugLogProvider, { filePath: debugLogFilePath }, /* @__PURE__ */ import_react.default.createElement(ProjectModelProvider, { value: model }, tree)))))))
    );
  });
}
async function runCommand(command, services, cliVersion, commandFn) {
  const model = await loadProjectModel(cwd());
  const flow = getFlow(model);
  await initServices({ model, command, flow, services, cliVersion });
  const { panorama, biLogger } = services;
  const commandStatus = reportCommandStartEvent({
    biLogger,
    command,
    cliVersion,
    flow,
    extensions: {},
    extraFields: void 0
  });
  const transaction = createCommandTransaction(panorama, command);
  try {
    transaction.start();
    await commandFn(model);
    transaction.finish();
    commandStatus.succeed();
  } catch (e) {
    commandStatus.failed(e);
    throw e;
  }
}

export {
  renderCommand,
  runCommand
};
//# sourceMappingURL=chunk-BA4M4QGR.js.map