import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  DeploymentSource,
  getDefaultBranch,
  getDeploymentSourceData
} from "./chunk-FNFKSHYK.js";
import "./chunk-C74BYNT5.js";
import "./chunk-KB3GWH37.js";
import "./chunk-RW55Y77T.js";
import "./chunk-ICJSZGEK.js";
import {
  DeploymentStatus,
  useDocumentManagementClient
} from "./chunk-Y6GHMYGK.js";
import "./chunk-J3APDAIV.js";
import "./chunk-MYWHJYA3.js";
import "./chunk-DZ3U3XXP.js";
import {
  useProjectModel
} from "./chunk-KSFSFWUC.js";
import "./chunk-4BLGYECY.js";
import {
  useVeloClient
} from "./chunk-R5WNFZTS.js";
import "./chunk-RFWFU5ZV.js";
import "./chunk-C7TVBDR6.js";
import "./chunk-KDZXPOOF.js";
import "./chunk-QRGKMYDD.js";
import "./chunk-NMCF7XG5.js";
import "./chunk-MHV22UIU.js";
import "./chunk-BRX4HWJE.js";
import "./chunk-ZBFOAJ5H.js";
import "./chunk-YNEGRO3E.js";
import {
  ErrorViewer,
  writeCrashReport
} from "./chunk-YHFCDWGT.js";
import "./chunk-IVQPYJ5M.js";
import {
  Trans,
  useTranslation
} from "./chunk-MDUBZZTI.js";
import "./chunk-RPTAJN5Y.js";
import {
  Alert,
  Box_default,
  Link,
  SelectInput,
  Spinner,
  useAsync,
  useAsyncCallback
} from "./chunk-QK47PIWK.js";
import {
  require_react
} from "./chunk-TODKQSCF.js";
import "./chunk-AFPDHQYI.js";
import "./chunk-HYKBBZLS.js";
import "./chunk-C5RTKQVL.js";
import "./chunk-DGJS6KZB.js";
import {
  getDebugLogFilePath
} from "./chunk-YT5BPQDJ.js";
import "./chunk-LU3R3EBR.js";
import "./chunk-XZUGLEBK.js";
import "./chunk-AYCGCYN3.js";
import "./chunk-SDLCFZRL.js";
import {
  require_lib
} from "./chunk-WGV2XQSB.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-EXLZF52D.js";

// ../cli-site-old/src/preview/PreviewCommand.tsx
init_esm_shims();
var import_react3 = __toESM(require_react(), 1);
var import_variant2 = __toESM(require_lib(), 1);

// ../cli-site-old/src/preview/SelectPreviewSource.tsx
init_esm_shims();
var import_react = __toESM(require_react(), 1);
var import_variant = __toESM(require_lib(), 1);
var SelectPreviewSource = ({
  projectFolder,
  onSubmit
}) => {
  const { t } = useTranslation();
  const { status } = useAsync(
    () => getDefaultBranch(
      (e) => writeCrashReport(getDebugLogFilePath(projectFolder), e, /* @__PURE__ */ new Date())
    ),
    []
  );
  return (0, import_variant.match)(status, {
    Error: () => null,
    Loading: () => /* @__PURE__ */ import_react.default.createElement(Spinner, null),
    Success: ({ result: defaultBranchName }) => /* @__PURE__ */ import_react.default.createElement(
      SelectInput,
      {
        label: t("preview_command.prompt_what_to_preview"),
        options: [
          {
            key: "remote",
            value: DeploymentSource.remote(),
            title: defaultBranchName ? t("preview_command.prompt_what_to_preview_choice_latest", {
              branchName: defaultBranchName
            }) : t(
              "preview_command.prompt_what_to_preview_choice_latest_fallback"
            )
          },
          {
            key: "local",
            value: DeploymentSource.local(),
            title: t("preview_command.prompt_what_to_preview_choice_local")
          }
        ],
        onSubmit
      }
    )
  });
};

// ../cli-site-old/src/preview/useCreatePreview.ts
init_esm_shims();
var import_react2 = __toESM(require_react(), 1);
function useCreatePreview() {
  const { model } = useProjectModel();
  const veloClient = useVeloClient();
  const dmClient = useDocumentManagementClient();
  return (0, import_react2.useCallback)(
    async (previewSource) => {
      const deploymentSourceData = await getDeploymentSourceData(
        model,
        previewSource === "local" ? DeploymentSource.local() : DeploymentSource.remote()
      );
      const preview = await veloClient.deploySitePreview(deploymentSourceData);
      return {
        deploymentId: preview.deploymentId,
        previewUrl: preview.deploymentShortUrl ?? preview.deploymentUrl,
        deploymentStatus: preview.isPublishPipelineDeployment ? await dmClient.getDeploymentDetails(preview.deploymentId) : DeploymentStatus.Success()
      };
    },
    [dmClient, model, veloClient]
  );
}

// ../cli-site-old/src/preview/PreviewCommand.tsx
var PreviewCommand = (options) => {
  const { t } = useTranslation();
  const { model } = useProjectModel();
  const createPreview = useCreatePreview();
  const { status, execute: handleCreatingPreview } = useAsyncCallback(
    async (_, previewSource) => {
      const { previewUrl, deploymentStatus } = await createPreview(previewSource);
      if (!options.force && (0, import_variant2.isType)(deploymentStatus, DeploymentStatus.Error)) {
        throw deploymentStatus.deploymentError;
      }
      return { previewUrl, deploymentStatus };
    }
  );
  useAsync(async () => {
    if (options.source) {
      void handleCreatingPreview(options.source);
    }
  }, []);
  return /* @__PURE__ */ import_react3.default.createElement(import_react3.default.Fragment, null, !options.source && /* @__PURE__ */ import_react3.default.createElement(
    SelectPreviewSource,
    {
      projectFolder: model.projectFolder,
      onSubmit: ({ value }) => handleCreatingPreview(value.type)
    }
  ), (0, import_variant2.match)(status, {
    NotRequested: () => null,
    Error: () => null,
    Loading: () => /* @__PURE__ */ import_react3.default.createElement(Spinner, { text: t("preview_command.creating_preview") }),
    Success: ({ result: { previewUrl, deploymentStatus } }) => /* @__PURE__ */ import_react3.default.createElement(import_react3.default.Fragment, null, (0, import_variant2.isType)(deploymentStatus, DeploymentStatus.Error) ? /* @__PURE__ */ import_react3.default.createElement(Box_default, { marginBottom: 1, flexDirection: "column" }, /* @__PURE__ */ import_react3.default.createElement(ErrorViewer, { error: deploymentStatus.deploymentError })) : null, /* @__PURE__ */ import_react3.default.createElement(Alert, { type: "success" }, /* @__PURE__ */ import_react3.default.createElement(
      Trans,
      {
        i18nKey: "preview_command.preview_created",
        components: [/* @__PURE__ */ import_react3.default.createElement(Link, { url: previewUrl })]
      }
    )))
  }));
};
export {
  PreviewCommand
};
//# sourceMappingURL=PreviewCommand-MN6RVSMU.js.map