import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  writeAppManifestFile
} from "./chunk-44QES6WV.js";
import {
  createLogger
} from "./chunk-6XBCKUUC.js";
import {
  createAppManifest,
  getComponentBuildSourceBuilder,
  getViteConfig,
  getViteConfig4 as getViteConfig2,
  getViteConfig5 as getViteConfig3,
  getViteConfig6 as getViteConfig4,
  getViteConfig7 as getViteConfig5,
  getViteConfig8 as getViteConfig6,
  validateComponents
} from "./chunk-PM5KYXD2.js";
import "./chunk-4SVKU2EF.js";
import {
  CliError,
  CliErrorCode,
  DevCenterComponent,
  getDistFolder,
  require_lib
} from "./chunk-EUAUZCCH.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-4EFJZ3GQ.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 runViteBuild(config) {
  try {
    return await viteBuild(config);
  } catch (error) {
    throw new CliError({
      code: CliErrorCode.FailedToBuildApp(),
      cause: error
    });
  }
}
function runBackendBuild(projectModel, customLogger) {
  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)
  );
  if (backendComponents.length === 0) {
    return;
  }
  return runViteBuild(
    getViteConfig(
      projectModel,
      { customLogger },
      {
        appSecret: "__APP_SECRET__",
        webhookPublicKey: "__WEBHOOK_PUBLIC_KEY__"
      }
    )
  );
}
function runWorkerBuild(projectModel, customLogger) {
  const workerComponents = projectModel.components.filter(
    (0, import_variant.isType)(DevCenterComponent.BlocksWidget)
  );
  if (workerComponents.length === 0) {
    return;
  }
  return runViteBuild(getViteConfig3(projectModel, { customLogger }));
}
function runSiteComponentsClientBuild(projectModel, customLogger) {
  const components = projectModel.components.filter(
    (0, import_variant.isType)(DevCenterComponent.SiteComponent)
  );
  if (components.length === 0) {
    return;
  }
  return runViteBuild(
    getViteConfig4(projectModel, { customLogger })
  );
}
async function runSiteComponentsServerBuild(projectModel, customLogger) {
  const components = projectModel.components.filter(
    (0, import_variant.isType)(DevCenterComponent.SiteComponent)
  );
  if (components.length === 0) {
    return;
  }
  const output = [];
  const configs = getViteConfig6(projectModel, {
    customLogger
  });
  for (const config of configs) {
    const result = await runViteBuild(config);
    if (Array.isArray(result)) {
      output.push(...result);
    } else if ("output" in result) {
      output.push(result);
    }
  }
  return output;
}
async function runSiteComponentsSdkBuild(projectModel, customLogger) {
  const components = projectModel.components.filter(
    (0, import_variant.isType)(DevCenterComponent.SiteComponent)
  );
  if (components.length === 0) {
    return;
  }
  const output = [];
  const configs = getViteConfig5(projectModel, {
    customLogger
  });
  for (const config of configs) {
    const result = await runViteBuild(config);
    if (Array.isArray(result)) {
      output.push(...result);
    } else if ("output" in result) {
      output.push(result);
    }
  }
  return output;
}
async function build(projectModel) {
  if (projectModel.components.length === 0) {
    throw new CliError({
      code: CliErrorCode.NoExtensionsFound(),
      cause: null
    });
  }
  validateComponents(projectModel.components);
  const logger = createLogger();
  await rm(getDistFolder(projectModel.projectFolder), {
    recursive: true,
    force: true
  }).catch((e) => {
    throw new CliError({
      code: CliErrorCode.FailedToCleanDistFolder(),
      cause: e
    });
  });
  const backendBuildOutput = await runBackendBuild(projectModel, logger);
  const browserBuildOutput = await runViteBuild(
    getViteConfig2(projectModel, { customLogger: logger })
  );
  const workerBuildOutput = await runWorkerBuild(projectModel, logger);
  const siteComponentsClientBuildOutput = await runSiteComponentsClientBuild(
    projectModel,
    logger
  );
  const siteComponentsServerBuildOutput = await runSiteComponentsServerBuild(
    projectModel,
    logger
  );
  const siteComponentsSdkBuildOutput = await runSiteComponentsSdkBuild(
    projectModel,
    logger
  );
  const manifest = await createAppManifest({
    appId: projectModel.config.appId,
    components: projectModel.components,
    sentryDSN: projectModel.config.sentryDSN,
    componentSourceBuilder: getComponentBuildSourceBuilder(
      projectModel,
      { browserBuildOutput },
      { workerBuildOutput },
      { backendBuildOutput },
      { siteComponentsClientBuildOutput },
      { siteComponentsServerBuildOutput },
      { siteComponentsSdkBuildOutput }
    )
  });
  await writeAppManifestFile({
    projectFolder: projectModel.projectFolder,
    manifest
  });
}
export {
  build
};
//# sourceMappingURL=build-QVOZ3YGC.js.map