import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  createLogger
} from "./chunk-FQ6UZCSF.js";
import {
  writeAppManifestFile
} from "./chunk-KUGUNAKD.js";
import {
  createAppManifest,
  getComponentSourceBuilder,
  getViteConfig,
  getViteConfig2,
  validateComponents
} from "./chunk-HWET32VV.js";
import "./chunk-K2BZ3TOE.js";
import {
  API_BASEURL_PLACEHOLDER,
  DevCenterComponent
} from "./chunk-UVMRGZ5H.js";
import {
  CliError,
  CliErrorCode,
  require_lib
} from "./chunk-32MMFZ46.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-PYIAC2GK.js";

// src/build.ts
init_esm_shims();
import { build as viteBuild } from "vite";
var import_variant = __toESM(require_lib(), 1);
async function build(projectModel) {
  if (projectModel.components.length === 0) {
    throw new CliError({
      code: CliErrorCode.NoExtensionsFound(),
      cause: null
    });
  }
  validateComponents(projectModel.components);
  const backendComponents = projectModel.components.filter(
    (component) => (0, import_variant.isType)(component, DevCenterComponent.Api) || (0, import_variant.isType)(component, DevCenterComponent.WebMethod) || (0, import_variant.isType)(component, DevCenterComponent.Event) || (0, import_variant.isType)(component, DevCenterComponent.ServicePlugin)
  );
  const logger = createLogger();
  const backendBuildOutput = backendComponents.length > 0 ? await viteBuild(
    await getViteConfig2(
      projectModel,
      { type: "build" /* build */, customLogger: logger },
      {
        appSecret: "__APP_SECRET__",
        webhookPublicKey: "__WEBHOOK_PUBLIC_KEY__"
      }
    )
  ).catch((e) => {
    throw new CliError({
      code: CliErrorCode.FailedToBuildApp(),
      cause: e
    });
  }) : void 0;
  const clientBuildOutput = await viteBuild(
    await getViteConfig(
      projectModel,
      {
        type: "build" /* build */,
        customLogger: logger
      },
      {
        baseBackendUrl: API_BASEURL_PLACEHOLDER
      }
    )
  ).catch((e) => {
    throw new CliError({
      code: CliErrorCode.FailedToBuildApp(),
      cause: e
    });
  });
  const manifest = await createAppManifest({
    appId: projectModel.config.appId,
    components: projectModel.components,
    sentryDSN: projectModel.config.sentryDSN,
    componentSourceBuilder: getComponentSourceBuilder(
      projectModel,
      { clientBuildOutput },
      { backendBuildOutput }
    )
  });
  await writeAppManifestFile({
    projectFolder: projectModel.projectFolder,
    manifest
  });
}
export {
  build
};
//# sourceMappingURL=build-2E4HKDUB.js.map