import { createRequire as _createRequire } from 'node:module';
const require = _createRequire(import.meta.url);
import {
  createPreviewTag
} from "./chunk-BBL4SXGY.js";
import {
  addUserFields
} from "./chunk-NMEVBUJF.js";
import "./chunk-KUGUNAKD.js";
import {
  ARMClient,
  SiteListClient,
  resolveConfiguredSite,
  resolveDashboardExtensionUrl,
  resolvePlatformUrl
} from "./chunk-D7O6F2SQ.js";
import "./chunk-HEHKVE3E.js";
import "./chunk-ZOFE4LJ7.js";
import "./chunk-ZR3N5INU.js";
import "./chunk-7VEP3B3B.js";
import "./chunk-HWET32VV.js";
import "./chunk-K2BZ3TOE.js";
import {
  DevCenterComponent,
  PlatformType,
  createHttpClient,
  isLoggedInToAccount,
  uploadApplication
} from "./chunk-UVMRGZ5H.js";
import {
  CliError,
  CliErrorCode,
  require_lib
} from "./chunk-32MMFZ46.js";
import {
  __toESM,
  init_esm_shims
} from "./chunk-PYIAC2GK.js";

// src/non-interactive/preview/index.ts
init_esm_shims();

// src/non-interactive/preview/preview.ts
init_esm_shims();
var import_variant3 = __toESM(require_lib(), 1);

// src/non-interactive/preview/resolve-site.ts
init_esm_shims();
var import_variant = __toESM(require_lib(), 1);
async function resolveSite(model, httpClient, siteId) {
  const siteListClient = new SiteListClient(httpClient);
  const site = await resolveConfiguredSite({
    siteId,
    projectFolder: model.projectFolder,
    siteListClient
  });
  return (0, import_variant.match)(site, {
    NotConfigured: () => {
      throw new CliError({
        code: CliErrorCode.SiteConfiguredRequired(),
        cause: null
      });
    },
    NotFound: () => {
      throw new CliError({
        code: CliErrorCode.SiteConfiguredRequired(),
        cause: null
      });
    },
    Resolved: ({ site: site2 }) => site2
  });
}

// src/non-interactive/preview/dashboard-page-url.ts
init_esm_shims();
var import_variant2 = __toESM(require_lib(), 1);
var getDashboardPageUrl = ({
  model,
  site,
  tag
}) => {
  const dashboardExtensions = model.components.filter(
    (component) => (0, import_variant2.isType)(component, DevCenterComponent.DashboardPage) || (0, import_variant2.isType)(component, DevCenterComponent.DashboardPlugin) || (0, import_variant2.isType)(component, DevCenterComponent.DashboardMenuPlugin)
  );
  const envConfig = {
    appId: model.config.appId,
    site,
    tag
  };
  return dashboardExtensions[0] ? resolveDashboardExtensionUrl(dashboardExtensions[0], envConfig) : resolvePlatformUrl(PlatformType.Dashboard(), envConfig);
};

// src/non-interactive/logger.ts
init_esm_shims();
function log(message) {
  console.log(message);
}

// src/non-interactive/preview/preview.ts
async function nonInteractivePreview(options, model, services) {
  const authData = await isLoggedInToAccount();
  if (!authData) {
    throw new CliError({
      code: CliErrorCode.AuthenticationRequired(),
      cause: null
    });
  }
  services.errorReporter.setUser({ id: authData.userInfo.userId });
  addUserFields(services.biLogger, authData.userInfo.userId);
  const httpClient = createHttpClient({
    type: "backoffice",
    getAppToken: () => authData.getAccessToken()
  });
  const site = await resolveSite(model, httpClient, options.siteId);
  const backendComponents = model.components.filter(
    (component) => (0, import_variant3.isType)(component, DevCenterComponent.Api) || (0, import_variant3.isType)(component, DevCenterComponent.WebMethod) || (0, import_variant3.isType)(component, DevCenterComponent.Event) || (0, import_variant3.isType)(component, DevCenterComponent.ServicePlugin)
  );
  const { serveUrl, backendServerUrl } = options.baseUrl ? { serveUrl: options.baseUrl, backendServerUrl: void 0 } : await uploadApplication(
    model.config.appId,
    model.projectFolder,
    services.biLogger,
    httpClient,
    backendComponents.length > 0
  );
  const arm = new ARMClient(httpClient);
  const tag = await createPreviewTag({
    model,
    arm,
    serveUrl,
    backendServerUrl
  });
  const dashboardPageUrl = getDashboardPageUrl({ site, model, tag });
  log(dashboardPageUrl);
}
export {
  nonInteractivePreview
};
//# sourceMappingURL=preview-LX67DEK4.js.map