import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  addUserFields,
  reportCommandStartEvent
} from "./chunk-YLGWO3AM.js";
import {
  messages_default
} from "./chunk-QBRIDBYB.js";
import {
  DebugLogProvider
} from "./chunk-MXJB23DQ.js";
import {
  AuthProvider,
  BiProvider,
  CliError,
  CliErrorCode,
  ErrorReporterProvider,
  HttpClientProvider,
  I18nProvider,
  PanoramaProvider,
  ProjectModelProvider,
  buildHttpClientHeaders,
  createCommandTransaction,
  getDebugLogFilePath,
  loadProjectModel,
  package_default,
  render,
  require_react
} from "./chunk-HPSJVSZT.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-4EFJZ3GQ.js";

// src/commands/render-command.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
import { cwd } from "node:process";
async function initServices({
  model,
  services,
  command
}) {
  const { biLogger, errorReporter, panorama } = services;
  const project = "Apps";
  errorReporter.setTag("project", project);
  errorReporter.setTag("project.app_id", model.config.appId);
  errorReporter.setTag("project.id", model.config.projectId);
  panorama.setDefaultClientData({
    cliProject: project,
    cliProjectId: model.config.projectId,
    cliAppId: model.config.appId
  });
  biLogger.updateDefaults({
    flow: project,
    command: command.name(),
    cliVersion: package_default.version,
    _appId: model.config.appId
  });
}
function getExtraBiFields(command, components) {
  switch (command) {
    case "dev":
    case "preview":
      return {
        is_mocking_values: components.some(
          (component) => component.type === "EmbeddedScript" && component.paramsOverride
        )
      };
    default:
      return;
  }
}
async function renderCommand(command, services, tree, options) {
  await runCommand(command, services, async (model) => {
    const { biLogger, errorReporter, panorama } = services;
    if (model.components.length === 0 && !options?.skipProjectValidation) {
      throw new CliError({
        code: CliErrorCode.NoExtensionsFound(),
        cause: null
      });
    }
    const debugLogFilePath = getDebugLogFilePath(model.projectFolder);
    const handleLogin = (userInfo) => {
      errorReporter.setUser({ id: userInfo.userId });
      addUserFields(biLogger, userInfo.userId);
    };
    const headers = buildHttpClientHeaders({
      flow: "Apps",
      command: command.name(),
      cliVersion: package_default.version
    });
    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(I18nProvider, { messages: messages_default }, /* @__PURE__ */ import_react.default.createElement(DebugLogProvider, { filePath: debugLogFilePath }, /* @__PURE__ */ import_react.default.createElement(
        ProjectModelProvider,
        {
          value: model,
          watchForChanges: options?.watchForModelChanges
        },
        tree
      ))))))))
    );
  });
}
async function runCommand(command, services, commandFn) {
  const model = await loadProjectModel(cwd());
  await initServices({ model, command, services });
  const commandStatus = reportCommandStartEvent({
    biLogger: services.biLogger,
    command,
    cliVersion: package_default.version,
    flow: "Apps",
    projectId: model.config.projectId,
    extensions: model.components.reduce((acc, item) => {
      const value = acc[item.type];
      acc[item.type] = value ? value + 1 : 1;
      return acc;
    }, {}),
    extraFields: getExtraBiFields(command.name(), model.components)
  });
  const transaction = createCommandTransaction(services.panorama, command);
  try {
    transaction.start();
    await commandFn(model);
    transaction.finish();
    commandStatus.succeed();
  } catch (e) {
    commandStatus.failed(e);
    throw e;
  }
}

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