import { loadToken, createInternalClientContext, third_party_applications_exports } from './chunk-WYC3GIR7.js';
import { ExitProcessError } from './chunk-PBYE7BDH.js';
import './chunk-N67QHFFS.js';
import { init_esm_shims } from './chunk-H26U4HNL.js';
import { consola } from 'consola';
import { autoVersion, AutoVersionError } from '@osdk/foundry-config-json';
import archiver from 'archiver';
import { colorize } from 'consola/utils';
import * as fs from 'fs';
import path from 'path';
import { Readable } from 'stream';
import prettyBytes from 'pretty-bytes';

// src/commands/site/deploy/siteDeployCommand.mts
init_esm_shims();

// src/util/maybeUpdateJemmaCustomMetadata.ts
init_esm_shims();
var JEMMA_JOB_CUSTOM_METADATA_PATH = "JEMMA_JOB_CUSTOM_METADATA_PATH";
function maybeUpdateJemmaCustomMetadata({
  siteLink,
  siteVersion
}) {
  const jemmaCustomMetadataFilePath = getJemmaCustomMetadataFilePath();
  if (jemmaCustomMetadataFilePath == null) {
    return;
  }
  try {
    const customMetadata = getOrCreateCustomMetadata(jemmaCustomMetadataFilePath);
    customMetadata.siteLink = siteLink;
    customMetadata.siteVersion = siteVersion;
    writeCustomMetadata(jemmaCustomMetadataFilePath, customMetadata);
  } catch (err) {
    consola.error(`Failed to update custom metadata file at ${jemmaCustomMetadataFilePath}`);
  }
}
function getJemmaCustomMetadataFilePath() {
  return process.env[JEMMA_JOB_CUSTOM_METADATA_PATH];
}
function getOrCreateCustomMetadata(jemmaCustomMetadataFilePath) {
  try {
    const parsedValue = JSON.parse(fs.readFileSync(jemmaCustomMetadataFilePath, "utf-8"));
    return typeof parsedValue === "object" && parsedValue != null ? parsedValue : {};
  } catch (e) {
    return {};
  }
}
function writeCustomMetadata(jemmaCustomMetadataFilePath, customMetadata) {
  fs.writeFileSync(jemmaCustomMetadataFilePath, JSON.stringify(customMetadata, null, 2));
}

// src/commands/site/deploy/siteDeployCommand.mts
async function siteDeployCommand({
  selectedVersion,
  application,
  foundryUrl,
  uploadOnly,
  snapshot,
  snapshotId,
  directory,
  token,
  tokenFile
}) {
  const loadedToken = await loadToken(token, tokenFile);
  const tokenProvider = () => loadedToken;
  const clientCtx = createInternalClientContext(foundryUrl, tokenProvider);
  let siteVersion;
  if (typeof selectedVersion === "string") {
    siteVersion = selectedVersion;
  } else {
    siteVersion = await findAutoVersion(selectedVersion);
    consola.info(`Auto version inferred next version to be: ${siteVersion}`);
  }
  consola.debug(`Using directory for site files: "${path.resolve(directory)}`);
  const stat = await fs.promises.stat(directory);
  if (!stat.isDirectory()) {
    throw new ExitProcessError(2, "Specified path exists but is not a directory");
  }
  consola.start("Zipping site files");
  const archive = archiver("zip").directory(directory, false);
  logArchiveStats(archive);
  if (snapshot) {
    await uploadSnapshot(clientCtx, application, siteVersion, snapshotId ?? "", archive);
    consola.info("Snapshot mode enabled, skipping deployment");
    return;
  }
  await upload(clientCtx, application, siteVersion, archive);
  let siteLink;
  if (!uploadOnly) {
    const website = await third_party_applications_exports.deployWebsite(clientCtx, application, {
      version: siteVersion
    });
    consola.success(`Deployed ${siteVersion} successfully`);
    const domain = website.subdomains[0];
    if (domain != null) {
      siteLink = `https://${domain}`;
      logSiteLink("View live site:", siteLink);
    }
  } else {
    const website = await third_party_applications_exports.getWebsite(clientCtx, application);
    const domain = website?.subdomains[0];
    consola.info("Upload only mode enabled, skipping deployment");
    if (domain != null) {
      siteLink = `https://${domain}/.system/preview?previewVersion=${siteVersion}`;
      logSiteLink("Preview link:", siteLink);
    }
  }
  if (siteLink != null) {
    maybeUpdateJemmaCustomMetadata({
      siteLink,
      siteVersion
    });
  }
}
async function findAutoVersion(config) {
  try {
    return await autoVersion(config);
  } catch (e) {
    throw new ExitProcessError(2, e instanceof Error ? e.message : void 0, e instanceof AutoVersionError ? e.tip : void 0);
  }
}
async function uploadSnapshot(clientCtx, application, siteVersion, snapshotId, archive) {
  consola.start("Uploading snapshot site files");
  await Promise.all([third_party_applications_exports.uploadSnapshotVersion(
    clientCtx,
    application,
    siteVersion,
    snapshotId,
    Readable.toWeb(archive)
    // This cast is because the dom fetch doesn't align type wise with streams
  ), archive.finalize()]);
  consola.success("Snapshot upload complete");
}
async function upload(clientCtx, application, siteVersion, archive) {
  consola.start("Uploading site files");
  await Promise.all([third_party_applications_exports.uploadVersion(
    clientCtx,
    application,
    siteVersion,
    Readable.toWeb(archive)
    // This cast is because the dom fetch doesn't align type wise with streams
  ), archive.finalize()]);
  consola.success("Upload complete");
}
function logArchiveStats(archive) {
  let archiveStats = {
    fileCount: 0,
    bytes: 0
  };
  archive.on("progress", (progress) => {
    archiveStats = {
      fileCount: progress.entries.total,
      bytes: progress.fs.totalBytes
    };
  });
  archive.on("finish", () => {
    consola.info(`Zipped ${prettyBytes(archiveStats.bytes, {
      binary: true
    })} total over ${archiveStats.fileCount} files`);
  });
}
function logSiteLink(title, link) {
  consola.box({
    message: `${title}

${colorize("green", link)}`,
    style: {
      padding: 2,
      borderColor: "green",
      borderStyle: "rounded"
    }
  });
}

export { siteDeployCommand as default };
//# sourceMappingURL=siteDeployCommand-JUTNRGZC.js.map
//# sourceMappingURL=siteDeployCommand-JUTNRGZC.js.map