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 {
  createPreviewTag
} from "./chunk-BBL4SXGY.js";
import {
  ArmTagProvider,
  SiteOverrideProvider,
  createSiteOverrides,
  useARMClient,
  useBlocksServicesClient,
  useSite,
  useSiteOverrideClient
} from "./chunk-D7O6F2SQ.js";
import {
  useBackendComponents
} from "./chunk-HEHKVE3E.js";
import {
  ComponentType2 as ComponentType,
  saveAppConfig,
  uploadApplication,
  useBiLogger,
  useDevCenterClient,
  useHttpClient,
  useProjectModel
} from "./chunk-UVMRGZ5H.js";
import {
  Alert,
  Box_default,
  PointerIcon,
  Spinner,
  Text,
  Trans,
  require_lib,
  require_react,
  useAsync,
  useAsyncCallback,
  useTranslation
} from "./chunk-32MMFZ46.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-PYIAC2GK.js";

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

// src/components/PreviewCommand/PreviewCommand.tsx
init_esm_shims();
var import_react3 = __toESM(require_react(), 1);

// src/components/PreviewCommand/Preview.tsx
init_esm_shims();
var import_react2 = __toESM(require_react(), 1);
var import_variant2 = __toESM(require_lib(), 1);

// src/components/PreviewCommand/ActionStatus.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
var import_variant = __toESM(require_lib(), 1);
var ActionStatus = ({ status, loadingText, successText }) => (0, import_variant.match)(status, {
  NotRequested: () => null,
  Loading: () => /* @__PURE__ */ import_react.default.createElement(Spinner, { text: loadingText }),
  Error: () => null,
  Success: () => /* @__PURE__ */ import_react.default.createElement(Alert, { type: "success" }, successText)
});

// src/components/PreviewCommand/Preview.tsx
var Preview = ({ baseUrl, onPreviewCreated }) => {
  const { site } = useSite();
  const { t } = useTranslation();
  const { model } = useProjectModel();
  const { backendComponents } = useBackendComponents();
  const arm = useARMClient();
  const biLogger = useBiLogger();
  const { getAppByVersion } = useDevCenterClient();
  const { createOverride } = useSiteOverrideClient();
  const httpClient = useHttpClient({ type: "backoffice" });
  const blocksServicesClient = useBlocksServicesClient();
  const uploadApplicationAction = useAsyncCallback(
    () => uploadApplication(
      model.config.appId,
      model.projectFolder,
      biLogger,
      httpClient,
      backendComponents.length > 0
    )
  );
  const createSiteOverrideIdAction = useAsyncCallback(
    (_, overrides) => createOverride({
      appId: model.config.appId,
      siteId: site.id,
      overrides
    })
  );
  const createPreviewTagAction = useAsyncCallback(
    (_, serveUrl, topologyComponentId, backendServerUrl, blocksComponents, monitoringData, monitoringComponentId) => createPreviewTag({
      model,
      arm,
      serveUrl,
      backendServerUrl,
      blocksComponents,
      topologyComponentId,
      monitoringData,
      monitoringComponentId
    })
  );
  const { status: createPreviewStatus } = useAsync(async () => {
    await assertBuildOutputExists(model.projectFolder);
    const { serveUrl, backendServerUrl } = baseUrl ? { serveUrl: baseUrl, backendServerUrl: void 0 } : await uploadApplicationAction.execute();
    const {
      data: { components }
    } = await getAppByVersion({
      appId: model.config.appId
    });
    const topologyComponent = components.find(
      (component) => {
        return component.compType === ComponentType.TOPOLOGY;
      }
    );
    const monitoringComponent = components.find(
      (component) => {
        return component.compType === ComponentType.MONITORING;
      }
    );
    const overrides = createSiteOverrides(model);
    const { id: siteOverrideId } = overrides ? await createSiteOverrideIdAction.execute(overrides) : await Promise.resolve({ id: void 0 });
    const blocksComponentsData = await blocksServicesClient.prepareBlocksComponentsData(model);
    const armTag = await createPreviewTagAction.execute(
      serveUrl,
      topologyComponent?.compId,
      backendServerUrl,
      blocksComponentsData,
      monitoringComponent?.compData.monitoring,
      monitoringComponent?.compId
    );
    if (topologyComponent || monitoringComponent) {
      await saveAppConfig(model.projectFolder, {
        ...topologyComponent ? { topologyComponentId: topologyComponent.compId } : {},
        ...monitoringComponent ? { monitoringComponentId: monitoringComponent.compId } : {}
      });
    }
    onPreviewCreated?.(armTag, backendServerUrl);
    return { armTag, siteOverrideId };
  }, []);
  return /* @__PURE__ */ import_react2.default.createElement(import_react2.default.Fragment, null, /* @__PURE__ */ import_react2.default.createElement(
    ActionStatus,
    {
      status: uploadApplicationAction.status,
      loadingText: t("preview_command.uploading_files"),
      successText: t("preview_command.uploading_files_success")
    }
  ), /* @__PURE__ */ import_react2.default.createElement(
    ActionStatus,
    {
      status: createPreviewTagAction.status,
      loadingText: t("preview_command.creating_preview"),
      successText: t("preview_command.creating_preview_success")
    }
  ), (0, import_variant2.match)(createPreviewStatus, {
    Error: () => null,
    Loading: () => null,
    Success: ({ result: { armTag, siteOverrideId } }) => /* @__PURE__ */ import_react2.default.createElement(SiteOverrideProvider, { siteOverrideId }, /* @__PURE__ */ import_react2.default.createElement(ArmTagProvider, { armTag }, /* @__PURE__ */ import_react2.default.createElement(Box_default, { rowGap: 1, marginY: 1, flexDirection: "column" }, /* @__PURE__ */ import_react2.default.createElement(Box_default, { gap: 1 }, /* @__PURE__ */ import_react2.default.createElement(PointerIcon, null), /* @__PURE__ */ import_react2.default.createElement(
      Trans,
      {
        i18nKey: "general.dev_site.current",
        values: { name: site.displayName }
      }
    )), /* @__PURE__ */ import_react2.default.createElement(Box_default, { rowGap: 1 }, /* @__PURE__ */ import_react2.default.createElement(PlatformViewer, null)), /* @__PURE__ */ import_react2.default.createElement(Text, { skin: "info" }, t("preview_command.note_about_temporary_backend")))))
  }));
};

// src/components/PreviewCommand/PreviewCommand.tsx
var PreviewCommand = ({ baseUrl, siteIdOverride, onPreviewCreated }) => {
  return /* @__PURE__ */ import_react3.default.createElement(SiteProvider, { siteId: siteIdOverride }, /* @__PURE__ */ import_react3.default.createElement(Preview, { baseUrl, onPreviewCreated }));
};

export {
  PreviewCommand
};
//# sourceMappingURL=chunk-5P4ZUCVU.js.map