import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  writeAppManifestFile
} from "./chunk-ZSOQ5MNN.js";
import {
  createLogger
} from "./chunk-VYBQ6YPB.js";
import {
  createAppManifest,
  getComponentBuildSourceBuilder,
  getViteConfig,
  getViteConfig3 as getViteConfig2,
  getViteConfig4 as getViteConfig3,
  validateComponents
} from "./chunk-4NK5BDKP.js";
import "./chunk-Q22BBPPS.js";
import {
  API_BASEURL_PLACEHOLDER,
  CliError,
  CliErrorCode,
  DevCenterComponent,
  getDistFolder,
  require_lib
} from "./chunk-A4MGEEM2.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);
import { rm } from "node:fs/promises";
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 workerComponents = projectModel.components.filter(
    (component) => (0, import_variant.isType)(component, DevCenterComponent.BlocksWidget) || (0, import_variant.isType)(component, DevCenterComponent.SiteComponent)
  );
  const logger = createLogger();
  await rm(getDistFolder(projectModel.projectFolder), {
    recursive: true,
    force: true
  }).catch((e) => {
    throw new CliError({
      code: CliErrorCode.FailedToCleanDistFolder(),
      cause: e
    });
  });
  const backendBuildOutput = backendComponents.length > 0 ? await viteBuild(
    await getViteConfig(
      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 browserBuildOutput = await viteBuild(
    await getViteConfig2(
      projectModel,
      {
        type: "build" /* build */,
        customLogger: logger
      },
      {
        baseBackendUrl: API_BASEURL_PLACEHOLDER
      }
    )
  ).catch((e) => {
    throw new CliError({
      code: CliErrorCode.FailedToBuildApp(),
      cause: e
    });
  });
  const workerBuildOutput = workerComponents.length > 0 ? await viteBuild(
    await getViteConfig3(
      projectModel,
      { type: "build" /* build */, customLogger: logger },
      {
        baseBackendUrl: API_BASEURL_PLACEHOLDER
      }
    )
  ).catch((e) => {
    throw new CliError({
      code: CliErrorCode.FailedToBuildApp(),
      cause: e
    });
  }) : void 0;
  const manifest = await createAppManifest({
    appId: projectModel.config.appId,
    components: projectModel.components,
    sentryDSN: projectModel.config.sentryDSN,
    componentSourceBuilder: getComponentBuildSourceBuilder(
      projectModel,
      { browserBuildOutput },
      { workerBuildOutput },
      { backendBuildOutput }
    )
  });
  await writeAppManifestFile({
    projectFolder: projectModel.projectFolder,
    manifest
  });
}
export {
  build
};
//# sourceMappingURL=build-LEBRLX2P.js.map