import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  PlatformViewer,
  assertBuildOutputExists
} from "./chunk-O6WUNJLD.js";
import {
  SiteProvider
} from "./chunk-NUHN5M5Q.js";
import {
  createLogger
} from "./chunk-FQ6UZCSF.js";
import {
  readAppManifestFile
} from "./chunk-KUGUNAKD.js";
import {
  ArmTagProvider,
  SiteOverrideProvider,
  createSiteOverrides,
  useARMClient,
  useBlocksServicesClient,
  useSite,
  useSiteOverrideClient
} from "./chunk-D7O6F2SQ.js";
import "./chunk-HEHKVE3E.js";
import "./chunk-ZOFE4LJ7.js";
import "./chunk-ZR3N5INU.js";
import "./chunk-7VEP3B3B.js";
import {
  getViteConfig,
  getViteServerUrl,
  updateAppManifestWithBaseUrl,
  updateBlocksComponents
} from "./chunk-HWET32VV.js";
import "./chunk-K2BZ3TOE.js";
import {
  useProjectModel
} from "./chunk-UVMRGZ5H.js";
import {
  Box_default,
  InfoIcon,
  Link,
  PointerIcon,
  Spinner,
  SuccessIcon,
  Text,
  Trans,
  require_lib,
  require_react,
  useAsync,
  useTranslation
} from "./chunk-32MMFZ46.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-PYIAC2GK.js";

// src/components/ServeCommand/index.ts
init_esm_shims();

// src/components/ServeCommand/ServeCommand.tsx
init_esm_shims();
var import_react4 = __toESM(require_react(), 1);

// src/components/ServeCommand/ServeEnvironment.tsx
init_esm_shims();
var import_react2 = __toESM(require_react(), 1);
var import_variant = __toESM(require_lib(), 1);

// src/components/ServeCommand/hooks/usePreviewServer.ts
init_esm_shims();
var import_react = __toESM(require_react(), 1);
import { preview } from "vite";
function usePreviewServer() {
  const previewServerRef = (0, import_react.useRef)();
  const { model } = useProjectModel();
  (0, import_react.useEffect)(() => {
    return () => {
      previewServerRef.current?.httpServer.close();
    };
  }, []);
  const logger = (0, import_react.useMemo)(() => createLogger({ timestamp: true }), []);
  return (0, import_react.useCallback)(async () => {
    const previewServer = await preview(
      await getViteConfig(model, {
        type: "build" /* build */,
        customLogger: logger
      })
    );
    previewServerRef.current = previewServer;
    return getViteServerUrl(previewServer);
  }, [model, logger]);
}

// src/components/ServeCommand/ServeEnvironment.tsx
var ServeEnvironmentContext = (0, import_react2.createContext)(null);
var ServeEnvironmentProvider = ({ children }) => {
  const { model } = useProjectModel();
  const { t } = useTranslation();
  const { site } = useSite();
  const arm = useARMClient();
  const { createOverride } = useSiteOverrideClient();
  const createPreviewServer = usePreviewServer();
  const blocksServicesClient = useBlocksServicesClient();
  const { status } = useAsync(async () => {
    await assertBuildOutputExists(model.projectFolder);
    const serverUrl = await createPreviewServer();
    const manifest = await readAppManifestFile(model.projectFolder);
    const blocksComponentsData = await blocksServicesClient.prepareBlocksComponentsData(model);
    const updatedManifest = updateAppManifestWithBaseUrl(
      updateBlocksComponents(manifest, blocksComponentsData),
      serverUrl
    );
    const armTag = await arm.createAppRelease(
      updatedManifest,
      model.config.projectId
    );
    const overrides = createSiteOverrides(model);
    const { id: siteOverrideId } = overrides ? await createOverride({
      appId: model.config.appId,
      siteId: site.id,
      overrides
    }) : await Promise.resolve({ id: void 0 });
    return { armTag, serverUrl, siteOverrideId };
  }, []);
  return (0, import_variant.match)(status, {
    Error: () => null,
    Loading: () => /* @__PURE__ */ import_react2.default.createElement(Spinner, { text: t("serve_command.starting_serve_environment") }),
    Success: ({ result: { armTag, serverUrl, siteOverrideId } }) => /* @__PURE__ */ import_react2.default.createElement(ServeEnvironmentContext.Provider, { value: { serverUrl } }, /* @__PURE__ */ import_react2.default.createElement(SiteOverrideProvider, { siteOverrideId }, /* @__PURE__ */ import_react2.default.createElement(ArmTagProvider, { armTag }, children)))
  });
};
function useServeEnvironment() {
  const context = (0, import_react2.useContext)(ServeEnvironmentContext);
  if (context == null) {
    throw new Error(
      "useServeEnvironment() cannot be used without an <ServeEnvironmentProvider>"
    );
  }
  return context;
}

// src/components/ServeCommand/ServeCommandScreen.tsx
init_esm_shims();
var import_react3 = __toESM(require_react(), 1);
function ServeCommandScreen() {
  const { site } = useSite();
  const { serverUrl } = useServeEnvironment();
  return /* @__PURE__ */ import_react3.default.createElement(Box_default, { flexDirection: "column" }, /* @__PURE__ */ import_react3.default.createElement(Box_default, { gap: 1 }, /* @__PURE__ */ import_react3.default.createElement(Text, { skin: "success" }, /* @__PURE__ */ import_react3.default.createElement(SuccessIcon, null)), /* @__PURE__ */ import_react3.default.createElement(
    Trans,
    {
      i18nKey: "serve_command.starting_serve_environment_success",
      components: [/* @__PURE__ */ import_react3.default.createElement(Link, { url: serverUrl })]
    }
  )), /* @__PURE__ */ import_react3.default.createElement(Box_default, { gap: 1 }, /* @__PURE__ */ import_react3.default.createElement(InfoIcon, null), /* @__PURE__ */ import_react3.default.createElement(Trans, { i18nKey: "serve_command.production_build" })), /* @__PURE__ */ import_react3.default.createElement(Box_default, { gap: 1, paddingTop: 1, paddingBottom: 1 }, /* @__PURE__ */ import_react3.default.createElement(PointerIcon, null), /* @__PURE__ */ import_react3.default.createElement(
    Trans,
    {
      i18nKey: "general.dev_site.current",
      values: { name: site.displayName }
    }
  )), /* @__PURE__ */ import_react3.default.createElement(Box_default, { paddingBottom: 1, flexDirection: "column" }, /* @__PURE__ */ import_react3.default.createElement(PlatformViewer, null)));
}

// src/components/ServeCommand/ServeCommand.tsx
var ServeCommand = () => {
  return /* @__PURE__ */ import_react4.default.createElement(SiteProvider, null, /* @__PURE__ */ import_react4.default.createElement(ServeEnvironmentProvider, null, /* @__PURE__ */ import_react4.default.createElement(ServeCommandScreen, null)));
};
export {
  ServeCommand,
  ServeEnvironmentProvider,
  useServeEnvironment
};
//# sourceMappingURL=ServeCommand-XVAHY5PD.js.map