import path2 from 'path';
import { fileURLToPath, pathToFileURL } from 'url';
import { existsSync, mkdirSync } from 'fs';
import { writeFile } from 'fs/promises';
import { createHash } from 'crypto';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __defProp2 = Object.defineProperty;
var __name2 = /* @__PURE__ */ __name((target, value) => __defProp2(target, "name", { value, configurable: true }), "__name");
function rootDir() {
  return process.cwd();
}
__name(rootDir, "rootDir");
__name2(rootDir, "rootDir");
function outputDirOverride() {
  const { CHROMATIC_ARCHIVE_LOCATION } = process.env;
  return CHROMATIC_ARCHIVE_LOCATION;
}
__name(outputDirOverride, "outputDirOverride");
__name2(outputDirOverride, "outputDirOverride");
function archivesDir(defaultOutputDir) {
  const outputDir = outputDirOverride() || defaultOutputDir;
  return path2.resolve(rootDir(), outputDir, "chromatic-archives");
}
__name(archivesDir, "archivesDir");
__name2(archivesDir, "archivesDir");
function checkArchivesDirExists(defaultOutputDir) {
  const dir = archivesDir(defaultOutputDir);
  if (!existsSync(dir)) {
    throw new Error(`Chromatic archives directory cannot be found: ${dir}

Please make sure that you have run your E2E tests, or have set the CHROMATIC_ARCHIVE_LOCATION env var if the output directory for the tests is not in the standard location.`);
  }
}
__name(checkArchivesDirExists, "checkArchivesDirExists");
__name2(checkArchivesDirExists, "checkArchivesDirExists");
function ensureDir(directory) {
  if (!existsSync(directory)) {
    mkdirSync(directory, {
      recursive: true
    });
  }
}
__name(ensureDir, "ensureDir");
__name2(ensureDir, "ensureDir");
async function outputFile(filePath, data) {
  ensureDir(path2.dirname(filePath));
  return writeFile(filePath, data, {
    mode: 511
  });
}
__name(outputFile, "outputFile");
__name2(outputFile, "outputFile");
async function outputJSONFile(filePath, data) {
  return outputFile(filePath, JSON.stringify(data));
}
__name(outputJSONFile, "outputJSONFile");
__name2(outputJSONFile, "outputJSONFile");
function hash(data) {
  return createHash("shake256", {
    outputLength: 2
  }).update(data).digest("hex");
}
__name(hash, "hash");
__name2(hash, "hash");
var MAX_FILE_NAME_BYTE_LENGTH = 255;
function truncateFileName(filePath, maxByteLength = MAX_FILE_NAME_BYTE_LENGTH) {
  const encoder = new TextEncoder();
  const decoder = new TextDecoder();
  const filePathParts = filePath.split("/");
  const fileName = filePathParts.pop();
  const fileNameByteArray = encoder.encode(fileName);
  if (fileNameByteArray.byteLength <= maxByteLength) {
    return filePath;
  }
  const hashedFileName = hash(fileName);
  const [baseName, ...extensions] = fileName.split(".");
  const baseNameByteArray = encoder.encode(baseName);
  const ext = extensions.join(".");
  const extLength = ext.length === 0 ? 0 : encoder.encode(ext).byteLength + 1;
  const lengthHashAndExt = encoder.encode(hashedFileName).byteLength + extLength;
  const truncatedBaseNameByteArray = baseNameByteArray.slice(0, maxByteLength - lengthHashAndExt);
  const truncatedBaseName = decoder.decode(truncatedBaseNameByteArray);
  const truncatedFileName = [
    `${truncatedBaseName}${hashedFileName}`,
    ext
  ].filter(Boolean).join(".");
  return [
    ...filePathParts,
    truncatedFileName
  ].join("/");
}
__name(truncateFileName, "truncateFileName");
__name2(truncateFileName, "truncateFileName");

// src/constants.ts
var DEFAULT_OUTPUT_DIR = "cypress/downloads";

// src/storybook-config/main.ts
var __filename$1 = fileURLToPath(import.meta.url);
var __dirname$1 = path2.dirname(__filename$1);
var packageRoot = path2.resolve(__dirname$1, "..", "..");
var embeddedDir = path2.join(packageRoot, "embedded", "node_modules");
var main_default = {
  stories: [
    path2.resolve(archivesDir(DEFAULT_OUTPUT_DIR), "*.stories.json")
  ],
  managerEntries: [
    path2.resolve(__dirname$1, "manager.mjs")
  ],
  previewAnnotations: [
    path2.resolve(__dirname$1, "preview.mjs")
  ],
  framework: {
    name: path2.join(embeddedDir, "@storybook", "server-webpack5"),
    options: {}
  },
  core: {
    // ESM does not support directory imports; point to the package entry file.
    builder: pathToFileURL(path2.join(embeddedDir, "@storybook", "builder-webpack5", "dist", "index.js")).href,
    renderer: pathToFileURL(path2.join(embeddedDir, "@storybook", "server", "preset.js")).href
  },
  staticDirs: [
    path2.resolve(archivesDir(DEFAULT_OUTPUT_DIR), "archive")
  ]
};

export { main_default as default };
