import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  addUserFields,
  reportCommandStartEvent
} from "./chunk-GFXZXRO4.js";
import {
  messages_default
} from "./chunk-5ZI5USPK.js";
import {
  DebugLogProvider
} from "./chunk-657KFISY.js";
import {
  AccountAuthProvider,
  BiProvider,
  CliError,
  CliErrorCode,
  ErrorReporterProvider,
  I18nProvider,
  ProjectModelProvider,
  getDebugLogFilePath,
  loadProjectModel,
  package_default,
  render,
  require_react
} from "./chunk-VCFM6SMQ.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, { biLogger, errorReporter }) {
  errorReporter.setContext("Application Info", {
    "Application ID": model.config.appId,
    "Project ID": model.config.projectId
  });
  biLogger.updateDefaults({
    _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, { biLogger, errorReporter }, tree, options) {
  await runCommand(command, { biLogger, errorReporter }, async (model) => {
    if (model.components.length === 0 && !options?.skipProjectValidation) {
      throw new CliError({
        code: CliErrorCode.NoExtensionsFound(),
        cause: null
      });
    }
    biLogger.updateDefaults({ command: command.name() });
    const debugLogFilePath = getDebugLogFilePath(model.projectFolder);
    const handleLogin = (userInfo) => {
      errorReporter.setUser({ id: userInfo.userId });
      addUserFields(biLogger, userInfo.userId);
    };
    await render(
      /* @__PURE__ */ import_react.default.createElement(BiProvider, { value: biLogger }, /* @__PURE__ */ import_react.default.createElement(ErrorReporterProvider, { value: errorReporter }, /* @__PURE__ */ import_react.default.createElement(AccountAuthProvider, { onLogin: handleLogin }, /* @__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, services);
  services.biLogger.updateDefaults({
    command: command.name()
  });
  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)
  });
  try {
    await commandFn(model);
    commandStatus.succeed();
  } catch (e) {
    commandStatus.failed(e);
    throw e;
  }
}

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